Passed
Push — master ( 37709b...f45e89 )
by Aleksei
06:22 queued 17s
created

SessionBootloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Bootloader\Http;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Spiral\Boot\Bootloader\Bootloader;
10
use Spiral\Boot\DirectoriesInterface;
11
use Spiral\Bootloader\Http\Exception\ContextualObjectNotFoundException;
12
use Spiral\Bootloader\Http\Exception\InvalidRequestScopeException;
13
use Spiral\Config\ConfiguratorInterface;
14
use Spiral\Core\BinderInterface;
15
use Spiral\Core\Config\Proxy;
16
use Spiral\Core\Container\Autowire;
17
use Spiral\Framework\Spiral;
18
use Spiral\Session\Config\SessionConfig;
19
use Spiral\Session\Handler\FileHandler;
20
use Spiral\Session\Middleware\SessionMiddleware;
21
use Spiral\Session\SessionFactory;
22
use Spiral\Session\SessionFactoryInterface;
23
use Spiral\Session\SessionInterface;
24
25
final class SessionBootloader extends Bootloader
26
{
27 356
    public function __construct(
28
        private readonly BinderInterface $binder,
29
    ) {
30 356
    }
31
32 356
    public function defineBindings(): array
33
    {
34 356
        $this->binder->getBinder(Spiral::HttpRequest)->bind(SessionInterface::class, $this->resolveSession(...));
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
        $this->binder->/** @scrutinizer ignore-call */ 
35
                       getBinder(Spiral::HttpRequest)->bind(SessionInterface::class, $this->resolveSession(...));
Loading history...
35 356
        $this->binder->getBinder(Spiral::Http)
36 356
            ->bind(
37 356
                SessionInterface::class,
38 356
                new Proxy(SessionInterface::class, false, $this->resolveSession(...)),
39 356
            );
40 356
        $this->binder->bind(SessionInterface::class, new Proxy(SessionInterface::class, true), );
41
42 356
        return [];
43
    }
44
45 356
    public function defineSingletons(): array
46
    {
47 356
        $http = $this->binder->getBinder(Spiral::Http);
48 356
        $http->bindSingleton(SessionFactory::class, SessionFactory::class);
49 356
        $http->bindSingleton(SessionFactoryInterface::class, SessionFactory::class);
50
51 356
        $this->binder->bind(SessionFactoryInterface::class, new Proxy(SessionFactoryInterface::class, true));
52
53 356
        return [];
54
    }
55
56
    /**
57
     * Automatically registers session starter middleware and excludes session cookie from
58
     * cookie protection.
59
     */
60 356
    public function init(
61
        ConfiguratorInterface $config,
62
        DirectoriesInterface $directories
63
    ): void {
64 356
        $config->setDefaults(
65 356
            SessionConfig::CONFIG,
66 356
            [
67 356
                'lifetime' => 86400,
68 356
                'cookie' => 'sid',
69 356
                'secure' => true,
70 356
                'sameSite' => null,
71 356
                'handler' => new Autowire(
72 356
                    FileHandler::class,
73 356
                    [
74 356
                        'directory' => $directories->get('runtime') . 'session',
75 356
                        'lifetime' => 86400,
76 356
                    ]
77 356
                ),
78 356
            ]
79 356
        );
80
    }
81
82 356
    public function boot(
83
        ConfiguratorInterface $config,
84
        CookiesBootloader $cookies
85
    ): void {
86 356
        $session = $config->getConfig(SessionConfig::CONFIG);
87
88 356
        $cookies->whitelistCookie($session['cookie']);
89
    }
90
91 10
    private function resolveSession(ContainerInterface $container): SessionInterface
0 ignored issues
show
Unused Code introduced by
The method resolveSession() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
92
    {
93
        try {
94
            /** @var ServerRequestInterface $request */
95 10
            $request = $container->get(ServerRequestInterface::class);
96 10
            return $request->getAttribute(SessionMiddleware::ATTRIBUTE) ?? throw new ContextualObjectNotFoundException(
97 10
                SessionInterface::class,
98 10
                SessionMiddleware::ATTRIBUTE,
99 10
            );
100 2
        } catch (InvalidRequestScopeException $e) {
101 1
            throw new InvalidRequestScopeException(SessionInterface::class, previous: $e);
102
        }
103
    }
104
}
105