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

Group::getNamePrefix()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use InvalidArgumentException;
8
use RuntimeException;
9
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
10
11
use function get_class;
12
use function in_array;
13
use function is_object;
14
15
final class Group implements GroupInterface
16
{
17
    /**
18
     * @var Group[]|Route[]
19
     */
20
    private array $items = [];
21
    private ?string $prefix;
22
    private array $middlewareDefinitions = [];
23
    private ?string $host = null;
24
    private ?string $namePrefix = null;
25
    private bool $routesAdded = false;
26
    private bool $middlewareAdded = false;
27
    private array $disabledMiddlewareDefinitions = [];
28
    private array $corsMiddlewares = [];
29
    private ?MiddlewareDispatcher $dispatcher;
30
31 26
    private function __construct(?string $prefix = null, MiddlewareDispatcher $dispatcher = null)
32
    {
33 26
        $this->dispatcher = $dispatcher;
34 26
        $this->prefix = $prefix;
35 26
    }
36
37
    /**
38
     * Create a new group instance.
39
     *
40
     * @param string|null $prefix URL prefix to prepend to all routes of the group.
41
     * @param MiddlewareDispatcher|null $dispatcher Middleware dispatcher to use for the group.
42
     *
43
     * @return GroupInterface
44
     */
45 26
    public static function create(
46
        ?string $prefix = null,
47
        MiddlewareDispatcher $dispatcher = null
48
    ): GroupInterface {
49 26
        return new self($prefix, $dispatcher);
50
    }
51
52 23
    public function routes(...$routes): GroupInterface
53
    {
54 23
        if ($this->middlewareAdded) {
55 1
            throw new RuntimeException('routes() can not be used after prependMiddleware().');
56
        }
57 22
        $new = clone $this;
58 22
        foreach ($routes as $route) {
59 22
            if ($route instanceof Route || $route instanceof self) {
60 21
                if (!$route->hasDispatcher() && $new->hasDispatcher()) {
61 5
                    $route = $route->withDispatcher($new->dispatcher);
62
                }
63 21
                $new->items[] = $route;
64
            } else {
65 1
                $type = is_object($route) ? get_class($route) : gettype($route);
66 1
                throw new InvalidArgumentException(
67 1
                    sprintf('Route should be either an instance of Route or Group, %s given.', $type)
68
                );
69
            }
70
        }
71
72 21
        $new->routesAdded = true;
73
74 21
        return $new;
75
    }
76
77 2
    public function withDispatcher(MiddlewareDispatcher $dispatcher): GroupInterface
78
    {
79 2
        $group = clone $this;
80 2
        $group->dispatcher = $dispatcher;
81 2
        foreach ($group->items as $index => $item) {
82 2
            if (!$item->hasDispatcher()) {
83 2
                $item = $item->withDispatcher($dispatcher);
84 2
                $group->items[$index] = $item;
85
            }
86
        }
87
88 2
        return $group;
89
    }
90
91 5
    public function withCors($middlewareDefinition): GroupInterface
92
    {
93 5
        $group = clone $this;
94 5
        $group->corsMiddlewares[] = $middlewareDefinition;
95
96 5
        return $group;
97
    }
98
99
    /**
100
     * Returns the middleware definitions used by the group for CORS.
101
     *
102
     * @return array
103
     */
104 5
    public function getCorsMiddlewares(): array
105
    {
106 5
        return $this->corsMiddlewares;
107
    }
108
109
    /**
110
     * @return bool Whether the group has CORS middleware.
111
     */
112 18
    public function hasCorsMiddlewares(): bool
113
    {
114 18
        return $this->corsMiddlewares !== [];
115
    }
116
117 21
    public function hasDispatcher(): bool
118
    {
119 21
        return $this->dispatcher !== null;
120
    }
121
122 7
    public function middleware($middlewareDefinition): GroupInterface
123
    {
124 7
        if ($this->routesAdded) {
125
            throw new RuntimeException('middleware() can not be used after routes().');
126
        }
127 7
        $new = clone $this;
128 7
        array_unshift($new->middlewareDefinitions, $middlewareDefinition);
129 7
        return $new;
130
    }
131
132 3
    public function prependMiddleware($middlewareDefinition): GroupInterface
133
    {
134 3
        $new = clone $this;
135 3
        $new->middlewareDefinitions[] = $middlewareDefinition;
136 3
        $new->middlewareAdded = true;
137 3
        return $new;
138
    }
139
140 3
    public function namePrefix(string $namePrefix): GroupInterface
141
    {
142 3
        $new = clone $this;
143 3
        $new->namePrefix = $namePrefix;
144 3
        return $new;
145
    }
146
147 2
    public function host(string $host): GroupInterface
148
    {
149 2
        $new = clone $this;
150 2
        $new->host = rtrim($host, '/');
151 2
        return $new;
152
    }
153
154
    public function disableMiddleware($middlewareDefinition): GroupInterface
155
    {
156
        $new = clone $this;
157
        $new->disabledMiddlewareDefinitions[] = $middlewareDefinition;
158
        return $new;
159
    }
160
161
    /**
162
     * @return Group[]|Route[]
163
     */
164 20
    public function getItems(): array
165
    {
166 20
        return $this->items;
167
    }
168
169 19
    public function getPrefix(): ?string
170
    {
171 19
        return $this->prefix;
172
    }
173
174 19
    public function getNamePrefix(): ?string
175
    {
176 19
        return $this->namePrefix;
177
    }
178
179 19
    public function getHost(): ?string
180
    {
181 19
        return $this->host;
182
    }
183
184 16
    public function getMiddlewareDefinitions(): array
185
    {
186 16
        foreach ($this->middlewareDefinitions as $index => $definition) {
187 8
            if (in_array($definition, $this->disabledMiddlewareDefinitions, true)) {
188
                unset($this->middlewareDefinitions[$index]);
189
            }
190
        }
191
192 16
        return $this->middlewareDefinitions;
193
    }
194
}
195