Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| SymfonySessionFactory | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\ApieBundle\Session; |
| 3 | |
| 4 | use Apie\Core\Session\SessionFactoryInterface; |
| 5 | use Symfony\Component\HttpFoundation\RequestStack; |
| 6 | use Symfony\Component\HttpFoundation\Session\Session; |
| 7 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 8 | use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
| 9 | use Symfony\Component\HttpKernel\KernelInterface; |
| 10 | use WeakMap; |
| 11 | |
| 12 | class SymfonySessionFactory implements SessionFactoryInterface |
| 13 | { |
| 14 | /** @var WeakMap<KernelInterface, SessionInterface>|null */ |
| 15 | private static ?WeakMap $mockSessions = null; |
| 16 | |
| 17 | public function __construct( |
| 18 | private readonly RequestStack $requestStack, |
| 19 | private readonly KernelInterface $kernel |
| 20 | ) { |
| 21 | } |
| 22 | |
| 23 | public function create(): SessionInterface |
| 24 | { |
| 25 | self::$mockSessions ??= new WeakMap(); |
| 26 | $request = $this->requestStack->getMainRequest(); |
| 27 | if ($request && $request->hasSession()) { |
| 28 | return $request->getSession(); |
| 29 | } |
| 30 | if (isset(self::$mockSessions[$this->kernel])) { |
| 31 | return self::$mockSessions[$this->kernel]; |
| 32 | } |
| 33 | |
| 34 | return self::$mockSessions[$this->kernel] = new Session(new MockArraySessionStorage()); |
| 35 | } |
| 36 | } |