Routes   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 82
ccs 23
cts 23
cp 1
rs 10
c 1
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 6 2
A getAllRoutes() 0 3 1
A match() 0 8 1
A redirect() 0 10 2
A addRoute() 0 24 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router\Configure;
6
7
use Gacela\Router\Controllers\RedirectController;
8
use Gacela\Router\Entities\Request;
9
use Gacela\Router\Entities\Route;
10
use Gacela\Router\Exceptions\MalformedPathException;
11
use Gacela\Router\Exceptions\UnsupportedHttpMethodException;
12
13
use Gacela\Router\Validators\PathValidator;
14
15
use function in_array;
16
use function is_array;
17
18
/**
19
 * @method head(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null)
20
 * @method connect(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null)
21
 * @method get(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null)
22
 * @method post(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null)
23
 * @method put(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null)
24
 * @method patch(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null)
25
 * @method delete(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null)
26
 * @method options(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null)
27
 * @method trace(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null)
28
 * @method any(string $path, object|string $controller, string $action = '__invoke', ?string $pathPattern = null)
29
 */
30
final class Routes
31
{
32
    /** @var list<Route> */
33
    private array $routes = [];
34
35
    /**
36
     * @psalm-suppress MixedArgument
37
     */
38 86
    public function __call(string $method, array $arguments): void
39
    {
40 86
        if ($method === 'any') {
41 9
            $this->addRoute(Request::ALL_METHODS, ...$arguments);
42
        } else {
43 77
            $this->addRoute($method, ...$arguments);
44
        }
45
    }
46
47
    /**
48
     * @psalm-suppress MixedArgument
49
     *
50
     * @param string[] $methods
51
     * @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...
52
     */
53 8
    public function match(
54
        array $methods,
55
        string $path,
56
        object|string $controller,
57
        string $action = '__invoke',
58
        ?string $pathPattern = null,
59
    ): void {
60 8
        $this->addRoute($methods, $path, $controller, $action, $pathPattern);
61
    }
62
63 15
    public function redirect(
64
        string $uri,
65
        string $destination,
66
        int $status = 302,
67
        string $method = null,
68
    ): void {
69 15
        if ($method === null) {
70 10
            $this->addRoute(Request::ALL_METHODS, $uri, new RedirectController($destination, $status));
71
        } else {
72 5
            $this->addRoute([$method], $uri, new RedirectController($destination, $status));
73
        }
74
    }
75
76
    /**
77
     * @return list<Route>
78
     */
79 104
    public function getAllRoutes(): array
80
    {
81 104
        return $this->routes;
82
    }
83
84
    /**
85
     * @param string[]|string $methods
86
     * @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...
87
     */
88 109
    private function addRoute(
89
        array|string $methods,
90
        string $path,
91
        object|string $controller,
92
        string $action = '__invoke',
93
        ?string $pathPattern = null,
94
    ): void {
95 109
        if (!PathValidator::isValid($path)) {
96 6
            throw MalformedPathException::withPath($path);
97
        }
98
99 103
        if (!is_array($methods)) {
0 ignored issues
show
introduced by
The condition is_array($methods) is always true.
Loading history...
100 71
            $methods = [$methods];
101
        }
102 103
        $methods = array_map(static fn ($method) => strtoupper($method), $methods);
103
104 103
        $path = ($path === '/') ? '' : $path;
105
106 103
        foreach ($methods as $method) {
107 103
            if (!in_array($method, Request::ALL_METHODS, true)) {
108 1
                throw UnsupportedHttpMethodException::withName($method);
109
            }
110
111 102
            $this->routes[] = new Route($method, $path, $controller, $action, $pathPattern);
112
        }
113
    }
114
}
115