Passed
Push — master ( 5e186e...dbb2e0 )
by Anton
02:56
created

CookiesBootloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\Bootloader\Http;
11
12
use Spiral\Boot\Bootloader\Bootloader;
13
use Spiral\Boot\Bootloader\DependedInterface;
14
use Spiral\Config\ConfiguratorInterface;
15
use Spiral\Config\Patch\Append;
16
use Spiral\Cookies\Config\CookiesConfig;
17
use Spiral\Cookies\Middleware\CookiesMiddleware;
18
use Spiral\Core\Container\SingletonInterface;
19
20
final class CookiesBootloader extends Bootloader implements SingletonInterface, DependedInterface
21
{
22
    /** @var ConfiguratorInterface */
23
    private $config;
24
25
    /**
26
     * @param ConfiguratorInterface $config
27
     */
28
    public function __construct(ConfiguratorInterface $config)
29
    {
30
        $this->config = $config;
31
    }
32
33
    /**
34
     * @param HttpBootloader $http
35
     */
36
    public function boot(HttpBootloader $http)
37
    {
38
        $this->config->setDefaults('cookies', [
39
            'domain'   => '.%s',
40
            'method'   => CookiesConfig::COOKIE_ENCRYPT,
41
            'excluded' => ['PHPSESSID', 'csrf-token']
42
        ]);
43
44
        $http->addMiddleware(CookiesMiddleware::class);
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function defineDependencies(): array
51
    {
52
        return [
53
            HttpBootloader::class
54
        ];
55
    }
56
57
    /**
58
     * Disable protection for given cookie.
59
     *
60
     * @param string $cookie
61
     */
62
    public function whitelistCookie(string $cookie)
63
    {
64
        $this->config->modify('cookies', new Append('excluded', null, $cookie));
65
    }
66
}