Completed
Push — master ( 1b32c6...be6f90 )
by Mikael
02:20
created

Router::getErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Anax\Route;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Anax\Route\Exception\ConfigurationException;
8
use Anax\Route\Exception\ForbiddenException;
9
use Anax\Route\Exception\InternalErrorException;
10
use Anax\Route\Exception\NotFoundException;
11
12
/**
13
 * A router to hold and match routes.
14
 */
15
class Router implements ContainerInjectableInterface
16
{
17
    use ContainerInjectableTrait;
18
19
20
21
    /**
22
     * @var array  $routes         all the routes.
23
     * @var array  $internalRoutes all internal routes.
24
     * @var Route  $lastRoute       last route that was matched and called.
25
     * @var string $errorMessage    last error message for internal routes.
26
     */
27
    private $routes         = [];
28
    private $internalRoutes = [];
29
    private $lastRoute      = null;
30
    private $errorMessage = null;
31
32
33
34
    /**
35
     * @const DEVELOPMENT Verbose with exceptions.
36
     * @const PRODUCTION  Exceptions turns into 500.
37
     */
38
    const DEVELOPMENT = 0;
39
    const PRODUCTION  = 1;
40
41
42
43
    /**
44
     * @var integer $mode current mode.
45
     */
46
    private $mode = self::DEVELOPMENT;
47
48
49
50
    /**
51
     * Set Router::DEVELOPMENT or Router::PRODUCTION mode.
52
     *
53
     * @param integer $mode which mode to set.
54
     *
55
     * @return self to enable chaining.
56
     */
57 4
    public function setMode($mode) : object
58
    {
59 4
        $this->mode = $mode;
60 4
        return $this;
61
    }
62
63
64
65
    /**
66
     * Add routes from an array where the array looks like this:
67
     * [
68
     *      "mount" => null|string, // Where to mount the routes
69
     *      "routes" => [           // All routes in this array
70
     *          [
71
     *              "info" => "Just say hi.",
72
     *              "method" => null,
73
     *              "path" => "hi",
74
     *              "handler" => function () {
75
     *                  return "Hi.";
76
     *              },
77
     *          ]
78
     *      ]
79
     * ]
80
     *
81
     * @throws ConfigurationException
82
     *
83
     * @param array $routes containing the routes to add.
84
     *
85
     * @return self to enable chaining.
86
     */
87 23
    public function addRoutes(array $routes) : object
88
    {
89 23
        if (!(isset($routes["routes"]) && is_array($routes["routes"]))) {
90 3
            throw new ConfigurationException("No routes found, missing key 'routes' in configuration array.");
91
        }
92
93 20
        foreach ($routes["routes"] as $route) {
94 19
            if ($route["internal"] ?? false) {
95 11
                $this->addInternalRoute(
96 11
                    $route["path"] ?? null,
97 11
                    $route["handler"] ?? null,
98 11
                    $route["info"] ?? null
99
                );
100 11
                continue;
101
            }
102
103 16
            $mount = $this->createMountPath(
104 16
                $routes["mount"] ?? null,
105 16
                $route["mount"] ?? null
106
            );
107
108 16
            $this->addRoute(
109 16
                $route["method"] ?? null,
110 16
                $mount,
111 16
                $route["path"] ?? null,
112 16
                $route["handler"] ?? null,
113 16
                $route["info"] ?? null
114
            );
115
        }
116
117 20
        return $this;
118
    }
119
120
121
122
    /**
123
     * Prepare the mount string from configuration, use $mount1 or $mount2,
124
     * the latter supersedes the first.
125
     *
126
     * @param string $mount1 first suggestion to mount path.
127
     * @param string $mount2 second suggestion to mount path, ovverides
128
     *                       the first.
129
     *
130
     * @return string|null as mount path.
131
     */
132 16
    private function createMountPath(
133
        string $mount1 = null,
134
        string $mount2 = null
135
    ) {
136 16
        $mount = null;
137 16
        if ($mount1 && $mount2) {
0 ignored issues
show
Bug Best Practice introduced by Mikael Roos
The expression $mount1 of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by Mikael Roos
The expression $mount2 of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
138 13
            $mount = rtrim($mount1, "/") . "/" . rtrim($mount2, "/");
139 13
            return $mount;
140
        }
141
142 15
        if ($mount1) {
0 ignored issues
show
Bug Best Practice introduced by Mikael Roos
The expression $mount1 of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
143 13
            $mount = $mount1;
144
        }
145
146 15
        if ($mount2) {
0 ignored issues
show
Bug Best Practice introduced by Mikael Roos
The expression $mount2 of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
147 1
            $mount = $mount2;
148
        }
149
150 15
        trim($mount);
151 15
        rtrim($mount, "/");
152 15
        $mount = empty($mount) ? null : $mount;
153
154 15
        return $mount;
155
    }
156
157
158
159
    /**
160
     * Add a route with a request method, a path rule to match and an action
161
     * as the callback. Adding several path rules (array) results in several
162
     * routes being created.
163
     *
164
     * @param string|array           $method  as request method to support
165
     * @param string                 $mount   prefix to $path
166
     * @param string|array           $path    for this route, array for several
167
     *                                        paths
168
     * @param string|array|callable  $handler for this path, callable or equal
169
     * @param string                 $info    description of the route
170
     *
171
     * @return void.
0 ignored issues
show
Documentation introduced by Mikael Roos
The doc-type void. could not be parsed: Unknown type name "void." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
172
     */
173 107
    public function addRoute(
174
        $method,
175
        $mount = null,
176
        $path = null,
177
        $handler = null,
178
        string $info = null
179
    ) : void {
180 107
        if (!is_array($path)) {
181 106
            $path = [$path];
182
        }
183
184 107
        foreach ($path as $thePath) {
185 107
            $route = new Route();
186 107
            $route->set($method, $mount, $thePath, $handler, $info);
187 107
            $this->routes[] = $route;
188
        }
189 107
    }
190
191
192
193
    /**
194
     * Add an internal route to the router, this route is not exposed to the
195
     * browser and the end user.
196
     *
197
     * @param string                 $path    for this route
198
     * @param string|array|callable  $handler for this path, callable or equal
199
     * @param string                 $info    description of the route
200
     *
201
     * @return void.
0 ignored issues
show
Documentation introduced by Mikael Roos
The doc-type void. could not be parsed: Unknown type name "void." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
202
     */
203 23
    public function addInternalRoute(
204
        string $path = null,
205
        $handler,
206
        string $info = null
207
    ) : void {
208 23
        $route = new Route();
209 23
        $route->set(null, null, $path, $handler, $info);
210 23
        $this->internalRoutes[$path] = $route;
211 23
    }
212
213
214
215
    /**
216
     * Handle the routes and match them towards the request, dispatch them
217
     * when a match is made. Each route handler may throw exceptions that
218
     * may redirect to an internal route for error handling.
219
     * Several routes can match and if the routehandler does not break
220
     * execution flow, the route matching will carry on.
221
     * Only the last routehandler will get its return value returned further.
222
     *
223
     * @param string $path    the path to find a matching handler for.
224
     * @param string $method  the request method to match.
225
     *
226
     * @return mixed content returned from route.
227
     */
228 108
    public function handle($path, $method = null)
229
    {
230
        try {
231 108
            $match = false;
0 ignored issues
show
Unused Code introduced by Mikael Roos
$match is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
232 108
            foreach ($this->routes as $route) {
233 107
                if ($route->match($path, $method)) {
234 105
                    $this->lastRoute = $route;
235 105
                    $match = true;
0 ignored issues
show
Unused Code introduced by Mikael Roos
$match is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
236 105
                    $results = $route->handle($path, $this->di);
237 91
                    if ($results) {
238 100
                        return $results;
239
                    }
240
                }
241
            }
242
243 5
            return $this->handleInternal("404", "No route could be matched by the router.");
244 14
        } catch (ForbiddenException $e) {
245 3
            return $this->handleInternal("403", $e->getMessage());
246 11
        } catch (NotFoundException $e) {
247 2
            return $this->handleInternal("404", $e->getMessage());
248 9
        } catch (InternalErrorException $e) {
249 3
            return $this->handleInternal("500", $e->getMessage());
250 6
        } catch (\Exception $e) {
251 6
            if ($this->mode === Router::DEVELOPMENT) {
252 3
                throw $e;
253
            }
254 3
            return $this->handleInternal("500", $e->getMessage());
255
        }
256
    }
257
258
259
260
    /**
261
     * Handle an internal route, the internal routes are not exposed to the
262
     * end user.
263
     *
264
     * @param string $path    for this route.
265
     * @param string $message with additional details.
266
     *
267
     * @throws \Anax\Route\Exception\NotFoundException
268
     *
269
     * @return mixed from the route handler.
270
     */
271 23
    public function handleInternal(string $path, string $message = null)
272
    {
273 23
        $route = $this->internalRoutes[$path]
274 1
            ?? $this->internalRoutes[null]
275 23
            ?? null;
276
277 23
        if (!$route) {
278 1
            throw new NotFoundException("No internal route to handle: " . $path);
279
        }
280
281 22
        $this->errorMessage = $message;
282 22
        if ($message) {
0 ignored issues
show
Bug Best Practice introduced by Mikael Roos
The expression $message of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
283 14
            $route->setArguments([$message]);
284
        }
285
286 22
        $route->setMatchedPath($path);
287 22
        $this->lastRoute = $route;
288 22
        return $route->handle(null, $this->di);
289
    }
290
291
292
293
    /**
294
     * Add a route having a controller as a handler.
295
     *
296
     * @param string|array    $method  as request method to support
297
     * @param string|array    $mount   for this route.
298
     * @param string|callable $handler a callback handler for the route.
299
     * @param string          $info    description of the route
300
     *
301
     * @return void.
0 ignored issues
show
Documentation introduced by Mikael Roos
The doc-type void. could not be parsed: Unknown type name "void." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
302
     */
303 6
    public function addController($method = null, $mount = null, $handler = null, $info = null)
304
    {
305 6
        $this->addRoute($method, $mount, null, $handler, $info);
0 ignored issues
show
Bug introduced by Mikael Roos
It seems like $method defined by parameter $method on line 303 can also be of type null; however, Anax\Route\Router::addRoute() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by Mikael Roos
It seems like $mount defined by parameter $mount on line 303 can also be of type array; however, Anax\Route\Router::addRoute() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
306 6
    }
307
308
309
310
    /**
311
     * Add a route to the router by its method(s),  path(s) and a callback.
312
     *
313
     * @param string|array    $method  as request method to support
314
     * @param string|array    $path    for this route.
315
     * @param string|callable $handler a callback handler for the route.
316
     * @param string          $info    description of the route
317
     *
318
     * @return void.
0 ignored issues
show
Documentation introduced by Mikael Roos
The doc-type void. could not be parsed: Unknown type name "void." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
319
     */
320 12
    public function any($method = null, $path = null, $handler = null, $info = null)
321
    {
322 12
        $this->addRoute($method, null, $path, $handler, $info);
0 ignored issues
show
Bug introduced by Mikael Roos
It seems like $method defined by parameter $method on line 320 can also be of type null; however, Anax\Route\Router::addRoute() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
323 12
    }
324
325
326
327
    /**
328
     * Add a route to the router by its path(s) and a callback for any
329
     * request method .
330
     *
331
     * @param string|array    $path    for this route.
332
     * @param string|callable $handler a callback handler for the route.
333
     * @param string          $info    description of the route
334
     *
335
     * @return void
336
     */
337 21
    public function add($path = null, $handler = null, $info = null)
338
    {
339 21
        $this->addRoute(null, null, $path, $handler, $info);
340 21
    }
341
342
343
344
    /**
345
    * Add a default route which will be applied for any path and any
346
    * request method.
347
     *
348
     * @param string|callable $handler a callback handler for the route.
349
     * @param string          $info    description of the route
350
     *
351
     * @return void
352
     */
353 48
    public function always($handler, $info = null)
354
    {
355 48
        $this->addRoute(null, null, null, $handler, $info);
356 48
    }
357
358
359
360
    /**
361
     * Add a default route which will be applied for any path, if the choosen
362
     * request method is matching.
363
     *
364
     * @param string|array    $method  as request method to support
365
     * @param string|callable $handler a callback handler for the route.
366
     * @param string          $info    description of the route
367
     *
368
     * @return void
369
     */
370 7
    public function all($method, $handler, $info = null)
371
    {
372 7
        $this->addRoute($method, null, null, $handler, $info);
373 7
    }
374
375
376
377
    /**
378
     * Shortcut to add a GET route for the http request method GET.
379
     *
380
     * @param string|array    $path   for this route.
381
     * @param string|callable $handler a callback handler for the route.
382
     * @param string          $info    description of the route
383
     *
384
     * @return void
385
     */
386 6
    public function get($path, $handler, $info = null)
387
    {
388 6
        $this->addRoute(["GET"], null, $path, $handler, $info);
389 6
    }
390
391
392
393
    /**
394
     * Shortcut to add a POST route for the http request method POST.
395
     *
396
     * @param string|array    $path   for this route.
397
     * @param string|callable $handler a callback handler for the route.
398
     * @param string          $info    description of the route
399
     *
400
     * @return void
401
     */
402 6
    public function post($path, $handler, $info = null)
403
    {
404 6
        $this->addRoute(["POST"], null, $path, $handler, $info);
405 6
    }
406
407
408
409
    /**
410
     * Shortcut to add a PUT route for the http request method PUT.
411
     *
412
     * @param string|array    $path   for this route.
413
     * @param string|callable $handler a callback handler for the route.
414
     * @param string          $info    description of the route
415
     *
416
     * @return void
417
     */
418 6
    public function put($path, $handler, $info = null)
419
    {
420 6
        $this->addRoute(["PUT"], null, $path, $handler, $info);
421 6
    }
422
423
424
425
    /**
426
     * Shortcut to add a PATCH route for the http request method PATCH.
427
     *
428
     * @param string|array    $path   for this route.
429
     * @param string|callable $handler a callback handler for the route.
430
     * @param string          $info    description of the route
431
     *
432
     * @return void
433
     */
434 6
    public function patch($path, $handler, $info = null)
435
    {
436 6
        $this->addRoute(["PATCH"], null, $path, $handler, $info);
437 6
    }
438
439
440
441
    /**
442
     * Shortcut to add a DELETE route for the http request method DELETE.
443
     *
444
     * @param string|array    $path   for this route.
445
     * @param string|callable $handler a callback handler for the route.
446
     * @param string          $info    description of the route
447
     *
448
     * @return void
449
     */
450 6
    public function delete($path, $handler, $info = null)
451
    {
452 6
        $this->addRoute(["DELETE"], null, $path, $handler, $info);
453 6
    }
454
455
456
457
    /**
458
     * Shortcut to add a OPTIONS route for the http request method OPTIONS.
459
     *
460
     * @param string|array    $path   for this route.
461
     * @param string|callable $handler a callback handler for the route.
462
     * @param string          $info    description of the route
463
     *
464
     * @return void
465
     */
466 6
    public function options($path, $handler, $info = null)
467
    {
468 6
        $this->addRoute(["OPTIONS"], null, $path, $handler, $info);
469 6
    }
470
471
472
473
    /**
474
     * Get the route for the last route that was handled.
475
     *
476
     * @return mixed
477
     */
478 4
    public function getLastRoute()
479
    {
480 4
        return $this->lastRoute->getAbsolutePath();
481
    }
482
483
484
485
    /**
486
     * Get the route for the last route that was handled.
487
     *
488
     * @return mixed
489
     */
490 3
    public function getMatchedPath()
491
    {
492 3
        return $this->lastRoute->getMatchedPath();
493
    }
494
495
496
497
    /**
498
     * Get last error message supplied when handling internal routes.
499
     *
500
     * @return string as the error message, if any.
501
     */
502
    public function getErrorMessage() : ?string
503
    {
504
        return $this->errorMessage;
505
    }
506
507
508
509
    /**
510
     * Get all routes.
511
     *
512
     * @return array with all routes.
513
     */
514 79
    public function getAll()
515
    {
516 79
        return $this->routes;
517
    }
518
519
520
521
    /**
522
     * Get all internal routes.
523
     *
524
     * @return array with internal routes.
525
     */
526 5
    public function getInternal()
527
    {
528 5
        return $this->internalRoutes;
529
    }
530
}
531