Passed
Pull Request — master (#92)
by Dmitriy
01:43
created

RouteCollection   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 63.79%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 50
c 3
b 0
f 0
dl 0
loc 149
ccs 37
cts 58
cp 0.6379
rs 10
wmc 21

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteTree() 0 4 1
A ensureItemsInjected() 0 4 2
A getRoute() 0 8 2
A injectItems() 0 4 2
A __construct() 0 3 1
A buildTree() 0 11 4
B injectGroup() 0 32 6
A injectItem() 0 11 2
A getRoutes() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use InvalidArgumentException;
8
9
final class RouteCollection implements RouteCollectionInterface
10
{
11
    private RouteCollectorInterface $collector;
12
13
    private array $items = [];
14
15
    /**
16
     * All attached routes as Route instances
17
     *
18
     * @var Route[]
19
     */
20
    private array $routes = [];
21
22 4
    public function __construct(RouteCollectorInterface $collector)
23
    {
24 4
        $this->collector = $collector;
25 4
    }
26
27
    /**
28
     * @return Route[]
29
     */
30
    public function getRoutes(): array
31
    {
32
        $this->ensureItemsInjected();
33
        return $this->routes;
34
    }
35
36
    /**
37
     * @param string $name
38
     *
39
     * @return Route
40
     */
41 4
    public function getRoute(string $name): Route
42
    {
43 4
        $this->ensureItemsInjected();
44 4
        if (!array_key_exists($name, $this->routes)) {
45
            throw new RouteNotFoundException($name);
46
        }
47
48 4
        return $this->routes[$name];
49
    }
50
51
    /**
52
     * Returns routes tree array
53
     *
54
     * @param bool $routeAsString
55
     *
56
     * @return array
57
     */
58
    public function getRouteTree(bool $routeAsString = true): array
59
    {
60
        $this->ensureItemsInjected();
61
        return $this->buildTree($this->items, $routeAsString);
62
    }
63
64 4
    private function ensureItemsInjected(): void
65
    {
66 4
        if ($this->items === []) {
67 4
            $this->injectItems([$this->collector]);
68
        }
69 4
    }
70
71
    /**
72
     * Build routes array
73
     *
74
     * @param Group[]|Route[] $items
75
     */
76 4
    private function injectItems(array $items): void
77
    {
78 4
        foreach ($items as $index => $item) {
79 4
            $this->injectItem($item);
80
        }
81 4
    }
82
83
    /**
84
     * Add an item into routes array
85
     *
86
     * @param Group|Route $route
87
     */
88 4
    private function injectItem($route): void
89
    {
90 4
        if ($route instanceof Group) {
91 4
            $this->injectGroup($route, $this->items);
92 4
            return;
93
        }
94
95
        $this->items[] = $route->getName();
96
        $routeName = $route->getName();
97
98
        $this->routes[$routeName] = $route;
99
    }
100
101
    /**
102
     * Inject a Group instance into route and item arrays.
103
     */
104 4
    private function injectGroup(Group $group, array &$tree, string $prefix = ''): void
105
    {
106 4
        $prefix .= $group->getPrefix();
107
        /** @var $items Group[]|Route[] */
108 4
        $items = $group->getItems();
109 4
        foreach ($items as $index => $item) {
110 4
            $groupMiddlewares = $group->getMiddlewares();
111 4
            foreach ($groupMiddlewares as $middleware) {
112 3
                $item = $item->addMiddleware($middleware);
113
            }
114
115 4
            if ($item instanceof Group) {
116 3
                if (empty($item->getPrefix())) {
117
                    $this->injectGroup($item, $tree, $prefix);
118
                    continue;
119
                }
120 3
                $tree[$item->getPrefix()] = [];
121 3
                $this->injectGroup($item, $tree[$item->getPrefix()], $prefix);
122 3
                continue;
123
            }
124
125
            /** @var Route $modifiedItem */
126 4
            $modifiedItem = $item->pattern($prefix . $item->getPattern());
127
128 4
            if (empty($tree[$group->getPrefix()])) {
129 4
                $tree[] = $modifiedItem->getName();
130
            } else {
131
                $tree[$group->getPrefix()][] = $modifiedItem->getName();
132
            }
133
134 4
            $routeName = $modifiedItem->getName();
135 4
            $this->routes[$routeName] = $modifiedItem;
136
        }
137 4
    }
138
139
    /**
140
     * Builds route tree from items
141
     *
142
     * @param array $items
143
     * @param bool $routeAsString
144
     *
145
     * @return array
146
     */
147
    private function buildTree(array $items, bool $routeAsString): array
148
    {
149
        $tree = [];
150
        foreach ($items as $key => $item) {
151
            if (is_array($item)) {
152
                $tree[$key] = $this->buildTree($items[$key], $routeAsString);
153
            } else {
154
                $tree[] = $routeAsString ? (string)$this->getRoute($item) : $this->getRoute($item);
155
            }
156
        }
157
        return $tree;
158
    }
159
}
160