Passed
Pull Request — main (#6)
by Chema
02:20
created

Route::path()   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;
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 64
    public function __construct(
17
        private string $method,
18
        private string $path,
19
        private object|string $controller,
20
        private string $action = '__invoke',
21
    ) {
22 64
    }
23
24
    /**
25
     * @psalm-suppress MixedMethodCall
26
     */
27 60
    public function run(RoutingConfigurator $routingConfigurator): string
28
    {
29 60
        $params = (new RouteParams($this))->asArray();
30
31 60
        if (is_object($this->controller)) {
32
            return (string)$this->controller->{$this->action}(...$params);
33
        }
34
35 60
        $creator = new InstanceCreator($routingConfigurator->getMappingInterfaces());
36 60
        $controller = $creator->createByClassName($this->controller);
37
38 60
        return (string)$controller->{$this->action}(...$params);
39
    }
40
41 62
    public function path(): string
42
    {
43 62
        return $this->path;
44
    }
45
46 11
    public function method(): string
47
    {
48 11
        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 60
    public function controller(): object|string
55
    {
56 60
        return $this->controller;
57
    }
58
59 60
    public function action(): string
60
    {
61 60
        return $this->action;
62
    }
63
64 61
    public function getPathPattern(): string
65
    {
66 61
        $pattern = preg_replace('#({.*})#U', '(.*)', $this->path);
67
68 61
        return '#^/' . $pattern . '$#';
69
    }
70
71 61
    public function requestMatches(): bool
72
    {
73 61
        if (!$this->methodMatches()) {
74 18
            return false;
75
        }
76
77 51
        if (!$this->pathMatches()) {
78 1
            return false;
79
        }
80
81 50
        return true;
82
    }
83
84 64
    public function methodMatches(): bool
85
    {
86 64
        return Request::instance()->isMethod($this->method);
87
    }
88
89 11
    public function isRedirected(Redirect $redirect): bool
90
    {
91 11
        return $this->path() === $redirect->destination
92 11
            && $this->method() === $redirect->method;
93
    }
94
95 51
    private function pathMatches(): bool
96
    {
97 51
        $path = Request::instance()->path();
98
99 51
        return preg_match($this->getPathPattern(), $path)
100 51
            || preg_match($this->getPathPatternWithoutOptionals(), $path);
101
    }
102
103 3
    private function getPathPatternWithoutOptionals(): string
104
    {
105 3
        $pattern = preg_replace('#/({.*\?})#U', '(/(.*))?', $this->path);
106
107 3
        return '#^/' . $pattern . '$#';
108
    }
109
}
110