Test Failed
Push — master ( 7f033d...d11579 )
by SignpostMarv
03:06
created

HttpHandler::AttachToEventDispatcher()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftFramework;
8
9
use InvalidArgumentException;
10
use SignpostMarv\DaftRouter\DaftSource;
11
use SignpostMarv\DaftRouter\Router\Compiler;
12
use Symfony\Component\EventDispatcher\EventDispatcher;
13
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
14
use Symfony\Component\HttpKernel\KernelEvents;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
18
class HttpHandler extends Framework
19
{
20
    const ERROR_SOURCE_CONFIG = DaftSource::class . ' config does not specify "%s" correctly.';
21
22
    const ERROR_ROUTER_CACHE_FILE_PATH =
23
        DaftSource::class .
24
        ' config property cacheFile does not exist under the framework base path.';
25
26
    /**
27
    * @var string
28
    */
29
    private $routerCacheFile;
30
31
    /**
32
    * @var array<int, string>
33 58
    */
34
    private $routerSources;
35 58
36
    public function __construct(string $baseUrl, string $basePath, array $config = [])
37 34
    {
38
        parent::__construct($baseUrl, $basePath, $config);
39
40
        $this->routerCacheFile = (string) ((array) $config[DaftSource::class])['cacheFile'];
41
42 34
        /**
43
        * @var string[] $sources
44 34
        */
45 34
        $sources = (array) ((array) $config[DaftSource::class])['sources'];
46
47 26
        $this->routerSources = array_values(array_filter($sources, 'is_string'));
48
    }
49 26
50
    public function handle(Request $request) : Response
51 26
    {
52
        self::PairWithRequest($this, $request);
53 26
54
        $dispatcher = Compiler::ObtainDispatcher(
55 26
            [
56
                'cacheFile' => $this->routerCacheFile,
57
            ],
58 26
            ...$this->routerSources
59 26
        );
60 26
61
        return $dispatcher->handle(
62
            $request,
63
            (string) parse_url($this->ObtainBaseUrl(), PHP_URL_PATH)
64 58
        );
65
    }
66
67
    public function AttachToEventDispatcher(EventDispatcher $dispatcher) : void
68
    {
69 58
        $dispatcher->addListener(KernelEvents::REQUEST, function (GetResponseEvent $e) : void {
70
            if ( ! $e->hasResponse()) {
71 58
                $e->setResponse($this->handle($e->getRequest()));
72 18
            }
73
        });
74
    }
75 40
76
    protected function ValidateConfig(array $config) : array
77 34
    {
78
        /**
79
        * @var array|null
80 40
        */
81
        $subConfig = $config[DaftSource::class] ?? null;
82 40
83 2
        if ( ! is_array($subConfig) || ! isset($subConfig['cacheFile'], $subConfig['sources'])) {
84 38
            throw new InvalidArgumentException(sprintf('%s config not found!', DaftSource::class));
85 2
        }
86 36
87 2
        $this->ValidateDaftSourceSubConfig($subConfig);
88
89 34
        return parent::ValidateConfig($config);
90
    }
91
92
    protected function ValidateDaftSourceSubConfig(array $subConfig) : void
93
    {
94
        if ( ! is_string($subConfig['cacheFile'])) {
95
            throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'cacheFile'));
96
        } elseif ( ! is_array($subConfig['sources'])) {
97
            throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'sources'));
98
        } elseif ( ! $this->FileIsUnderBasePath($subConfig['cacheFile'], false)) {
99
            throw new InvalidArgumentException(self::ERROR_ROUTER_CACHE_FILE_PATH);
100
        }
101
    }
102
}
103