Passed
Push — master ( c76078...5e617a )
by Alexander
01:15
created

RouteCollection::injectGroup()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.3554

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 36
ccs 14
cts 22
cp 0.6364
rs 8.6346
cc 7
nc 11
nop 3
crap 9.3554
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
    /**
12
     * @var array
13
     */
14
    private array $items = [];
15
16
    /**
17
     * All attached routes as Route instances
18
     *
19
     * @var Route[]
20
     */
21
    private array $routes = [];
22
23
    /**
24
     * RouteCollection constructor.
25
     * @param RouteCollectorInterface $collector
26
     * @param string $prefix
27
     * @param bool $buildRoutes
28
     */
29 3
    public function __construct(RouteCollectorInterface $collector)
30
    {
31 3
        if ($collector instanceof Group && count($collector->getMiddlewares()) > 0) {
32
            throw new InvalidArgumentException('Collector can\'t have middlewares');
33
        }
34 3
        $this->injectItems($collector->getItems());
35
    }
36
37
    /**
38
     * @return Route[]
39
     */
40
    public function getRoutes(): array
41
    {
42
        return $this->routes;
43
    }
44
45
    /**
46
     * @param string $name
47
     * @return Route
48
     */
49 2
    public function getRoute(string $name): Route
50
    {
51 2
        if (!array_key_exists($name, $this->routes)) {
52
            throw new RouteNotFoundException($name);
53
        }
54
55 2
        return $this->routes[$name];
56
    }
57
58
    /**
59
     * Returns routes tree array
60
     *
61
     * @param bool $routeAsString
62
     * @return array
63
     */
64
    public function getRouteTree(bool $routeAsString = true): array
65
    {
66
        return $this->buildTree($this->items, $routeAsString);
67
    }
68
69
    /**
70
     * Build routes array
71
     *
72
     * @param Route[]|Group[] $items
73
     */
74 3
    private function injectItems(array $items): void
75
    {
76 3
        foreach ($items as $index => $item) {
77 3
            $this->injectItem($item);
78
        }
79
    }
80
81
    /**
82
     * Add an item into routes array
83
     * @param Route|Group $route
84
     */
85 3
    private function injectItem($route): void
86
    {
87 3
        if ($route instanceof Group) {
88 2
            $this->injectGroup($route, $this->items);
89 2
            return;
90
        }
91
92 1
        $this->items[] = $route->getName();
93 1
        $routeName = $route->getName();
94 1
        if (isset($this->routes[$routeName])) {
95 1
            throw new InvalidArgumentException("A route with name '$routeName' already exists.");
96
        }
97 1
        $this->routes[$routeName] = $route;
98
    }
99
100
    /**
101
     * Inject a Group instance into route and item arrays.
102
     */
103 2
    private function injectGroup(Group $group, array &$tree, string $prefix = ''): void
104
    {
105 2
        $prefix .= $group->getPrefix();
106
        /** @var $items Group[]|Route[] */
107 2
        $items = $group->getItems();
108 2
        foreach ($items as $index => $item) {
109 2
            if ($item instanceof Group) {
110
                if (empty($item->getPrefix())) {
111
                    $this->injectGroup($item, $tree, $prefix);
112
                    continue;
113
                }
114
                $tree[$item->getPrefix()] = [];
115
                $this->injectGroup($item, $tree[$item->getPrefix()], $prefix);
116
                continue;
117
            }
118
119 2
            if (empty($tree[$group->getPrefix()])) {
120 2
                $tree[] = $item->getName();
121
            } else {
122
                $tree[$group->getPrefix()][] = $item->getName();
123
            }
124
125
            /** @var Route $modifiedItem */
126 2
            $modifiedItem = $item->pattern($prefix . $item->getPattern());
127
128 2
            $groupMiddlewares = $group->getMiddlewares();
129
130 2
            for (end($groupMiddlewares); key($groupMiddlewares) !== null; prev($groupMiddlewares)) {
131 2
                $modifiedItem = $modifiedItem->addMiddleware(current($groupMiddlewares));
132
            }
133
134 2
            $routeName = $modifiedItem->getName();
135 2
            if (isset($this->routes[$routeName])) {
136
                throw new InvalidArgumentException("A route with name '$routeName' already exists.");
137
            }
138 2
            $this->routes[$routeName] = $modifiedItem;
139
        }
140
    }
141
142
    /**
143
     * Builds route tree from items
144
     *
145
     * @param array $items
146
     * @param bool $routeAsString
147
     * @return array
148
     */
149
    private function buildTree(array $items, bool $routeAsString): array
150
    {
151
        $tree = [];
152
        foreach ($items as $key => $item) {
153
            if (is_array($item)) {
154
                $tree[$key] = $this->buildTree($items[$key], $routeAsString);
155
            } else {
156
                $tree[] = $routeAsString ? (string)$this->getRoute($item) : $this->getRoute($item);
157
            }
158
        }
159
        return $tree;
160
    }
161
}
162