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

RoutingConfigurator::redirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 9
rs 10
c 1
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router;
6
7
/**
8
 * @method head(string $path, object|string $controller, string $action = '__invoke')
9
 * @method connect(string $path, object|string $controller, string $action = '__invoke')
10
 * @method get(string $path, object|string $controller, string $action = '__invoke')
11
 * @method post(string $path, object|string $controller, string $action = '__invoke')
12
 * @method put(string $path, object|string $controller, string $action = '__invoke')
13
 * @method patch(string $path, object|string $controller, string $action = '__invoke')
14
 * @method delete(string $path, object|string $controller, string $action = '__invoke')
15
 * @method options(string $path, object|string $controller, string $action = '__invoke')
16
 * @method trace(string $path, object|string $controller, string $action = '__invoke')
17
 * @method any(string $path, object|string $controller, string $action = '__invoke')
18
 */
19
final class RoutingConfigurator
20
{
21
    /** @var list<Route> */
22
    private array $routes = [];
23
24
    /** @var array<string, Redirect> */
25
    private array $redirects = [];
26
27
    /** @var array<class-string, callable|class-string|object> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, callable|class-string|object> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable|class-string|object>.
Loading history...
28
    private array $mappingInterfaces = [];
29
30
    /**
31
     * @psalm-suppress MixedArgument
32
     */
33 65
    public function __call(string $name, array $arguments): void
34
    {
35 65
        if ($name === 'any') {
36 18
            $this->addRoutesForAllMethods($arguments);
37
        } else {
38 47
            $this->addRouteByName($name, $arguments);
39
        }
40
    }
41
42
    /**
43
     * @return list<Route>
44
     */
45 64
    public function routes(): array
46
    {
47 64
        return $this->routes;
48
    }
49
50
    /**
51
     * @return array<string, Redirect>
52
     */
53 62
    public function redirects(): array
54
    {
55 62
        return $this->redirects;
56
    }
57
58
    /**
59
     * @param array<class-string, callable|class-string|object> $array
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, callable|class-string|object> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable|class-string|object>.
Loading history...
60
     */
61 1
    public function setMappingInterfaces(array $array): self
62
    {
63 1
        $this->mappingInterfaces = $array;
64 1
        return $this;
65
    }
66
67
    /**
68
     * @return array<class-string, callable|class-string|object>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, callable|class-string|object> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable|class-string|object>.
Loading history...
69
     */
70 60
    public function getMappingInterfaces(): array
71
    {
72 60
        return $this->mappingInterfaces;
73
    }
74
75 12
    public function redirect(
76
        string $uri,
77
        string $destination,
78
        string $method = 'GET',
79
    ): void {
80 12
        $this->redirects[$destination] = new Redirect(
81 12
            $uri,
82 12
            $destination,
83 12
            $method,
84 12
        );
85
    }
86
87
    /**
88
     * @psalm-suppress MixedArgument
89
     */
90 18
    private function addRoutesForAllMethods(array $arguments): void
91
    {
92 18
        foreach (Request::ALL_METHODS as $methodName) {
93 18
            $this->routes[] = $this->createRoute($methodName, ...$arguments);
94
        }
95
    }
96
97
    /**
98
     * @psalm-suppress MixedArgument
99
     */
100 47
    private function addRouteByName(string $name, array $arguments): void
101
    {
102 47
        $this->routes[] = match ($name) {
103 47
            'head' => $this->createRoute(Request::METHOD_HEAD, ...$arguments),
104 47
            'connect' => $this->createRoute(Request::METHOD_CONNECT, ...$arguments),
105 47
            'get' => $this->createRoute(Request::METHOD_GET, ...$arguments),
106 47
            'post' => $this->createRoute(Request::METHOD_POST, ...$arguments),
107 47
            'delete' => $this->createRoute(Request::METHOD_DELETE, ...$arguments),
108 47
            'options' => $this->createRoute(Request::METHOD_OPTIONS, ...$arguments),
109 47
            'patch' => $this->createRoute(Request::METHOD_PATCH, ...$arguments),
110 47
            'put' => $this->createRoute(Request::METHOD_PUT, ...$arguments),
111 47
            'trace' => $this->createRoute(Request::METHOD_TRACE, ...$arguments),
112 47
            default => throw new UnsupportedHttpMethodException($name),
113 47
        };
114
    }
115
116
    /**
117
     * @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...
118
     */
119 64
    private function createRoute(
120
        string $method,
121
        string $path,
122
        object|string $controller,
123
        string $action = '__invoke',
124
    ): Route {
125 64
        $path = ($path === '/') ? '' : $path;
126
127 64
        return new Route($method, $path, $controller, $action);
128
    }
129
}
130