Skip to content

Commit

Permalink
feat: Rework for symfony 6, drop PHP 7.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Wentz authored and Jost Voelker committed Nov 7, 2022
1 parent 94aa626 commit c59cf72
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 14 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jobs:
matrix:
dependencies: ["lowest", "highest"]
php-version:
- "7.4"
- "8.0"
- "8.1"
operating-system: ["ubuntu-latest"]
Expand Down
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
}
],
"require": {
"php": "^7.4|^8.0"
"php": "^8.0"
},
"require-dev": {
"mikey179/vfsstream": "^1.6.10",
"phpunit/phpunit": "^9.5",
"symfony/http-foundation": ">=4.4",
"symfony/security-core": ">=4.4",
"symfony/http-foundation": "^5.4|^6.0",
"symfony/security-core": "^5.4|^6.0",
"phpspec/prophecy-phpunit": "^2.0.1",
"squizlabs/php_codesniffer": "^3.6",
"brainbits/phpcs-standard": "^5.0",
"phpstan/phpstan": "^0.12.99",
"brainbits/phpstan-rules": "^2.0"
"phpstan/phpstan": "^1.0",
"brainbits/phpstan-rules": "^3.0"
},
"suggest": {
"symfony/http-foundation": "If you want to use the SymfonySessionOwnerFactory",
Expand All @@ -38,5 +38,10 @@
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
22 changes: 22 additions & 0 deletions src/Exception/NoSessionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This file is part of the brainbits blocking package.
*
* (c) brainbits GmbH (http://www.brainbits.net)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Brainbits\Blocking\Exception;

class NoSessionException extends RuntimeException
{
public static function create(): self
{
return new self('No session.');
}
}
5 changes: 4 additions & 1 deletion src/Exception/UnserializeFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace Brainbits\Blocking\Exception;

use function gettype;
use function is_scalar;

/**
* Unserialize failed exception.
*/
Expand All @@ -29,7 +32,7 @@ private function __construct(string $message, string $input)

public static function createFromInput(mixed $input): self
{
return new self('Unserialize failed.', (string) $input);
return new self('Unserialize failed.', is_scalar($input) ? (string) $input : gettype($input));
}

public function getInput(): string
Expand Down
21 changes: 17 additions & 4 deletions src/Owner/SymfonySessionOwnerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,35 @@

namespace Brainbits\Blocking\Owner;

use Brainbits\Blocking\Exception\NoSessionException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
* Symfony session owner factory.
*/
class SymfonySessionOwnerFactory implements OwnerFactoryInterface
{
private SessionInterface $session;
private RequestStack $requestStack;

public function __construct(SessionInterface $session)
public function __construct(RequestStack $requestStack)
{
$this->session = $session;
$this->requestStack = $requestStack;
}

public function createOwner(): OwnerInterface
{
return new Owner($this->session->getId());
$request = $this->requestStack->getCurrentRequest();
if (!$request instanceof Request) {
throw NoSessionException::create();
}

$session = $request->getSession();
if (!$session instanceof SessionInterface) {
throw NoSessionException::create();
}

return new Owner($session->getId());
}
}
2 changes: 1 addition & 1 deletion src/Owner/SymfonyTokenOwnerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public function createOwner(): OwnerInterface
throw NoUserFoundException::create();
}

return new Owner($user->getUsername());
return new Owner($user->getUserIdentifier());
}
}
11 changes: 10 additions & 1 deletion tests/Owner/SymfonySessionOwnerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Brainbits\Blocking\Owner\SymfonySessionOwnerFactory;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
Expand All @@ -26,7 +28,14 @@ class SymfonySessionOwnerFactoryTest extends TestCase

public function testToString(): void
{
$factory = new SymfonySessionOwnerFactory($this->createSession('foo'));
$request = new Request();
$request->setSession($this->createSession('foo'));

$requestStack = new RequestStack();
$requestStack->push($request);

$factory = new SymfonySessionOwnerFactory($requestStack);

$owner = $factory->createOwner();

$this->assertEquals($owner, new Owner('foo'));
Expand Down
2 changes: 1 addition & 1 deletion tests/Owner/SymfonyTokenOwnerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testE2(): void
private function createTokenStorage(string $username, bool $createToken = true, bool $createUser = true)
{
$user = $this->prophesize(UserInterface::class);
$user->getUsername()
$user->getUserIdentifier()
->willReturn($username);

$token = $this->prophesize(TokenInterface::class);
Expand Down

0 comments on commit c59cf72

Please sign in to comment.