RouteCollectionMatcher   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 67
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
D matchRouteCollection() 0 45 10
1
<?php
2
/**
3
 * Dash
4
 *
5
 * @link      http://github.com/DASPRiD/Dash For the canonical source repository
6
 * @copyright 2013-2015 Ben Scholzen 'DASPRiD'
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Dash\RouteCollection;
11
12
use Dash\Exception\UnexpectedValueException;
13
use Dash\MatchResult;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
/**
17
 * Utility methods for working with route collections.
18
 */
19
final class RouteCollectionMatcher
0 ignored issues
show
Coding Style introduced by
RouteCollectionMatcher does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
20
{
21
    /**
22
     * This is a purely static class, so disallow instantiating.
23
     *
24
     * @codeCoverageIgnore
25
     */
26
    private function __construct()
27
    {
28
    }
29
30
    /**
31
     * Matches a request against a route collection.
32
     *
33
     * @param  RouteCollectionInterface $routeCollection
34
     * @param  ServerRequestInterface   $request
35
     * @param  int                      $pathOffset
36
     * @param  array                    $parentParams
37
     * @return MatchResult|null
38
     * @throws UnexpectedValueException
39
     */
40
    public static function matchRouteCollection(
41
        RouteCollectionInterface $routeCollection,
42
        ServerRequestInterface $request,
43
        $pathOffset,
44
        array $parentParams
45
    ) {
46
        $methodFailureResult = null;
47
        $schemeFailureResult = null;
48
49
        foreach ($routeCollection as $name => $route) {
50
            if (null === ($matchResult = $route->match($request, $pathOffset))) {
51
                continue;
52
            }
53
54
            if ($matchResult->isSuccess()) {
55
                return MatchResult::fromChildMatch($matchResult, $parentParams, $name);
56
            }
57
58
            if ($matchResult->isMethodFailure()) {
59
                if (null === $methodFailureResult) {
60
                    $methodFailureResult = $matchResult;
61
                } else {
62
                    $methodFailureResult = MatchResult::mergeMethodFailures($methodFailureResult, $matchResult);
63
                }
64
                continue;
65
            }
66
67
            if ($matchResult->isSchemeFailure()) {
68
                $schemeFailureResult = $schemeFailureResult ?: $matchResult;
69
                continue;
70
            }
71
72
            return $matchResult;
73
        }
74
75
        if (null !== $schemeFailureResult) {
76
            return $schemeFailureResult;
77
        }
78
79
        if (null !== $methodFailureResult) {
80
            return $methodFailureResult;
81
        }
82
83
        return null;
84
    }
85
}
86