Passed
Push — main ( 123b60...fa6044 )
by Chema
01:08 queued 13s
created

RoutingConfigurator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

7 Methods

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