Passed
Branch master (60a40e)
by Alex
03:08
created

Group   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 28
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 227
ccs 58
cts 58
cp 1
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 8 3
A setRoute() 0 5 1
A all() 0 4 1
A nth() 0 4 1
A forget() 0 6 2
A setMethod() 0 6 2
A setAction() 0 6 2
A setNamespace() 0 6 2
A setPrefix() 0 7 2
A setMetadata() 0 6 2
A setMetadataArray() 0 6 2
A setDefaults() 0 6 2
A setDefault() 0 6 2
A setStrategy() 0 6 2
A setConstraint() 0 6 2
1
<?php
2
3
/**
4
 * Codeburner Framework.
5
 *
6
 * @author Alex Rohleder <[email protected]>
7
 * @copyright 2016 Alex Rohleder
8
 * @license http://opensource.org/licenses/MIT
9
 */
10
11
namespace Codeburner\Router;
12
13
/**
14
 * Group several routes and abstract operations applied to all.
15
 *
16
 * @author Alex Rohleder <[email protected]>
17
 */
18
19
class Group
20
{
21
22
    /**
23
     * All grouped route objects.
24
     *
25
     * @var Route[]
26
     */
27
28
    protected $routes;
29
30
    /**
31
     * Set a new Route or merge an existing group of routes.
32
     *
33
     * @param Group|Route $route
34
     * @return self
35
     */
36
37 28
    public function set($route)
38
    {
39 28
        if ($route instanceof Group) {
40 28
            foreach ($route->all() as $r)
41 28
                $this->routes[] = $r;
42 28
        } else  $this->routes[] = $route;
43 28
        return  $this;
44
    }
45
46
    /**
47
     * A fast way to register a route into the group
48
     *
49
     * @param Route $route
50
     * @return self
51
     */
52
53 47
    public function setRoute(Route $route)
54
    {
55 47
        $this->routes[] = $route;
56 47
        return $this;
57
    }
58
59
    /**
60
     * Return all grouped routes objects.
61
     *
62
     * @return Route[]
63
     */
64
65 28
    public function all()
66
    {
67 28
        return $this->routes;
68
    }
69
70
    /**
71
     * Get a specific route of the group, routes receive a key based on
72
     * the order they are added to the group.
73
     *
74
     * @param int $number
75
     * @return Route
76
     */
77
78 4
    public function nth($number)
79
    {
80 4
        return $this->routes[$number];
81
    }
82
83
    /**
84
     * Forget the registration of all grouped routes on to collector.
85
     * After the forget the route object will still exist but will not
86
     * count for the matcher.
87
     *
88
     * @return self
89
     */
90
91 3
    public function forget()
92
    {
93 3
        foreach ($this->routes as $route)
94 3
            $route->forget();
95 3
        return $this;
96
    }
97
98
    /**
99
     * Set one HTTP method to all grouped routes.
100
     *
101
     * @param string $method The HTTP Method
102
     * @return self
103
     */
104
105 1
    public function setMethod($method)
106
    {
107 1
        foreach ($this->routes as $route)
108 1
            $route->setMethod($method);
109 1
        return $this;
110
    }
111
112
    /**
113
     * Set one action to all grouped routes.
114
     *
115
     * @param string $action
116
     * @return self
117
     */
118
119 3
    public function setAction($action)
120
    {
121 3
        foreach ($this->routes as $route)
122 3
            $route->setAction($action);
123 3
        return $this;
124
    }
125
126
    /**
127
     * Set one namespace to all grouped routes.
128
     *
129
     * @param string $namespace
130
     * @return self
131
     */
132
133 1
    public function setNamespace($namespace)
134
    {
135 1
        foreach ($this->routes as $route)
136 1
            $route->setNamespace($namespace);
137 1
        return $this;
138
    }
139
140
    /**
141
     * Add a prefix to all grouped routes pattern.
142
     *
143
     * @param string $prefix
144
     * @return self
145
     */
146
147 3
    public function setPrefix($prefix)
148
    {
149 3
        $prefix = "/" . ltrim($prefix, "/");
150 3
        foreach ($this->routes as $route)
151 3
            $route->setPattern(rtrim($prefix . $route->getPattern(), "/"));
152 3
        return $this;
153
    }
154
155
    /**
156
     * Set metadata to all grouped routes.
157
     *
158
     * @param string $key
159
     * @param string $value
160
     *
161
     * @return $this
162
     */
163
164 1
    public function setMetadata($key, $value)
165
    {
166 1
        foreach ($this->routes as $route)
167 1
            $route->setMetadata($key, $value);
168 1
        return $this;
169
    }
170
171
    /**
172
     * Set a bunch of metadata to all grouped routes.
173
     *
174
     * @param mixed[] $metadata
175
     * @return $this
176
     */
177
178 1
    public function setMetadataArray(array $metadata)
179
    {
180 1
        foreach ($this->routes as $route)
181 1
            $route->setMetadataArray($metadata);
182 1
        return $this;
183
    }
184
185
    /**
186
     * Set default parameters to all grouped routes.
187
     *
188
     * @param mixed[] $defaults
189
     * @return $this
190
     */
191
192 1
    public function setDefaults(array $defaults)
193
    {
194 1
        foreach ($this->routes as $route)
195 1
            $route->setDefaults($defaults);
196 1
        return $this;
197
    }
198
199
    /**
200
     * Set a default parameter to all grouped routes.
201
     *
202
     * @param string $key
203
     * @param mixed $value
204
     *
205
     * @return $this
206
     */
207
208 1
    public function setDefault($key, $value)
209
    {
210 1
        foreach ($this->routes as $route)
211 1
            $route->setDefault($key, $value);
212 1
        return $this;
213
    }
214
215
    /**
216
     * Set one dispatch strategy to all grouped routes.
217
     *
218
     * @param string|Strategies\StrategyInterface $strategy
219
     * @return self
220
     */
221
222 12
    public function setStrategy($strategy)
223
    {
224 12
        foreach ($this->routes as $route)
225 12
            $route->setStrategy($strategy);
226 12
        return $this;
227
    }
228
229
    /**
230
     * Replace or define a constraint for all dynamic segments named by $name.
231
     *
232
     * @param string $name
233
     * @param string $regex
234
     *
235
     * @return self
236
     */
237
238 1
    public function setConstraint($name, $regex)
239
    {
240 1
        foreach ($this->routes as $route)
241 1
            $route->setConstraint($name, $regex);
242 1
        return $this;
243
    }
244
245
}
246