RouteCollector   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A any() 0 3 1
A group() 0 6 1
A post() 0 3 1
A options() 0 3 1
A patch() 0 3 1
A get() 0 3 1
A head() 0 3 1
A routes() 0 3 1
A put() 0 3 1
A __construct() 0 3 1
A delete() 0 3 1
A add() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\Router;
6
7
final class RouteCollector
8
{
9
    /**
10
     * @var string
11
     */
12
    private string $currentGroupPrefix = '';
13
14
    /**
15
     * @var RouteCollectionInterface
16
     */
17
    private RouteCollectionInterface $routes;
18
19
    /**
20
     * @param RouteCollectionInterface|null $routes
21
     */
22 15
    public function __construct(?RouteCollectionInterface $routes = null)
23
    {
24 15
        $this->routes = $routes ?? new RouteCollection();
25
    }
26
27
    /**
28
     * Gets an instance of the `RouteCollectionInterface` with all routes set.
29
     *
30
     * @return RouteCollectionInterface
31
     */
32 6
    public function routes(): RouteCollectionInterface
33
    {
34 6
        return $this->routes;
35
    }
36
37
    /**
38
     * Creates a route group with a common prefix.
39
     *
40
     * The callback can take a RouteCollector instance as a parameter.
41
     * All routes created in the passed callback will have the given group prefix prepended.
42
     *
43
     * @param string $prefix common path prefix for the route group.
44
     * @param callable $callback callback that will add routes with a common path prefix.
45
     */
46 1
    public function group(string $prefix, callable $callback): void
47
    {
48 1
        $previousGroupPrefix = $this->currentGroupPrefix;
49 1
        $this->currentGroupPrefix = $previousGroupPrefix . $prefix;
50 1
        $callback($this);
51 1
        $this->currentGroupPrefix = $previousGroupPrefix;
52
    }
53
54
    /**
55
     * Adds a route and returns it.
56
     *
57
     * @param string $name route name.
58
     * @param string $pattern path pattern with parameters.
59
     * @param mixed $handler action, controller, callable, closure, etc.
60
     * @param array $methods allowed request methods of the route.
61
     * @return Route
62
     */
63 15
    public function add(string $name, string $pattern, $handler, array $methods): Route
64
    {
65 15
        $pattern = $this->currentGroupPrefix . $pattern;
66 15
        $route = new Route($name, $pattern, $handler, $methods);
67 15
        $this->routes->set($route);
68 15
        return $route;
69
    }
70
71
    /**
72
     * Adds a generic route for any request methods and returns it.
73
     *
74
     * @param string $name route name.
75
     * @param string $pattern path pattern with parameters.
76
     * @param mixed $handler action, controller, callable, closure, etc.
77
     * @return Route
78
     */
79 1
    public function any(string $name, string $pattern, $handler): Route
80
    {
81 1
        return $this->add($name, $pattern, $handler, []);
82
    }
83
84
    /**
85
     * Adds a GET route and returns it.
86
     *
87
     * @param string $name route name.
88
     * @param string $pattern path pattern with parameters.
89
     * @param mixed $handler action, controller, callable, closure, etc.
90
     * @return Route
91
     */
92 4
    public function get(string $name, string $pattern, $handler): Route
93
    {
94 4
        return $this->add($name, $pattern, $handler, ['GET']);
95
    }
96
97
    /**
98
     * Adds a POST route and returns it.
99
     *
100
     * @param string $name route name.
101
     * @param string $pattern path pattern with parameters.
102
     * @param mixed $handler action, controller, callable, closure, etc.
103
     * @return Route
104
     */
105 5
    public function post(string $name, string $pattern, $handler): Route
106
    {
107 5
        return $this->add($name, $pattern, $handler, ['POST']);
108
    }
109
110
    /**
111
     * Adds a PUT route and returns it.
112
     *
113
     * @param string $name route name.
114
     * @param string $pattern path pattern with parameters.
115
     * @param mixed $handler action, controller, callable, closure, etc.
116
     * @return Route
117
     */
118 2
    public function put(string $name, string $pattern, $handler): Route
119
    {
120 2
        return $this->add($name, $pattern, $handler, ['PUT']);
121
    }
122
123
    /**
124
     * Adds a PATCH route and returns it.
125
     *
126
     * @param string $name route name.
127
     * @param string $pattern path pattern with parameters.
128
     * @param mixed $handler action, controller, callable, closure, etc.
129
     * @return Route
130
     */
131 2
    public function patch(string $name, string $pattern, $handler): Route
132
    {
133 2
        return $this->add($name, $pattern, $handler, ['PATCH']);
134
    }
135
136
    /**
137
     * Adds a DELETE route and returns it.
138
     *
139
     * @param string $name route name.
140
     * @param string $pattern path pattern with parameters.
141
     * @param mixed $handler action, controller, callable, closure, etc.
142
     * @return Route
143
     */
144 2
    public function delete(string $name, string $pattern, $handler): Route
145
    {
146 2
        return $this->add($name, $pattern, $handler, ['DELETE']);
147
    }
148
149
    /**
150
     * Adds a HEAD route and returns it.
151
     *
152
     * @param string $name route name.
153
     * @param string $pattern path pattern with parameters.
154
     * @param mixed $handler action, controller, callable, closure, etc.
155
     * @return Route
156
     */
157 2
    public function head(string $name, string $pattern, $handler): Route
158
    {
159 2
        return $this->add($name, $pattern, $handler, ['HEAD']);
160
    }
161
162
    /**
163
     * Adds a OPTIONS route and returns it.
164
     *
165
     * @param string $name route name.
166
     * @param string $pattern path pattern with parameters.
167
     * @param mixed $handler action, controller, callable, closure, etc.
168
     * @return Route
169
     */
170 2
    public function options(string $name, string $pattern, $handler): Route
171
    {
172 2
        return $this->add($name, $pattern, $handler, ['OPTIONS']);
173
    }
174
}
175