Passed
Push — master ( c68a30...978f61 )
by SignpostMarv
04:06
created

HttpKernel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
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\Symfony\HttpKernel;
8
9
use InvalidArgumentException;
10
use SignpostMarv\DaftFramework\Framework;
11
use SignpostMarv\DaftRouter\DaftSource;
12
use SignpostMarv\DaftRouter\Router\Compiler;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\HttpKernelInterface;
16
17
class HttpKernel extends Framework implements HttpKernelInterface
18
{
19
    /**
20
    * @var string
21
    */
22
    private $routerCacheFile;
23
24
    /**
25
    * @var array<int, string>
26
    */
27
    private $routerSources;
28
29 16
    public function __construct(string $baseUrl, string $basePath, array $config = [])
30
    {
31 16
        parent::__construct($baseUrl, $basePath, $config);
32
33 8
        $this->routerCacheFile = $config[DaftSource::class]['cacheFile'];
34
35 8
        $this->routerSources = array_values(array_filter(
36 8
            $config[DaftSource::class]['sources'],
37 8
            'is_string'
38
        ));
39 8
    }
40
41 8
    public function handle(
42
        Request $request,
43
        $type = HttpKernelInterface::MASTER_REQUEST,
44
        $catch = true
45
    ) : Response {
46 8
        return $this->handleStrict($request, $type, $catch);
47
    }
48
49 8
    public function handleStrict(
50
        Request $request,
51
        int $type = HttpKernelInterface::MASTER_REQUEST,
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
        /** @scrutinizer ignore-unused */ int $type = HttpKernelInterface::MASTER_REQUEST,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
        bool $catch = true
0 ignored issues
show
Unused Code introduced by
The parameter $catch is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

52
        /** @scrutinizer ignore-unused */ bool $catch = true

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    ) : Response {
54 8
        self::PairWithRequest($this, $request);
55
56 8
        $dispatcher = Compiler::ObtainDispatcher(
57
            [
58 8
                'cacheFile' => $this->routerCacheFile,
59
            ],
60 8
            ...$this->routerSources
61
        );
62
63 8
        return $dispatcher->handle($request, parse_url($this->ObtainBaseUrl(), PHP_URL_PATH));
64
    }
65
66 16
    protected function ValidateConfig(array $config) : void
67
    {
68
        if (
69
            ! isset(
70 16
                $config[DaftSource::class],
71 16
                $config[DaftSource::class]['cacheFile'],
72 16
                $config[DaftSource::class]['sources']
73
            )
74
        ) {
75 2
            throw new InvalidArgumentException(sprintf('%s config not found!', DaftSource::class));
76 14
        } elseif ( ! is_string($config[DaftSource::class]['cacheFile'])) {
77 2
            throw new InvalidArgumentException(sprintf(
78 2
                '%s config does not specify "%s" correctly.',
79 2
                DaftSource::class,
80 2
                'cacheFile'
81
            ));
82 12
        } elseif ( ! is_array($config[DaftSource::class]['sources'])) {
83 2
            throw new InvalidArgumentException(sprintf(
84 2
                '%s config does not specify "%s" correctly.',
85 2
                DaftSource::class,
86 2
                'sources'
87
            ));
88
        } elseif (
89 10
            file_exists($config[DaftSource::class]['cacheFile']) &&
90 10
            ! $this->FileIsUnderBasePath($config[DaftSource::class]['cacheFile'])
91
        ) {
92 2
            throw new InvalidArgumentException(sprintf(
93 2
                '%s config property cacheFile does not exist under the framework base path.',
94 2
                DaftSource::class
95
            ));
96
        }
97 8
    }
98
}
99