HttpHandler::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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