Test Failed
Pull Request — master (#225)
by Alexander
12:36
created

Group::getPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use Yiisoft\Router\Internal\MiddlewareFilter;
8
9
final class Group
10
{
11
    /**
12
     * @var Group[]|RoutableInterface[]|Route[]
13
     */
14
    private array $routes = [];
15
16
    /**
17
     * @var array[]|callable[]|string[]
18
     * @psalm-var list<array|callable|string>
19
     */
20
    private array $middlewares = [];
21
22
    /**
23
     * @var string[]
24
     */
25
    private array $hosts = [];
26
27
    /**
28
     * @psalm-var list<array|callable|string>|null
29
     */
30
    private ?array $enabledMiddlewaresCache = null;
31
32
    /**
33
     * @var array|callable|string|null Middleware definition for CORS requests.
34
     */
35
    private $corsMiddleware = null;
36
37
    /**
38
     * @param array $disabledMiddlewares Excludes middleware from being invoked when action is handled.
39
     * It is useful to avoid invoking one of the parent group middleware for
40
     * a certain route.
41
     */
42
    public function __construct(
43
        private ?string $prefix = null,
44
        private ?string $namePrefix = null,
45 41
        array $routes = [],
46
        array $middlewares = [],
47
        array $hosts = [],
48 41
        private array $disabledMiddlewares = [],
49
        array|callable|string|null $corsMiddleware = null
50
    ) {
51
        $this->setRoutes($routes);
52
        $this->setMiddlewares($middlewares);
53
        $this->setHosts($hosts);
54
        $this->corsMiddleware = $corsMiddleware;
55 41
    }
56
57 41
    /**
58
     * @return Group[]|RoutableInterface[]|Route[]
59
     */
60 29
    public function getRoutes(): array
61
    {
62 29
        return $this->routes;
63 1
    }
64
65
    public function getMiddlewares(): array
66 28
    {
67 28
        return $this->middlewares;
68 28
    }
69
70 28
    public function getHosts(): array
71
    {
72
        return $this->hosts;
73
    }
74
75
    public function getCorsMiddleware(): callable|array|string|null
76
    {
77
        return $this->corsMiddleware;
78
    }
79 8
80
    public function getPrefix(): ?string
81 8
    {
82 8
        return $this->prefix;
83
    }
84 8
85
    public function getNamePrefix(): ?string
86
    {
87
        return $this->namePrefix;
88
    }
89
90
    public function getDisabledMiddlewares(): array
91 15
    {
92
        return $this->disabledMiddlewares;
93 15
    }
94 1
95
    public function setRoutes(array $routes): self
96
    {
97 14
        $this->assertRoutes($routes);
98 14
        $this->routes = $routes;
99 14
        return $this;
100 14
    }
101 14
102
    public function setMiddlewares(array $middlewares): self
103 14
    {
104
        $this->assertMiddlewares($middlewares);
105 14
        $this->middlewares = $middlewares;
106
        $this->enabledMiddlewaresCache = null;
107
        return $this;
108
    }
109
110
    public function setHosts(array $hosts): self
111
    {
112 28
        foreach ($hosts as $host) {
113
            if (!is_string($host)) {
114 28
                throw new \InvalidArgumentException('Invalid $hosts provided, list of string expected.');
115 28
            }
116 28
            $host = rtrim($host, '/');
117 28
118 28
            if ($host !== '' && !in_array($host, $this->hosts, true)) {
119
                $this->hosts[] = $host;
120 28
            }
121 28
        }
122
123 28
        return $this;
124
    }
125
126 4
    public function setCorsMiddleware(callable|array|string|null $corsMiddleware): self
127
    {
128 4
        $this->corsMiddleware = $corsMiddleware;
129 4
        return $this;
130 4
    }
131
132
    public function setPrefix(?string $prefix): self
133 2
    {
134
        $this->prefix = $prefix;
135 2
        return $this;
136
    }
137
138 5
    public function setNamePrefix(?string $namePrefix): self
139
    {
140 5
        $this->namePrefix = $namePrefix;
141
        return $this;
142 5
    }
143 4
144
    public function setDisabledMiddlewares(array $disabledMiddlewares): self
145 4
    {
146 4
        $this->disabledMiddlewares = $disabledMiddlewares;
147
        $this->enabledMiddlewaresCache = null;
148
        return $this;
149
    }
150 5
151
    /**
152
     * @return array[]|callable[]|string[]
153
     * @psalm-return list<array|callable|string>
154
     */
155
    public function getEnabledMiddlewares(): array
156
    {
157
        if ($this->enabledMiddlewaresCache !== null) {
158 6
            return $this->enabledMiddlewaresCache;
159
        }
160 6
161 6
        return $this->enabledMiddlewaresCache = MiddlewareFilter::filter($this->middlewares, $this->disabledMiddlewares);
162 6
    }
163 6
164 6
    /**
165
     * @psalm-assert list<array|callable|string> $middlewares
166 6
     */
167
    private function assertMiddlewares(array $middlewares): void
168 6
    {
169
        /** @var mixed $middleware */
170
        foreach ($middlewares as $middleware) {
171
            if (is_string($middleware) || is_callable($middleware) || is_array($middleware)) {
172
                continue;
173
            }
174
175
            throw new \InvalidArgumentException(
176
                'Invalid $middlewares provided, list of string or array or callable expected.'
177
            );
178
        }
179
    }
180
181
    /**
182
     * @psalm-assert array<Route|Group|RoutableInterface> $routes
183
     */
184
    private function assertRoutes(array $routes): void
185
    {
186
        /** @var Group|RoutableInterface|Route $route */
187
        foreach ($routes as $route) {
188
            if ($route instanceof Route || $route instanceof self || $route instanceof RoutableInterface) {
189 35
                continue;
190
            }
191 35
192 35
            throw new \InvalidArgumentException(
193 35
                'Invalid $routes provided, array of `Route` or `Group` or `RoutableInterface` instance expected.'
194 35
            );
195 35
        }
196 35
    }
197
}
198