Passed
Pull Request — master (#1104)
by Aleksei
26:20
created

SessionBootloader::defineBindings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 11
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 9.9
c 2
b 1
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Bootloader\Http;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Spiral\Boot\Bootloader\Bootloader;
9
use Spiral\Boot\DirectoriesInterface;
10
use Spiral\Bootloader\Http\Exception\ContextualObjectNotFoundException;
11
use Spiral\Bootloader\Http\Exception\InvalidRequestScopeException;
12
use Spiral\Config\ConfiguratorInterface;
13
use Spiral\Core\BinderInterface;
14
use Spiral\Core\Config\Proxy;
15
use Spiral\Core\Container\Autowire;
16
use Spiral\Framework\Spiral;
17
use Spiral\Session\Config\SessionConfig;
18
use Spiral\Session\Handler\FileHandler;
19
use Spiral\Session\Middleware\SessionMiddleware;
20
use Spiral\Session\SessionFactory;
21
use Spiral\Session\SessionFactoryInterface;
22
use Spiral\Session\SessionInterface;
23
24
final class SessionBootloader extends Bootloader
25
{
26 357
    public function __construct(
27
        private readonly BinderInterface $binder,
28
    ) {
29 357
    }
30
31 357
    public function defineBindings(): array
32
    {
33 357
        $this->binder
34 357
            ->getBinder(Spiral::Http)
0 ignored issues
show
Bug introduced by
The method getBinder() does not exist on Spiral\Core\BinderInterface. It seems like you code against a sub-type of Spiral\Core\BinderInterface such as Spiral\Core\Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
            ->/** @scrutinizer ignore-call */ 
35
              getBinder(Spiral::Http)
Loading history...
35 357
            ->bind(
36 357
                SessionInterface::class,
37 357
                static fn (?ServerRequestInterface $request): SessionInterface =>
38 10
                    ($request ?? throw new InvalidRequestScopeException(SessionInterface::class))
39 10
                        ->getAttribute(SessionMiddleware::ATTRIBUTE) ?? throw new ContextualObjectNotFoundException(
40 10
                            SessionInterface::class,
41 10
                            SessionMiddleware::ATTRIBUTE,
42 10
                        )
43 357
            );
44 357
        $this->binder->bind(SessionInterface::class, new Proxy(SessionInterface::class, false));
45
46 357
        return [];
47
    }
48
49 357
    public function defineSingletons(): array
50
    {
51 357
        $http = $this->binder->getBinder(Spiral::Http);
52 357
        $http->bindSingleton(SessionFactory::class, SessionFactory::class);
53 357
        $http->bindSingleton(SessionFactoryInterface::class, SessionFactory::class);
54
55 357
        $this->binder->bind(SessionFactoryInterface::class, new Proxy(SessionFactoryInterface::class, true));
56
57 357
        return [];
58
    }
59
60
    /**
61
     * Automatically registers session starter middleware and excludes session cookie from
62
     * cookie protection.
63
     */
64 357
    public function init(
65
        ConfiguratorInterface $config,
66
        DirectoriesInterface $directories
67
    ): void {
68 357
        $config->setDefaults(
69 357
            SessionConfig::CONFIG,
70 357
            [
71 357
                'lifetime' => 86400,
72 357
                'cookie' => 'sid',
73 357
                'secure' => true,
74 357
                'sameSite' => null,
75 357
                'handler' => new Autowire(
76 357
                    FileHandler::class,
77 357
                    [
78 357
                        'directory' => $directories->get('runtime') . 'session',
79 357
                        'lifetime' => 86400,
80 357
                    ]
81 357
                ),
82 357
            ]
83 357
        );
84
    }
85
86 357
    public function boot(
87
        ConfiguratorInterface $config,
88
        CookiesBootloader $cookies
89
    ): void {
90 357
        $session = $config->getConfig(SessionConfig::CONFIG);
91
92 357
        $cookies->whitelistCookie($session['cookie']);
93
    }
94
}
95