Passed
Push — master ( d11579...277b77 )
by SignpostMarv
02:53
created

HttpHandler::ValidateDaftSourceSubConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 4
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\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16
use Symfony\Component\HttpKernel\KernelEvents;
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
    */
34
    private $routerSources;
35
36 58
    public function __construct(string $baseUrl, string $basePath, array $config = [])
37
    {
38 58
        parent::__construct($baseUrl, $basePath, $config);
39
40 34
        $this->routerCacheFile = (string) ((array) $config[DaftSource::class])['cacheFile'];
41
42
        /**
43
        * @var string[] $sources
44
        */
45 34
        $sources = (array) ((array) $config[DaftSource::class])['sources'];
46
47 34
        $this->routerSources = array_values(array_filter($sources, 'is_string'));
48 34
    }
49
50 34
    public function handle(Request $request) : Response
51
    {
52 34
        self::PairWithRequest($this, $request);
53
54 34
        $dispatcher = Compiler::ObtainDispatcher(
55
            [
56 34
                'cacheFile' => $this->routerCacheFile,
57
            ],
58 34
            ...$this->routerSources
59
        );
60
61 34
        return $dispatcher->handle(
62 34
            $request,
63 34
            (string) parse_url($this->ObtainBaseUrl(), PHP_URL_PATH)
64
        );
65
    }
66
67 8
    public function AttachToEventDispatcher(EventDispatcher $dispatcher) : void
68
    {
69
        $dispatcher->addListener(KernelEvents::REQUEST, function (GetResponseEvent $e) : void {
70 8
            if ( ! $e->hasResponse()) {
71 8
                $e->setResponse($this->handle($e->getRequest()));
72
            }
73 8
        });
74 8
    }
75
76 58
    protected function ValidateConfig(array $config) : array
77
    {
78
        /**
79
        * @var array|null
80
        */
81 58
        $subConfig = $config[DaftSource::class] ?? null;
82
83 58
        if ( ! is_array($subConfig) || ! isset($subConfig['cacheFile'], $subConfig['sources'])) {
84 18
            throw new InvalidArgumentException(sprintf('%s config not found!', DaftSource::class));
85
        }
86
87 40
        $this->ValidateDaftSourceSubConfig($subConfig);
88
89 34
        return parent::ValidateConfig($config);
90
    }
91
92 40
    protected function ValidateDaftSourceSubConfig(array $subConfig) : void
93
    {
94 40
        if ( ! is_string($subConfig['cacheFile'])) {
95 2
            throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'cacheFile'));
96 38
        } elseif ( ! is_array($subConfig['sources'])) {
97 2
            throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'sources'));
98 36
        } elseif ( ! $this->FileIsUnderBasePath($subConfig['cacheFile'], false)) {
99 2
            throw new InvalidArgumentException(self::ERROR_ROUTER_CACHE_FILE_PATH);
100
        }
101 34
    }
102
}
103