Passed
Push — master ( 03a7f0...f222ff )
by SignpostMarv
03:35
created

HttpHandler::ValidateConfig()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 6
nop 1
dl 0
loc 26
ccs 14
cts 14
cp 1
crap 6
rs 8.439
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 58
        if ( ! isset($config[DaftSource::class])) {
67 2
            throw new InvalidArgumentException(sprintf('%s config not found!', DaftSource::class));
68
        }
69
70 56
        $subConfig = (array) $config[DaftSource::class];
71
72 56
        if ( ! isset($subConfig['cacheFile'], $subConfig['sources'])) {
73 16
            throw new InvalidArgumentException(sprintf('%s config not found!', DaftSource::class));
74 40
        } elseif ( ! is_string($subConfig['cacheFile'])) {
75 2
            throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'cacheFile'));
76
        }
77
78
        /**
79
        * @var string $cacheFilename
80
        */
81 38
        $cacheFilename = $subConfig['cacheFile'];
82
83 38
        if ( ! is_array($subConfig['sources'])) {
84 2
            throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'sources'));
85 36
        } elseif ( ! $this->FileIsUnderBasePath($cacheFilename, false)) {
86 2
            throw new InvalidArgumentException(self::ERROR_ROUTER_CACHE_FILE_PATH);
87
        }
88
89 34
        return parent::ValidateConfig($config);
90
    }
91
}
92