Passed
Push — main ( 5b3d5b...c3557b )
by
unknown
54s queued 12s
created

Route::methodMatches()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router\Entities;
6
7
use Gacela\Resolver\InstanceCreator;
8
use Gacela\Router\MappingInterfaces;
9
10
use function is_object;
11
12
final class Route
13
{
14
    /**
15
     * @param object|class-string $controller
0 ignored issues
show
Documentation Bug introduced by
The doc comment object|class-string at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in object|class-string.
Loading history...
16
     */
17 72
    public function __construct(
18
        private string $method,
19
        private string $path,
20
        private object|string $controller,
21
        private string $action = '__invoke',
22
    ) {
23 72
    }
24
25
    /**
26
     * @psalm-suppress MixedMethodCall
27
     */
28 65
    public function run(MappingInterfaces $mappingInterfaces): string
29
    {
30 65
        $params = (new RouteParams($this))->asArray();
31
32 65
        if (is_object($this->controller)) {
33 15
            return (string)$this->controller->{$this->action}(...$params);
34
        }
35
36 50
        $creator = new InstanceCreator($mappingInterfaces->getAll());
37 50
        $controller = $creator->createByClassName($this->controller);
38
39 50
        return (string)$controller->{$this->action}(...$params);
40
    }
41
42 65
    public function path(): string
43
    {
44 65
        return $this->path;
45
    }
46
47
    public function method(): string
48
    {
49
        return $this->method;
50
    }
51
52
    /**
53
     * @return object|class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment object|class-string at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in object|class-string.
Loading history...
54
     */
55 65
    public function controller(): object|string
56
    {
57 65
        return $this->controller;
58
    }
59
60 65
    public function action(): string
61
    {
62 65
        return $this->action;
63
    }
64
65 66
    public function getPathPattern(): string
66
    {
67 66
        $pattern = preg_replace('#({.*})#U', '(.*)', $this->path);
68
69 66
        return '#^/' . $pattern . '$#';
70
    }
71
72 72
    public function requestMatches(): bool
73
    {
74 72
        if (!$this->methodMatches()) {
75 24
            return false;
76
        }
77
78 66
        if (!$this->pathMatches()) {
79 1
            return false;
80
        }
81
82 65
        return true;
83
    }
84
85 72
    public function methodMatches(): bool
86
    {
87 72
        return Request::fromGlobals()->isMethod($this->method);
88
    }
89
90 66
    private function pathMatches(): bool
91
    {
92 66
        $path = Request::fromGlobals()->path();
93
94 66
        return preg_match($this->getPathPattern(), $path)
95 66
            || preg_match($this->getPathPatternWithoutOptionals(), $path);
96
    }
97
98 3
    private function getPathPatternWithoutOptionals(): string
99
    {
100 3
        $pattern = preg_replace('#/({.*\?})#U', '(/(.*))?', $this->path);
101
102 3
        return '#^/' . $pattern . '$#';
103
    }
104
}
105