Passed
Pull Request — master (#113)
by Alexander
02:46
created

RouteCollection   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 68.66%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 59
c 3
b 0
f 0
dl 0
loc 161
ccs 46
cts 67
cp 0.6866
rs 9.92
wmc 31

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 getRoutes() 0 4 1
A __construct() 0 3 1
A injectItem() 0 13 4
A buildTree() 0 11 4
C injectGroup() 0 42 14
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 8
    public function __construct(RouteCollectorInterface $collector)
23
    {
24 8
        $this->collector = $collector;
25 8
    }
26
27
    /**
28
     * @return Route[]
29
     */
30 1
    public function getRoutes(): array
31
    {
32 1
        $this->ensureItemsInjected();
33
        return $this->routes;
34
    }
35
36
    /**
37
     * @param string $name
38
     *
39
     * @return Route
40
     */
41 7
    public function getRoute(string $name): Route
42
    {
43 7
        $this->ensureItemsInjected();
44 7
        if (!array_key_exists($name, $this->routes)) {
45
            throw new RouteNotFoundException($name);
46
        }
47
48 7
        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 8
    private function ensureItemsInjected(): void
65
    {
66 8
        if ($this->items === []) {
67 8
            $this->injectItems([$this->collector]);
68
        }
69 7
    }
70
71
    /**
72
     * Build routes array
73
     *
74
     * @param Group[]|Route[]|RouteCollectorInterface[] $items
75
     */
76 8
    private function injectItems(array $items): void
77
    {
78 8
        foreach ($items as $index => $item) {
79 8
            $this->injectItem($item);
80
        }
81 7
    }
82
83
    /**
84
     * Add an item into routes array
85
     *
86
     * @param Group|Route $route
87
     */
88 8
    private function injectItem($route): void
89
    {
90 8
        if ($route instanceof Group) {
91 8
            $this->injectGroup($route, $this->items);
92 7
            return;
93
        }
94
95
        $this->items[] = $route->getName();
96
        $routeName = $route->getName();
97
        if (isset($this->routes[$routeName]) && !$route->isOverride()) {
98
            throw new InvalidArgumentException("A route with name '$routeName' already exists.");
99
        }
100
        $this->routes[$routeName] = $route;
101
    }
102
103
    /**
104
     * Inject a Group instance into route and item arrays.
105
     */
106 8
    private function injectGroup(Group $group, array &$tree, string $prefix = ''): void
107
    {
108 8
        $prefix .= $group->getPrefix();
109 8
        $items = $group->getItems();
110 8
        foreach ($items as $item) {
111 8
            if ($item instanceof Group || $item->hasMiddlewares()) {
112 4
                $groupMiddlewares = $group->getMiddlewareDefinitions();
113 4
                foreach ($groupMiddlewares as $middleware) {
114 4
                    $item = $item->prependMiddleware($middleware);
115
                }
116
            }
117
118 8
            if ($item instanceof Group) {
119 3
                if (empty($item->getPrefix())) {
120
                    $this->injectGroup($item, $tree, $prefix);
121
                    continue;
122
                }
123 3
                $tree[$item->getPrefix()] = [];
124 3
                $this->injectGroup($item, $tree[$item->getPrefix()], $prefix);
125 3
                continue;
126
            }
127
128
            /** @var Route $modifiedItem */
129 8
            $modifiedItem = $item->pattern($prefix . $item->getPattern());
130 8
            if ($group->getHost() !== null && $modifiedItem->getHost() === null) {
131 1
                $modifiedItem = $modifiedItem->host($group->getHost());
132
            }
133 8
            if ($group->getName() !== null && strpos($modifiedItem->getName(), implode(', ', $modifiedItem->getMethods())) === false) {
134 1
                $modifiedItem = $modifiedItem->name($group->getName() . $modifiedItem->getName());
135
            }
136
137 8
            if (empty($tree[$group->getPrefix()])) {
138 8
                $tree[] = $modifiedItem->getName();
139
            } else {
140
                $tree[$group->getPrefix()][] = $modifiedItem->getName();
141
            }
142
143 8
            $routeName = $modifiedItem->getName();
144 8
            if (isset($this->routes[$routeName]) && !$modifiedItem->isOverride()) {
145 1
                throw new InvalidArgumentException("A route with name '$routeName' already exists.");
146
            }
147 8
            $this->routes[$routeName] = $modifiedItem;
148
        }
149 7
    }
150
151
    /**
152
     * Builds route tree from items
153
     *
154
     * @param array $items
155
     * @param bool $routeAsString
156
     *
157
     * @return array
158
     */
159
    private function buildTree(array $items, bool $routeAsString): array
160
    {
161
        $tree = [];
162
        foreach ($items as $key => $item) {
163
            if (is_array($item)) {
164
                $tree[$key] = $this->buildTree($items[$key], $routeAsString);
165
            } else {
166
                $tree[] = $routeAsString ? (string)$this->getRoute($item) : $this->getRoute($item);
167
            }
168
        }
169
        return $tree;
170
    }
171
}
172