Passed
Push — master ( db691e...cd1392 )
by SignpostMarv
03:38
created

HttpHandler::ValidateDaftSourceSubConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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