Test Failed
Pull Request — main (#6)
by Chema
05:22 queued 03:08
created

RoutingConfigurator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 99
rs 10
c 3
b 0
f 0
ccs 30
cts 30
cp 1
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setMappingInterfaces() 0 4 1
A __call() 0 6 2
A getMappingInterfaces() 0 3 1
A routes() 0 3 1
A addRoutesForAllMethods() 0 4 2
A redirect() 0 10 2
A addRouteByName() 0 13 1
A createRoute() 0 9 2
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 52
    public function routes(): array
43
    {
44 52
        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
    public function redirect(
65
        string $uri,
66
        string $destination,
67 9
        int $status = 302,
68
        string $method = null,
69 9
    ): void {
70 9
        if ($method === null) {
71
            $this->addRoutesForAllMethods([$uri, new RedirectController($destination, $status)]);
72
        } else {
73
            $this->addRouteByName($method, [$uri, new RedirectController($destination, $status)]);
74
        }
75
    }
76
77 44
    /**
78
     * @psalm-suppress MixedArgument
79 44
     */
80 44
    private function addRoutesForAllMethods(array $arguments): void
81 44
    {
82 44
        foreach (Request::ALL_METHODS as $methodName) {
83 44
            $this->routes[] = $this->createRoute($methodName, ...$arguments);
84 44
        }
85 44
    }
86 44
87 44
    /**
88 44
     * @psalm-suppress MixedArgument
89 44
     */
90 44
    private function addRouteByName(string $name, array $arguments): void
91
    {
92
        $this->routes[] = match ($name) {
93
            'head' => $this->createRoute(Request::METHOD_HEAD, ...$arguments),
94
            'connect' => $this->createRoute(Request::METHOD_CONNECT, ...$arguments),
95
            'get' => $this->createRoute(Request::METHOD_GET, ...$arguments),
96 52
            'post' => $this->createRoute(Request::METHOD_POST, ...$arguments),
97
            'delete' => $this->createRoute(Request::METHOD_DELETE, ...$arguments),
98
            'options' => $this->createRoute(Request::METHOD_OPTIONS, ...$arguments),
99
            'patch' => $this->createRoute(Request::METHOD_PATCH, ...$arguments),
100
            'put' => $this->createRoute(Request::METHOD_PUT, ...$arguments),
101
            'trace' => $this->createRoute(Request::METHOD_TRACE, ...$arguments),
102 52
            default => throw new UnsupportedHttpMethodException($name),
103
        };
104 52
    }
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
    private function createRoute(
110
        string $method,
111
        string $path,
112
        object|string $controller,
113
        string $action = '__invoke',
114
    ): Route {
115
        $path = ($path === '/') ? '' : $path;
116
117
        return new Route($method, $path, $controller, $action);
118
    }
119
}
120