Completed
Push — master ( 701f1f...fb5414 )
by Vítor
02:37
created

RouteCollectionBuilder::processRoutes()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 22
rs 6.9811
cc 7
eloc 13
nc 9
nop 3
1
<?php
2
/**
3
 * ZfDebugModule. Console commands and other utilities for debugging ZF2 apps.
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
6
 * @copyright 2016 Vítor Brandão <[email protected]>
7
 */
8
9
namespace Noiselabs\ZfDebugModule\Util\Routing;
10
11
use Zend\Mvc\Router\RouteInterface;
12
13
/**
14
 * RouteCollectionBuilder is only able to build FlatRouteCollection(s).
15
 */
16
class RouteCollectionBuilder
17
{
18
    /**
19
     * @var RouteInterface
20
     */
21
    private $routes;
22
23
    /**
24
     * RoutesInspector constructor.
25
     *
26
     * @param array $routes
27
     */
28
    public function __construct(array $routes = [])
29
    {
30
        $this->routes = $routes;
0 ignored issues
show
Documentation Bug introduced by
It seems like $routes of type array is incompatible with the declared type object<Zend\Mvc\Router\RouteInterface> of property $routes.

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...
31
    }
32
33
    /**
34
     * @return FlatRouteCollection
35
     */
36
    public function build()
37
    {
38
        return $this->processRoutes(new FlatRouteCollection(), $this->routes);
0 ignored issues
show
Documentation introduced by
$this->routes is of type object<Zend\Mvc\Router\RouteInterface>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
    }
40
41
    /**
42
     * @param FlatRouteCollection $routeCollection
43
     * @param array               $routesConfig
44
     * @param Route               $parentRoute
45
     *
46
     * @return FlatRouteCollection
47
     */
48
    private function processRoutes(FlatRouteCollection $routeCollection, array $routesConfig, Route $parentRoute = null)
49
    {
50
        foreach (array_keys($routesConfig) as $k) {
51
            $childRoutes = null;
52
            if (isset($routesConfig[$k]['child_routes']) && !empty($routesConfig[$k]['child_routes'])) {
53
                $childRoutes = $routesConfig[$k]['child_routes'];
54
                unset($routesConfig[$k]['child_routes']);
55
            }
56
57
            $routesConfig[$k]['name'] = $k;
58
            $route = $this->processRoute($routesConfig[$k], $parentRoute);
59
            if (!isset($routesConfig[$k]['may_terminate']) || false !== $routesConfig[$k]['may_terminate']) {
60
                $routeCollection->addRoute($route);
61
            }
62
63
            if (null !== $childRoutes) {
64
                $this->processRoutes($routeCollection, $childRoutes, $route);
65
            }
66
        }
67
68
        return $routeCollection;
69
    }
70
71
    /**
72
     * @param array      $routeConfig
73
     * @param Route|null $parentRoute
74
     *
75
     * @return Route
76
     */
77
    private function processRoute(array $routeConfig, Route $parentRoute = null)
78
    {
79
        $name = $routeConfig['name'];
80
        $url = isset($routeConfig['options']['route']) ? $routeConfig['options']['route'] : '';
81
        $controller = isset($routeConfig['options']['defaults']['controller'])
82
            ? $routeConfig['options']['defaults']['controller'] : null;
83
        $action = isset($routeConfig['options']['defaults']['action'])
84
            ? $routeConfig['options']['defaults']['action'] : null;
85
86
        if (null !== $parentRoute) {
87
            $name = $parentRoute->getName() . '/' . $name;
88
            $url = $parentRoute->getUrl() . $url;
89
            if (null === $controller) {
90
                $controller = $parentRoute->getController();
91
            }
92
        }
93
94
        return new Route($name, $url, $controller, $action);
95
    }
96
}
97