Completed
Push — 3.x ( a7763e...34e508 )
by Paul
8s
created

RouterContainer   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 13

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
c 1
b 0
f 0
lcom 2
cbo 13
dl 0
loc 344
ccs 72
cts 72
cp 1
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A loggerFactory() 0 4 1
A routeFactory() 0 4 1
A mapFactory() 0 4 1
A buildMap() 0 4 1
A setLoggerFactory() 0 4 1
A setRouteFactory() 0 4 1
A setMapFactory() 0 4 1
A setMapBuilder() 0 4 1
A getMatcher() 0 11 2
A getGenerator() 0 7 2
A getLogger() 0 7 2
A getRoute() 0 7 2
A getRuleIterator() 0 13 2
A getMap() 0 8 2
A newRouteHelper() 0 4 1
A newRouteRawHelper() 0 4 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Router;
10
11
use Aura\Router\Rule;
12
use Psr\Log\NullLogger;
13
14
/**
15
 *
16
 * A library-specific container.
17
 *
18
 * @package Aura.Router
19
 *
20
 */
21
class RouterContainer
22
{
23
    /**
24
     *
25
     * Generates paths from routes.
26
     *
27
     * @var Generator
28
     *
29
     */
30
    protected $generator;
31
32
    /**
33
     *
34
     * Logs activity in the Matcher.
35
     *
36
     * @var Psr\Log\LoggerInterface
37
     *
38
     */
39
    protected $logger;
40
41
    /**
42
     *
43
     * A factory to create the logger.
44
     *
45
     * @var callable
46
     *
47
     */
48
    protected $loggerFactory;
49
50
    /**
51
     *
52
     * A route map.
53
     *
54
     * @var Map
55
     *
56
     */
57
    protected $map;
58
59
    /**
60
     *
61
     * A factory to create the map.
62
     *
63
     * @var callable
64
     *
65
     */
66
    protected $mapFactory;
67
68
    /**
69
     *
70
     * The route matcher.
71
     *
72
     * @var Matcher
73
     *
74
     */
75
    protected $matcher;
76
77
    /**
78
     *
79
     * A proto-route for the map.
80
     *
81
     * @var Route
82
     *
83
     */
84
    protected $route;
85
86
    /**
87
     *
88
     * A factory to create the route.
89
     *
90
     * @var callable
91
     *
92
     */
93
    protected $routeFactory;
94
95
    /**
96
     *
97
     * An collection of route-matching rules to iterate through.
98
     *
99
     * @var RuleIterator
100
     *
101
     */
102
    protected $ruleIterator;
103
104
    /**
105
     *
106
     * The basepath to use for matching and generating.
107
     *
108
     * @var string
109
     *
110
     */
111
    protected $basepath;
112
113
    /**
114
     *
115
     * Constructor.
116
     *
117
     * @param string $basepath The basepath to use for matching and generating.
118
     *
119
     */
120 24
    public function __construct($basepath = null)
121
    {
122 24
        $this->basepath = $basepath;
123
124 24
        $this->setLoggerFactory([$this, 'loggerFactory']);
125
126 24
        $this->setRouteFactory([$this, 'routeFactory']);
127
128 24
        $this->setMapFactory([$this, 'mapFactory']);
129
130 24
        $this->setMapBuilder([$this, 'buildMap']);
131 24
    }
132
133
    /**
134
     * Creates a Logger
135
     *
136
     * @return NullLogger
137
     *
138
     * @access protected
139
     */
140 5
    protected function loggerFactory()
141
    {
142 5
        return new NullLogger();
143
    }
144
145
    /**
146
     * Creates a new Route
147
     *
148
     * @return Route
149
     *
150
     * @access protected
151
     */
152 24
    protected function routeFactory()
153
    {
154 24
        return new Route();
155
    }
156
157
    /**
158
     * mapFactory
159
     *
160
     * @return mixed
161
     *
162
     * @access protected
163
     */
164 24
    protected function mapFactory()
165
    {
166 24
        return new Map($this->getRoute());
167
    }
168
169
    /**
170
     * Builds a map
171
     *
172
     * @param Map $map DESCRIPTION
173
     *
174
     * @return mixed
175
     *
176
     * @access protected
177
     */
178 24
    protected function buildMap(Map $map)
0 ignored issues
show
Unused Code introduced by
The parameter $map is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
179
    {
180
        // do nothing
181 24
    }
182
183
    /**
184
     *
185
     * Sets the logger factory.
186
     *
187
     * @param callable $loggerFactory The logger factory.
188
     *
189
     * @return null
190
     *
191
     */
192 24
    public function setLoggerFactory(callable $loggerFactory)
193
    {
194 24
        $this->loggerFactory = $loggerFactory;
195 24
    }
196
197
    /**
198
     *
199
     * Sets the proto-route factory.
200
     *
201
     * @param callable $routeFactory The proto-route factory.
202
     *
203
     * @return null
204
     *
205
     */
206 24
    public function setRouteFactory(callable $routeFactory)
207
    {
208 24
        $this->routeFactory = $routeFactory;
209 24
    }
210
211
    /**
212
     *
213
     * Sets the map factory.
214
     *
215
     * @param callable $mapFactory The map factory.
216
     *
217
     * @return null
218
     *
219
     */
220 24
    public function setMapFactory(callable $mapFactory)
221
    {
222 24
        $this->mapFactory = $mapFactory;
223 24
    }
224
225
    /**
226
     *
227
     * Sets the map builder.
228
     *
229
     * @param callable $mapBuilder The map builder.
230
     *
231
     * @return null
232
     *
233
     */
234 24
    public function setMapBuilder(callable $mapBuilder)
235
    {
236 24
        $this->mapBuilder = $mapBuilder;
0 ignored issues
show
Bug introduced by
The property mapBuilder does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
237 24
    }
238
239
    /**
240
     *
241
     * Gets the shared Map instance. Creates it with the map factory, and runs
242
     * it through the map builder, on first call.
243
     *
244
     * @return Map
245
     *
246
     */
247 24
    public function getMap()
248
    {
249 24
        if (! $this->map) {
250 24
            $this->map = call_user_func($this->mapFactory);
251 24
            call_user_func($this->mapBuilder, $this->map);
252 24
        }
253 24
        return $this->map;
254
    }
255
256
    /**
257
     *
258
     * Gets the shared Matcher instance.
259
     *
260
     * @return Matcher
261
     *
262
     */
263 5
    public function getMatcher()
264
    {
265 5
        if (! $this->matcher) {
266 5
            $this->matcher = new Matcher(
267 5
                $this->getMap(),
268 5
                $this->getLogger(),
269 5
                $this->getRuleIterator()
270 5
            );
271 5
        }
272 5
        return $this->matcher;
273
    }
274
275
    /**
276
     *
277
     * Gets the shared Generator instance.
278
     *
279
     * @return Generator
280
     *
281
     */
282 14
    public function getGenerator()
283
    {
284 14
        if (! $this->generator) {
285 14
            $this->generator = new Generator($this->getMap(), $this->basepath);
286 14
        }
287 14
        return $this->generator;
288
    }
289
290
    /**
291
     *
292
     * Gets the shared Logger instance.
293
     *
294
     * @return Logger
295
     *
296
     */
297 5
    public function getLogger()
298
    {
299 5
        if (! $this->logger) {
300 5
            $this->logger = call_user_func($this->loggerFactory);
301 5
        }
302 5
        return $this->logger;
303
    }
304
305
    /**
306
     *
307
     * Gets the shared proto-route instance.
308
     *
309
     * @return Route
310
     *
311
     */
312 24
    public function getRoute()
313
    {
314 24
        if (! $this->route) {
315 24
            $this->route = call_user_func($this->routeFactory);
316 24
        }
317 24
        return $this->route;
318
    }
319
320
    /**
321
     *
322
     * Gets the rule iterator instance.
323
     *
324
     * @return RuleIterator
325
     *
326
     */
327 5
    public function getRuleIterator()
328
    {
329 5
        if (! $this->ruleIterator) {
330 5
            $this->ruleIterator = new Rule\RuleIterator([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Aura\Router\Rule\Ru...Router\Rule\Accepts())) of type object<Aura\Router\Rule\RuleIterator> is incompatible with the declared type object<Aura\Router\RuleIterator> of property $ruleIterator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
331 5
                new Rule\Secure(),
332 5
                new Rule\Host(),
333 5
                new Rule\Path($this->basepath),
334 5
                new Rule\Allows(),
335 5
                new Rule\Accepts(),
336 5
            ]);
337 5
        }
338 5
        return $this->ruleIterator;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->ruleIterator; of type Aura\Router\Rule\RuleIte...ura\Router\RuleIterator adds the type Aura\Router\Rule\RuleIterator to the return on line 338 which is incompatible with the return type documented by Aura\Router\RouterContainer::getRuleIterator of type Aura\Router\RuleIterator.
Loading history...
339
    }
340
341
    /**
342
     *
343
     * Gets a new route generation helper
344
     *
345
     * @return Helper\Route
346
     *
347
     */
348 1
    public function newRouteHelper()
349
    {
350 1
        return new Helper\Route($this->getGenerator());
351
    }
352
353
    /**
354
     *
355
     * Gets a new raw route generation helper
356
     *
357
     * @return Helper\RouteRaw
358
     *
359
     */
360 1
    public function newRouteRawHelper()
361
    {
362 1
        return new Helper\RouteRaw($this->getGenerator());
363
    }
364
}
365