Passed
Push — master ( 148e08...112cfb )
by Alexander
04:36 queued 02:23
created

RouteCollection::injectGroup()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7.1243

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 22
c 2
b 0
f 0
nc 13
nop 3
dl 0
loc 35
ccs 19
cts 22
cp 0.8636
crap 7.1243
rs 8.6346
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 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 3
    public function getRoute(string $name): Route
42
    {
43 3
        $this->ensureItemsInjected();
44 3
        if (!array_key_exists($name, $this->routes)) {
45
            throw new RouteNotFoundException($name);
46
        }
47
48 3
        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 3
    }
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 3
    }
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 3
            return;
93
        }
94
95
        $this->items[] = $route->getName();
96
        $routeName = $route->getName();
97
        if (isset($this->routes[$routeName])) {
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 4
    private function injectGroup(Group $group, array &$tree, string $prefix = ''): void
107
    {
108 4
        $prefix .= $group->getPrefix();
109
        /** @var $items Group[]|Route[] */
110 4
        $items = $group->getItems();
111 4
        foreach ($items as $index => $item) {
112 4
            $groupMiddlewares = $group->getMiddlewares();
113 4
            foreach ($groupMiddlewares as $middleware) {
114 3
                $item = $item->addMiddleware($middleware);
115
            }
116
117 4
            if ($item instanceof Group) {
118 3
                if (empty($item->getPrefix())) {
119
                    $this->injectGroup($item, $tree, $prefix);
120
                    continue;
121
                }
122 3
                $tree[$item->getPrefix()] = [];
123 3
                $this->injectGroup($item, $tree[$item->getPrefix()], $prefix);
124 3
                continue;
125
            }
126
127
            /** @var Route $modifiedItem */
128 4
            $modifiedItem = $item->pattern($prefix . $item->getPattern());
129
130 4
            if (empty($tree[$group->getPrefix()])) {
131 4
                $tree[] = $modifiedItem->getName();
132
            } else {
133
                $tree[$group->getPrefix()][] = $modifiedItem->getName();
134
            }
135
136 4
            $routeName = $modifiedItem->getName();
137 4
            if (isset($this->routes[$routeName])) {
138 1
                throw new InvalidArgumentException("A route with name '$routeName' already exists.");
139
            }
140 4
            $this->routes[$routeName] = $modifiedItem;
141
        }
142 3
    }
143
144
    /**
145
     * Builds route tree from items
146
     *
147
     * @param array $items
148
     * @param bool $routeAsString
149
     *
150
     * @return array
151
     */
152
    private function buildTree(array $items, bool $routeAsString): array
153
    {
154
        $tree = [];
155
        foreach ($items as $key => $item) {
156
            if (is_array($item)) {
157
                $tree[$key] = $this->buildTree($items[$key], $routeAsString);
158
            } else {
159
                $tree[] = $routeAsString ? (string)$this->getRoute($item) : $this->getRoute($item);
160
            }
161
        }
162
        return $tree;
163
    }
164
}
165