Skip to content

Commit 6c7e20f

Browse files
authored
Merge pull request #11 from brainbits/feature/symfony6
feat: Rework for symfony 6, drop PHP 7.4 support
2 parents 94aa626 + c59cf72 commit 6c7e20f

8 files changed

+65
-14
lines changed

.github/workflows/test.yml

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ jobs:
1616
matrix:
1717
dependencies: ["lowest", "highest"]
1818
php-version:
19-
- "7.4"
2019
- "8.0"
2120
- "8.1"
2221
operating-system: ["ubuntu-latest"]

composer.json

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^7.4|^8.0"
14+
"php": "^8.0"
1515
},
1616
"require-dev": {
1717
"mikey179/vfsstream": "^1.6.10",
1818
"phpunit/phpunit": "^9.5",
19-
"symfony/http-foundation": ">=4.4",
20-
"symfony/security-core": ">=4.4",
19+
"symfony/http-foundation": "^5.4|^6.0",
20+
"symfony/security-core": "^5.4|^6.0",
2121
"phpspec/prophecy-phpunit": "^2.0.1",
2222
"squizlabs/php_codesniffer": "^3.6",
2323
"brainbits/phpcs-standard": "^5.0",
24-
"phpstan/phpstan": "^0.12.99",
25-
"brainbits/phpstan-rules": "^2.0"
24+
"phpstan/phpstan": "^1.0",
25+
"brainbits/phpstan-rules": "^3.0"
2626
},
2727
"suggest": {
2828
"symfony/http-foundation": "If you want to use the SymfonySessionOwnerFactory",
@@ -38,5 +38,10 @@
3838
"branch-alias": {
3939
"dev-master": "4.0-dev"
4040
}
41+
},
42+
"config": {
43+
"allow-plugins": {
44+
"dealerdirect/phpcodesniffer-composer-installer": true
45+
}
4146
}
4247
}

src/Exception/NoSessionException.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the brainbits blocking package.
7+
*
8+
* (c) brainbits GmbH (http://www.brainbits.net)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Brainbits\Blocking\Exception;
15+
16+
class NoSessionException extends RuntimeException
17+
{
18+
public static function create(): self
19+
{
20+
return new self('No session.');
21+
}
22+
}

src/Exception/UnserializeFailedException.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace Brainbits\Blocking\Exception;
1515

16+
use function gettype;
17+
use function is_scalar;
18+
1619
/**
1720
* Unserialize failed exception.
1821
*/
@@ -29,7 +32,7 @@ private function __construct(string $message, string $input)
2932

3033
public static function createFromInput(mixed $input): self
3134
{
32-
return new self('Unserialize failed.', (string) $input);
35+
return new self('Unserialize failed.', is_scalar($input) ? (string) $input : gettype($input));
3336
}
3437

3538
public function getInput(): string

src/Owner/SymfonySessionOwnerFactory.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,35 @@
1313

1414
namespace Brainbits\Blocking\Owner;
1515

16+
use Brainbits\Blocking\Exception\NoSessionException;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\RequestStack;
1619
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1720

1821
/**
1922
* Symfony session owner factory.
2023
*/
2124
class SymfonySessionOwnerFactory implements OwnerFactoryInterface
2225
{
23-
private SessionInterface $session;
26+
private RequestStack $requestStack;
2427

25-
public function __construct(SessionInterface $session)
28+
public function __construct(RequestStack $requestStack)
2629
{
27-
$this->session = $session;
30+
$this->requestStack = $requestStack;
2831
}
2932

3033
public function createOwner(): OwnerInterface
3134
{
32-
return new Owner($this->session->getId());
35+
$request = $this->requestStack->getCurrentRequest();
36+
if (!$request instanceof Request) {
37+
throw NoSessionException::create();
38+
}
39+
40+
$session = $request->getSession();
41+
if (!$session instanceof SessionInterface) {
42+
throw NoSessionException::create();
43+
}
44+
45+
return new Owner($session->getId());
3346
}
3447
}

src/Owner/SymfonyTokenOwnerFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ public function createOwner(): OwnerInterface
4242
throw NoUserFoundException::create();
4343
}
4444

45-
return new Owner($user->getUsername());
45+
return new Owner($user->getUserIdentifier());
4646
}
4747
}

tests/Owner/SymfonySessionOwnerFactoryTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Brainbits\Blocking\Owner\SymfonySessionOwnerFactory;
1616
use PHPUnit\Framework\TestCase;
1717
use Prophecy\PhpUnit\ProphecyTrait;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\RequestStack;
1820
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1921

2022
/**
@@ -26,7 +28,14 @@ class SymfonySessionOwnerFactoryTest extends TestCase
2628

2729
public function testToString(): void
2830
{
29-
$factory = new SymfonySessionOwnerFactory($this->createSession('foo'));
31+
$request = new Request();
32+
$request->setSession($this->createSession('foo'));
33+
34+
$requestStack = new RequestStack();
35+
$requestStack->push($request);
36+
37+
$factory = new SymfonySessionOwnerFactory($requestStack);
38+
3039
$owner = $factory->createOwner();
3140

3241
$this->assertEquals($owner, new Owner('foo'));

tests/Owner/SymfonyTokenOwnerFactoryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testE2(): void
6060
private function createTokenStorage(string $username, bool $createToken = true, bool $createUser = true)
6161
{
6262
$user = $this->prophesize(UserInterface::class);
63-
$user->getUsername()
63+
$user->getUserIdentifier()
6464
->willReturn($username);
6565

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

0 commit comments

Comments
 (0)