Passed
Pull Request — main (#11)
by Chema
02:23
created

Route::method()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 76
    public function __construct(
18
        private string $method,
19
        private string $path,
20
        private object|string $controller,
21
        private string $action = '__invoke',
22
    ) {
23 76
    }
24
25
    /**
26
     * @psalm-suppress MixedMethodCall
27
     */
28 76
    public function run(MappingInterfaces $mappingInterfaces): string
29
    {
30 76
        $params = (new RouteParams($this))->asArray();
31
32 76
        if (is_object($this->controller)) {
33 15
            return (string)$this->controller->{$this->action}(...$params);
34
        }
35
36 61
        $creator = new InstanceCreator($mappingInterfaces->getAll());
37 61
        $controller = $creator->createByClassName($this->controller);
38
39 61
        return (string)$controller->{$this->action}(...$params);
40
    }
41
42 76
    public function path(): string
43
    {
44 76
        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 76
    public function controller(): object|string
56
    {
57 76
        return $this->controller;
58
    }
59
60 76
    public function action(): string
61
    {
62 76
        return $this->action;
63
    }
64
65 76
    public function getPathPattern(): string
66
    {
67 76
        $pattern = preg_replace('#({.*})#U', '(.*)', $this->path);
68
69 76
        return '#^/' . $pattern . '$#';
70
    }
71
72 75
    public function requestMatches(): bool
73
    {
74 75
        if (!$this->methodMatches()) {
75 25
            return false;
76
        }
77
78 70
        if (!$this->pathMatches()) {
79
            return false;
80
        }
81
82 70
        return true;
83
    }
84
85 75
    public function methodMatches(): bool
86
    {
87 75
        return Request::fromGlobals()->isMethod($this->method);
88
    }
89
90 70
    private function pathMatches(): bool
91
    {
92 70
        $path = Request::fromGlobals()->path();
93
94 70
        return preg_match($this->getPathPattern(), $path)
95 70
            || preg_match($this->getPathPatternWithoutOptionals(), $path);
96
    }
97
98 2
    private function getPathPatternWithoutOptionals(): string
99
    {
100 2
        $pattern = preg_replace('#/({.*\?})#U', '(/(.*))?', $this->path);
101
102 2
        return '#^/' . $pattern . '$#';
103
    }
104
}
105