Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
LaravelSessionFactory
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
6
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%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2namespace Apie\LaravelApie\Session;
3
4use Apie\Core\Session\SessionFactoryInterface;
5use Illuminate\Contracts\Foundation\Application;
6use Illuminate\Http\Request;
7use Illuminate\Session\SymfonySessionDecorator;
8use Symfony\Component\HttpFoundation\Session\Session;
9use Symfony\Component\HttpFoundation\Session\SessionInterface;
10use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
11use WeakMap;
12
13class LaravelSessionFactory implements SessionFactoryInterface
14{
15    /** @var WeakMap<Application, SessionInterface>|null */
16    private static ?WeakMap $mockSessions = null;
17
18    public function __construct(private readonly Application $app)
19    {
20    }
21
22    public function create(): SessionInterface
23    {
24        self::$mockSessions ??= new WeakMap();
25        if ($this->app->bound('request')) {
26            $request = $this->app->make('request');
27            if ($request instanceof Request && $request->hasSession()) {
28                return new SymfonySessionDecorator($request->session());
29            }
30        }
31        if (isset(self::$mockSessions[$this->app])) {
32            return self::$mockSessions[$this->app];
33        }
34
35        return self::$mockSessions[$this->app] = new Session(new MockArraySessionStorage());
36    }
37}