Passed
Push — master ( 436c58...98654e )
by Anton
06:21 queued 03:52
created

HttpAuthBootloader::addTransport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Bootloader\Auth;
13
14
use Spiral\Auth\Config\AuthConfig;
15
use Spiral\Auth\HttpTransportInterface;
16
use Spiral\Auth\Middleware\AuthMiddleware;
17
use Spiral\Auth\Transport\CookieTransport;
18
use Spiral\Auth\Transport\HeaderTransport;
19
use Spiral\Auth\TransportRegistry;
20
use Spiral\Boot\Bootloader\Bootloader;
21
use Spiral\Bootloader\Http\HttpBootloader;
22
use Spiral\Config\ConfiguratorInterface;
23
use Spiral\Config\Patch\Append;
24
use Spiral\Core\Container\Autowire;
25
use Spiral\Core\Container\SingletonInterface;
26
use Spiral\Core\FactoryInterface;
27
28
/**
29
 * Enables Auth middleware and http transports to read and write tokens in PSR-7 request/response.
30
 */
31
final class HttpAuthBootloader extends Bootloader implements SingletonInterface
32
{
33
    protected const DEPENDENCIES = [
34
        AuthBootloader::class,
35
        HttpBootloader::class
36
    ];
37
38
    protected const SINGLETONS = [
39
        TransportRegistry::class => [self::class, 'transportRegistry']
40
    ];
41
42
    /** @var ConfiguratorInterface */
43
    private $config;
44
45
    /**
46
     * @param ConfiguratorInterface $config
47
     */
48
    public function __construct(ConfiguratorInterface $config)
49
    {
50
        $this->config = $config;
51
    }
52
53
    /**
54
     * @param HttpBootloader $http
55
     */
56
    public function boot(HttpBootloader $http): void
57
    {
58
        $http->addMiddleware(AuthMiddleware::class);
59
60
        $this->config->setDefaults('auth', [
61
            'defaultTransport' => 'cookie',
62
            'transports'       => [
63
                'cookie' => new CookieTransport('token'),
64
                'header' => new HeaderTransport('X-Auth-Token')
65
            ]
66
        ]);
67
    }
68
69
    /**
70
     * Add new Http token transport.
71
     *
72
     * @param string                                 $name
73
     * @param HttpTransportInterface|Autowire|string $transport
74
     */
75
    public function addTransport(string $name, $transport): void
76
    {
77
        $this->config->modify('auth', new Append('transports', $name, $transport));
78
    }
79
80
    /**
81
     * @param AuthConfig       $config
82
     * @param FactoryInterface $factory
83
     * @return TransportRegistry
84
     */
85
    private function transportRegistry(AuthConfig $config, FactoryInterface $factory): TransportRegistry
86
    {
87
        $registry = new TransportRegistry();
88
        $registry->setDefaultTransport($config->getDefaultTransport());
89
90
        foreach ($config->getTransports() as $name => $transport) {
91
            if ($transport instanceof Autowire) {
92
                $transport = $transport->resolve($factory);
93
            }
94
95
            $registry->setTransport($name, $transport);
0 ignored issues
show
Bug introduced by
It seems like $transport can also be of type null; however, parameter $transport of Spiral\Auth\TransportRegistry::setTransport() does only seem to accept Spiral\Auth\HttpTransportInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

95
            $registry->setTransport($name, /** @scrutinizer ignore-type */ $transport);
Loading history...
96
        }
97
98
        return $registry;
99
    }
100
}
101