Passed
Push — master ( 7f2d87...e04bc8 )
by Anton
02:29 queued 11s
created

RouterBootloader::__construct()   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 1
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\Http;
13
14
use Psr\Container\ContainerInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Server\RequestHandlerInterface;
17
use Spiral\Boot\Bootloader\Bootloader;
18
use Spiral\Config\ConfiguratorInterface;
19
use Spiral\Core\Core;
20
use Spiral\Core\CoreInterface;
21
use Spiral\Core\Exception\ScopeException;
22
use Spiral\Http\Config\HttpConfig;
23
use Spiral\Router\RouteInterface;
24
use Spiral\Router\Router;
25
use Spiral\Router\RouterInterface;
26
use Spiral\Router\UriHandler;
27
28
final class RouterBootloader extends Bootloader
29
{
30
    protected const DEPENDENCIES = [
31
        HttpBootloader::class
32
    ];
33
34
    protected const SINGLETONS = [
35
        CoreInterface::class           => Core::class,
36
        RouterInterface::class         => [self::class, 'router'],
37
        RouteInterface::class          => [self::class, 'route'],
38
        RequestHandlerInterface::class => RouterInterface::class,
39
    ];
40
41
    /** @var ConfiguratorInterface */
42
    private $config;
43
44
    /**
45
     * @param ConfiguratorInterface $config
46
     */
47
    public function __construct(ConfiguratorInterface $config)
48
    {
49
        $this->config = $config;
50
    }
51
52
    /**
53
     * @param UriHandler         $uriHandler
54
     * @param ContainerInterface $container
55
     * @return RouterInterface
56
     */
57
    private function router(
58
        UriHandler $uriHandler,
59
        ContainerInterface $container
60
    ): RouterInterface {
61
        return new Router($this->config->getConfig('http')['basePath'], $uriHandler, $container);
62
    }
63
64
    /**
65
     * @param ServerRequestInterface $request
66
     * @return RouteInterface
67
     */
68
    private function route(ServerRequestInterface $request): RouteInterface
69
    {
70
        $route = $request->getAttribute(Router::ROUTE_ATTRIBUTE, null);
71
        if ($route === null) {
72
            throw new ScopeException('Unable to resolve Route, invalid request scope');
73
        }
74
75
        return $route;
76
    }
77
}
78