Passed
Push — master ( 5b49ac...957a79 )
by SignpostMarv
02:45
created

HttpHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 9.6666
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
    public function __construct(string $baseUrl, string $basePath, array $config = [])
34
    {
35
        parent::__construct($baseUrl, $basePath, $config);
36
37
        $this->routerCacheFile = $config[DaftSource::class]['cacheFile'];
38
39
        $this->routerSources = array_values(array_filter(
40
            $config[DaftSource::class]['sources'],
41
            'is_string'
42
        ));
43
    }
44
45
    public function handle(Request $request) : Response
46
    {
47
        self::PairWithRequest($this, $request);
48
49
        $dispatcher = Compiler::ObtainDispatcher(
50
            [
51
                'cacheFile' => $this->routerCacheFile,
52
            ],
53
            ...$this->routerSources
54
        );
55
56
        return $dispatcher->handle($request, parse_url($this->ObtainBaseUrl(), PHP_URL_PATH));
57
    }
58
59
    protected function ValidateConfig(array $config) : array
60
    {
61
        if (
62
            ! isset(
63
                $config[DaftSource::class],
64
                $config[DaftSource::class]['cacheFile'],
65
                $config[DaftSource::class]['sources']
66
            )
67
        ) {
68
            throw new InvalidArgumentException(sprintf('%s config not found!', DaftSource::class));
69
        } elseif ( ! is_string($config[DaftSource::class]['cacheFile'])) {
70
            throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'cacheFile'));
71
        } elseif ( ! is_array($config[DaftSource::class]['sources'])) {
72
            throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'sources'));
73
        } elseif ( ! $this->FileIsUnderBasePath($config[DaftSource::class]['cacheFile'], false)) {
74
            throw new InvalidArgumentException(self::ERROR_ROUTER_CACHE_FILE_PATH);
75
        }
76
77
        return parent::ValidateConfig($config);
78
    }
79
}
80