Passed
Push — master ( 41ec8f...8375fa )
by SignpostMarv
02:44
created

HttpHandler::__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;
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKernel\HttpKernelInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
class HttpHandler extends Framework
18
{
19
    const ERROR_SOURCE_CONFIG = DaftSource::class . ' config does not specify "%s" correctly.';
20
21
    const ERROR_ROUTER_CACHE_FILE_PATH =
22
        DaftSource::class .
23
        ' config property cacheFile does not exist under the framework base path.';
24
25
    /**
26
    * @var string
27
    */
28
    private $routerCacheFile;
29
30
    /**
31
    * @var array<int, string>
32
    */
33
    private $routerSources;
34
35 16
    public function __construct(string $baseUrl, string $basePath, array $config = [])
36
    {
37 16
        parent::__construct($baseUrl, $basePath, $config);
38
39 8
        $this->routerCacheFile = $config[DaftSource::class]['cacheFile'];
40
41 8
        $this->routerSources = array_values(array_filter(
42 8
            $config[DaftSource::class]['sources'],
43 8
            'is_string'
44
        ));
45 8
    }
46
47 8
    public function handle(Request $request) : Response
48
    {
49 8
        self::PairWithRequest($this, $request);
50
51 8
        $dispatcher = Compiler::ObtainDispatcher(
52
            [
53 8
                'cacheFile' => $this->routerCacheFile,
54
            ],
55 8
            ...$this->routerSources
56
        );
57
58 8
        return $dispatcher->handle($request, parse_url($this->ObtainBaseUrl(), PHP_URL_PATH));
59
    }
60
61 16
    protected function ValidateConfig(array $config) : array
62
    {
63
        if (
64
            ! isset(
65 16
                $config[DaftSource::class],
66 16
                $config[DaftSource::class]['cacheFile'],
67 16
                $config[DaftSource::class]['sources']
68
            )
69
        ) {
70 2
            throw new InvalidArgumentException(sprintf('%s config not found!', DaftSource::class));
71 14
        } elseif ( ! is_string($config[DaftSource::class]['cacheFile'])) {
72 2
            throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'cacheFile'));
73 12
        } elseif ( ! is_array($config[DaftSource::class]['sources'])) {
74 2
            throw new InvalidArgumentException(sprintf(self::ERROR_SOURCE_CONFIG, 'sources'));
75 10
        } elseif ( ! $this->FileIsUnderBasePath($config[DaftSource::class]['cacheFile'], false)) {
76 2
            throw new InvalidArgumentException(self::ERROR_ROUTER_CACHE_FILE_PATH);
77
        }
78
79 8
        return parent::ValidateConfig($config);
80
    }
81
}
82