Passed
Pull Request — main (#19)
by Chema
02:25
created

Router   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 66
c 1
b 0
f 0
ccs 0
cts 36
cp 0
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 4 2
A __construct() 0 4 1
A parameters() 0 15 4
A requestUrl() 0 6 1
A route() 0 23 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpLightning\Router;
6
7
use function call_user_func_array;
8
use function count;
9
10
final class Router
11
{
12
    public function __construct(
13
        private string $requestMethod,
14
        private string $requestUri,
15
    ) {
16
    }
17
18
    public function get(string $route, callable $callback): void
19
    {
20
        if ($this->requestMethod === 'GET') {
21
            $this->route($route, $callback);
22
        }
23
    }
24
25
    private function route(string $route, callable $callback): void
26
    {
27
        $requestUrl = $this->requestUrl();
28
        $routeParts = explode('/', $route);
29
        $requestUrlParts = explode('/', $requestUrl);
30
        array_shift($routeParts);
31
        array_shift($requestUrlParts);
32
33
        if ($routeParts[0] === '' && count($requestUrlParts) === 0) {
34
            call_user_func_array($callback, []);
35
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
36
        }
37
38
        if (count($routeParts) !== count($requestUrlParts)) {
39
            return;
40
        }
41
42
        $parameters = $this->parameters($routeParts, $requestUrlParts);
43
        if ($parameters === []) {
44
            return;
45
        }
46
        call_user_func_array($callback, $parameters);
47
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
48
    }
49
50
    private function requestUrl(): string
51
    {
52
        $requestUrl = (string)filter_var($this->requestUri, FILTER_SANITIZE_URL);
53
        $requestUrl = rtrim($requestUrl, '/');
54
55
        return (string)strtok($requestUrl, '?');
56
    }
57
58
    /**
59
     * @psalm-suppress MixedAssignment,MixedArgument
60
     */
61
    private function parameters(array $routeParts, array $requestUrlParts): array
62
    {
63
        $parameters = [];
64
        for ($i = 0, $iMax = count($routeParts); $i < $iMax; ++$i) {
65
            $routePart = $routeParts[$i];
66
            if (preg_match('/^[$]/', $routePart)) {
67
                $routePart = ltrim($routePart, '$');
68
                $parameters[] = $requestUrlParts[$i];
69
                $$routePart = $requestUrlParts[$i];
70
            } elseif ($routeParts[$i] !== $requestUrlParts[$i]) {
71
                return [];
72
            }
73
        }
74
75
        return $parameters;
76
    }
77
}
78