Passed
Pull Request — master (#134)
by Alexander
03:43
created

RouteCollection::injectGroup()   C

Complexity

Conditions 15
Paths 81

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 15.0062

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 15
eloc 33
c 6
b 0
f 0
nc 81
nop 4
dl 0
loc 54
ccs 32
cts 33
cp 0.9697
crap 15.0062
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
                foreach ($group->getMiddlewareDefinitions() as $middleware) {
106 6
                    $item = $item->prependMiddleware($middleware);
107
                }
108
            }
109
110 18
            if ($group->getHost() !== null && $item->getHost() === null) {
111 1
                $item = $item->host($group->getHost());
112
            }
113
114 18
            if ($item instanceof Group) {
115 6
                if ($group->hasCorsMiddleware()) {
116 2
                    $item = $item->withCors($group->getCorsMiddleware());
117
                }
118
                /** @var Group $item */
119 6
                if (empty($item->getPrefix())) {
120 2
                    $this->injectGroup($item, $tree, $prefix, $namePrefix);
121 2
                    continue;
122
                }
123 5
                $tree[$item->getPrefix()] = [];
124 5
                $this->injectGroup($item, $tree[$item->getPrefix()], $prefix, $namePrefix);
125 5
                continue;
126
            }
127
128
            /** @var Route $modifiedItem */
129 18
            $modifiedItem = $item->pattern($prefix . $item->getPattern());
130
131 18
            if (strpos($modifiedItem->getName(), implode(', ', $modifiedItem->getMethods())) === false) {
132 13
                $modifiedItem = $modifiedItem->name($namePrefix . $modifiedItem->getName());
133
            }
134
135 18
            if ($group->hasCorsMiddleware()) {
136 5
                $this->processCors($group, $host, $pattern, $modifiedItem, $tree);
137
            }
138
139 18
            if (empty($tree[$group->getPrefix()])) {
140 18
                $tree[] = $modifiedItem->getName();
141
            } else {
142
                $tree[$group->getPrefix()][] = $modifiedItem->getName();
143
            }
144
145 18
            $routeName = $modifiedItem->getName();
146 18
            if (isset($this->routes[$routeName]) && !$modifiedItem->isOverride()) {
147 1
                throw new InvalidArgumentException("A route with name '$routeName' already exists.");
148
            }
149 18
            $this->routes[$routeName] = $modifiedItem;
150
        }
151 17
    }
152
153 5
    private function processCors(Group $group, ?string &$host, ?string &$pattern, Route &$modifiedItem, array &$tree): void
154
    {
155 5
        $middleware = $group->getCorsMiddleware();
156 5
        $isNotDuplicate = !in_array(Method::OPTIONS, $modifiedItem->getMethods(), true)
157 5
            && ($pattern !== $modifiedItem->getPattern() || $host !== $modifiedItem->getHost());
158
159 5
        $pattern = $modifiedItem->getPattern();
160 5
        $host = $modifiedItem->getHost();
161
        /** @var Route $optionsRoute */
162 5
        $optionsRoute = Route::options($pattern);
163 5
        if ($host !== null) {
164 1
            $optionsRoute = $optionsRoute->host($host);
165
        }
166 5
        if ($isNotDuplicate) {
167 5
            $optionsRoute = $optionsRoute->middleware($middleware);
168 5
            if (empty($tree[$group->getPrefix()])) {
169 5
                $tree[] = $optionsRoute->getName();
170
            } else {
171
                $tree[$group->getPrefix()][] = $optionsRoute->getName();
172
            }
173 5
            $this->routes[$optionsRoute->getName()] = $optionsRoute->action(
174 5
                static fn (ResponseFactoryInterface $responseFactory) => $responseFactory->createResponse(204)
175 5
            );
176
        }
177 5
        $modifiedItem = $modifiedItem->prependMiddleware($middleware);
178 5
    }
179
180
    /**
181
     * Builds route tree from items
182
     *
183
     * @param array $items
184
     * @param bool $routeAsString
185
     *
186
     * @return array
187
     */
188 1
    private function buildTree(array $items, bool $routeAsString): array
189
    {
190 1
        $tree = [];
191 1
        foreach ($items as $key => $item) {
192 1
            if (is_array($item)) {
193 1
                $tree[$key] = $this->buildTree($items[$key], $routeAsString);
194
            } else {
195 1
                $tree[] = $routeAsString ? (string)$this->getRoute($item) : $this->getRoute($item);
196
            }
197
        }
198 1
        return $tree;
199
    }
200
}
201