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

Route   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 91
ccs 33
cts 35
cp 0.9429
rs 10
c 5
b 0
f 0
wmc 15

11 Methods

Rating   Name   Duplication   Size   Complexity  
A requestMatches() 0 11 3
A controller() 0 3 1
A action() 0 3 1
A path() 0 3 1
A methodMatches() 0 3 1
A getPathPattern() 0 5 1
A __construct() 0 6 1
A method() 0 3 1
A run() 0 12 2
A getPathPatternWithoutOptionals() 0 5 1
A pathMatches() 0 6 2
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