Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SymfonySessionFactory
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2namespace Apie\ApieBundle\Session;
3
4use Apie\Core\Session\SessionFactoryInterface;
5use Symfony\Component\HttpFoundation\RequestStack;
6use Symfony\Component\HttpFoundation\Session\Session;
7use Symfony\Component\HttpFoundation\Session\SessionInterface;
8use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
9use Symfony\Component\HttpKernel\KernelInterface;
10use WeakMap;
11
12class 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}