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