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

RoutingConfigurator::redirect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 4
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 2
rs 10
c 2
b 0
f 0
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<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...
25
    private array $mappingInterfaces = [];
26
27
    /**
28
     * @psalm-suppress MixedArgument
29
     */
30 53
    public function __call(string $name, array $arguments): void
31
    {
32 53
        if ($name === 'any') {
33 9
            $this->addRoutesForAllMethods($arguments);
34
        } else {
35 44
            $this->addRouteByName($name, $arguments);
36
        }
37
    }
38
39
    /**
40
     * @return list<Route>
41
     */
42 72
    public function routes(): array
43
    {
44 72
        return $this->routes;
45
    }
46
47
    /**
48
     * @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...
49
     */
50 1
    public function setMappingInterfaces(array $array): self
51
    {
52 1
        $this->mappingInterfaces = $array;
53 1
        return $this;
54
    }
55
56
    /**
57
     * @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...
58
     */
59 50
    public function getMappingInterfaces(): array
60
    {
61 50
        return $this->mappingInterfaces;
62
    }
63
64 20
    public function redirect(
65
        string $uri,
66
        string $destination,
67
        int $status = 302,
68
        string $method = null,
69
    ): void {
70 20
        if ($method === null) {
71 10
            $this->addRoutesForAllMethods([$uri, new RedirectController($destination, $status)]);
72
        } else {
73 10
            $this->addRouteByName($method, [$uri, new RedirectController($destination, $status)]);
74
        }
75
    }
76
77
    /**
78
     * @psalm-suppress MixedArgument
79
     */
80 19
    private function addRoutesForAllMethods(array $arguments): void
81
    {
82 19
        foreach (Request::ALL_METHODS as $methodName) {
83 19
            $this->routes[] = $this->createRoute($methodName, ...$arguments);
84
        }
85
    }
86
87
    /**
88
     * @psalm-suppress MixedArgument
89
     */
90 54
    private function addRouteByName(string $name, array $arguments): void
91
    {
92 54
        $this->routes[] = match (strtolower(trim($name))) {
93 54
            'head' => $this->createRoute(Request::METHOD_HEAD, ...$arguments),
94 54
            'connect' => $this->createRoute(Request::METHOD_CONNECT, ...$arguments),
95 54
            'get' => $this->createRoute(Request::METHOD_GET, ...$arguments),
96 54
            'post' => $this->createRoute(Request::METHOD_POST, ...$arguments),
97 54
            'delete' => $this->createRoute(Request::METHOD_DELETE, ...$arguments),
98 54
            'options' => $this->createRoute(Request::METHOD_OPTIONS, ...$arguments),
99 54
            'patch' => $this->createRoute(Request::METHOD_PATCH, ...$arguments),
100 54
            'put' => $this->createRoute(Request::METHOD_PUT, ...$arguments),
101 54
            'trace' => $this->createRoute(Request::METHOD_TRACE, ...$arguments),
102 54
            default => throw new UnsupportedHttpMethodException($name),
103 54
        };
104
    }
105
106
    /**
107
     * @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...
108
     */
109 72
    private function createRoute(
110
        string $method,
111
        string $path,
112
        object|string $controller,
113
        string $action = '__invoke',
114
    ): Route {
115 72
        $path = ($path === '/') ? '' : $path;
116
117 72
        return new Route($method, $path, $controller, $action);
118
    }
119
}
120