Passed
Pull Request — master (#134)
by Rustam
02:39
created

RouteCollection::injectGroup()   C

Complexity

Conditions 15
Paths 49

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 15.0057

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 15
eloc 34
c 5
b 0
f 0
nc 49
nop 4
dl 0
loc 55
ccs 33
cts 34
cp 0.9706
crap 15.0057
rs 5.9166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use InvalidArgumentException;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Yiisoft\Http\Method;
10
11
final class RouteCollection implements RouteCollectionInterface
12
{
13
    private RouteCollectorInterface $collector;
14
15
    private array $items = [];
16
17
    /**
18
     * All attached routes as Route instances
19
     *
20
     * @var Route[]
21
     */
22
    private array $routes = [];
23
24 18
    public function __construct(RouteCollectorInterface $collector)
25
    {
26 18
        $this->collector = $collector;
27 18
    }
28
29 7
    public function getRoutes(): array
30
    {
31 7
        $this->ensureItemsInjected();
32 5
        return $this->routes;
33
    }
34
35 13
    public function getRoute(string $name): Route
36
    {
37 13
        $this->ensureItemsInjected();
38 13
        if (!array_key_exists($name, $this->routes)) {
39 1
            throw new RouteNotFoundException($name);
40
        }
41
42 12
        return $this->routes[$name];
43
    }
44
45 1
    public function getRouteTree(bool $routeAsString = true): array
46
    {
47 1
        $this->ensureItemsInjected();
48 1
        return $this->buildTree($this->items, $routeAsString);
49
    }
50
51 18
    private function ensureItemsInjected(): void
52
    {
53 18
        if ($this->items === []) {
54 18
            $this->injectItems($this->collector->getItems());
55
        }
56 16
    }
57
58
    /**
59
     * Build routes array
60
     *
61
     * @param Group[]|Route[] $items
62
     */
63 18
    private function injectItems(array $items): void
64
    {
65 18
        foreach ($items as $index => $item) {
66 18
            foreach ($this->collector->getMiddlewareDefinitions() as $middlewareDefinition) {
67 1
                $item = $item->prependMiddleware($middlewareDefinition);
68
            }
69 18
            $this->injectItem($item);
70
        }
71 16
    }
72
73
    /**
74
     * Add an item into routes array
75
     *
76
     * @param Group|Route $route
77
     */
78 18
    private function injectItem($route): void
79
    {
80 18
        if ($route instanceof Group) {
81 18
            $this->injectGroup($route, $this->items);
82 17
            return;
83
        }
84
85 2
        $this->items[] = $route->getName();
86 2
        $routeName = $route->getName();
87 2
        if (isset($this->routes[$routeName]) && !$route->isOverride()) {
88 1
            throw new InvalidArgumentException("A route with name '$routeName' already exists.");
89
        }
90 1
        $this->routes[$routeName] = $route;
91 1
    }
92
93
    /**
94
     * Inject a Group instance into route and item arrays.
95
     */
96 18
    private function injectGroup(Group $group, array &$tree, string $prefix = '', string $namePrefix = ''): void
97
    {
98 18
        $prefix .= $group->getPrefix();
99 18
        $namePrefix .= $group->getNamePrefix();
100 18
        $items = $group->getItems();
101 18
        $pattern = null;
102 18
        $host = null;
103 18
        foreach ($items as $item) {
104 18
            if ($item instanceof Group || $item->hasMiddlewares()) {
105 14
                $groupMiddlewares = $group->getMiddlewareDefinitions();
106 14
                foreach ($groupMiddlewares as $middleware) {
107 6
                    $item = $item->prependMiddleware($middleware);
108
                }
109
            }
110
111 18
            if ($group->getHost() !== null && $item->getHost() === null) {
112 1
                $item = $item->host($group->getHost());
113
            }
114
115 18
            if ($item instanceof Group) {
116 6
                if ($group->hasCorsMiddlewares()) {
117 2
                    foreach ($group->getCorsMiddlewares() as $corsMiddleware) {
118 2
                        $item = $item->withCors($corsMiddleware);
119
                    }
120
                }
121
                /** @var Group $item */
122 6
                if (empty($item->getPrefix())) {
123 2
                    $this->injectGroup($item, $tree, $prefix, $namePrefix);
124 2
                    continue;
125
                }
126 5
                $tree[$item->getPrefix()] = [];
127 5
                $this->injectGroup($item, $tree[$item->getPrefix()], $prefix, $namePrefix);
128 5
                continue;
129
            }
130
131
            /** @var Route $modifiedItem */
132 18
            $modifiedItem = $item->pattern($prefix . $item->getPattern());
133
134 18
            if (strpos($modifiedItem->getName(), implode(', ', $modifiedItem->getMethods())) === false) {
135 13
                $modifiedItem = $modifiedItem->name($namePrefix . $modifiedItem->getName());
136
            }
137
138 18
            $this->processAutoOptions($group, $host, $pattern, $modifiedItem, $tree);
139
140 18
            if (empty($tree[$group->getPrefix()])) {
141 18
                $tree[] = $modifiedItem->getName();
142
            } else {
143
                $tree[$group->getPrefix()][] = $modifiedItem->getName();
144
            }
145
146 18
            $routeName = $modifiedItem->getName();
147 18
            if (isset($this->routes[$routeName]) && !$modifiedItem->isOverride()) {
148 1
                throw new InvalidArgumentException("A route with name '$routeName' already exists.");
149
            }
150 18
            $this->routes[$routeName] = $modifiedItem;
151
        }
152 17
    }
153
154
    /**
155
     * @param Group $group
156
     * @param string|null $host
157
     * @param string|null $pattern
158
     * @param Route|RouteInterface $modifiedItem
159
     * @param array $tree
160
     */
161 18
    private function processAutoOptions(
162
        Group $group,
163
        ?string &$host,
164
        ?string &$pattern,
165
        Route &$modifiedItem,
166
        array &$tree
167
    ): void {
168
        if (
169 18
            $group->hasCorsMiddlewares()
170 5
            && !(($pattern === $modifiedItem->getPattern() && $host === $modifiedItem->getHost())
171 18
                || in_array(Method::OPTIONS, $modifiedItem->getMethods(), true))
172
        ) {
173 5
            $pattern = $modifiedItem->getPattern();
174 5
            $host = $modifiedItem->getHost();
175
            /** @var Route $optionsRoute */
176 5
            $optionsRoute = Route::options($pattern);
177 5
            if ($host !== null) {
178 1
                $optionsRoute = $optionsRoute->host($host);
179
            }
180 5
            foreach ($group->getCorsMiddlewares() as $middleware) {
181 5
                $optionsRoute = $optionsRoute->middleware($middleware);
182 5
                $modifiedItem = $modifiedItem->prependMiddleware($middleware);
183
            }
184
185 5
            if (empty($tree[$group->getPrefix()])) {
186 5
                $tree[] = $optionsRoute->getName();
187
            } else {
188
                $tree[$group->getPrefix()][] = $optionsRoute->getName();
189
            }
190
191 5
            $this->routes[$optionsRoute->getName()] = $optionsRoute->action(
192 5
                static fn (ResponseFactoryInterface $responseFactory) => $responseFactory->createResponse(204)
193 5
            );
194
        }
195 18
    }
196
197
    /**
198
     * Builds route tree from items
199
     *
200
     * @param array $items
201
     * @param bool $routeAsString
202
     *
203
     * @return array
204
     */
205 1
    private function buildTree(array $items, bool $routeAsString): array
206
    {
207 1
        $tree = [];
208 1
        foreach ($items as $key => $item) {
209 1
            if (is_array($item)) {
210 1
                $tree[$key] = $this->buildTree($items[$key], $routeAsString);
211
            } else {
212 1
                $tree[] = $routeAsString ? (string)$this->getRoute($item) : $this->getRoute($item);
213
            }
214
        }
215 1
        return $tree;
216
    }
217
}
218