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

Group::middleware()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
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 $corsMiddleware = null;
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->corsMiddleware = $middlewareDefinition;
95
96 5
        return $group;
97
    }
98
99
    /**
100
     * @return mixed Middleware definition for CORS requests.
101
     */
102 5
    public function getCorsMiddleware()
103
    {
104 5
        return $this->corsMiddleware;
105
    }
106
107
    /**
108
     * @return bool Middleware definition for CORS requests.
109
     */
110 18
    public function hasCorsMiddleware(): bool
111
    {
112 18
        return $this->corsMiddleware !== null;
113
    }
114
115 21
    public function hasDispatcher(): bool
116
    {
117 21
        return $this->dispatcher !== null;
118
    }
119
120 7
    public function middleware($middlewareDefinition): GroupInterface
121
    {
122 7
        if ($this->routesAdded) {
123
            throw new RuntimeException('middleware() can not be used after routes().');
124
        }
125 7
        $new = clone $this;
126 7
        array_unshift($new->middlewareDefinitions, $middlewareDefinition);
127 7
        return $new;
128
    }
129
130 3
    public function prependMiddleware($middlewareDefinition): GroupInterface
131
    {
132 3
        $new = clone $this;
133 3
        $new->middlewareDefinitions[] = $middlewareDefinition;
134 3
        $new->middlewareAdded = true;
135 3
        return $new;
136
    }
137
138 3
    public function namePrefix(string $namePrefix): GroupInterface
139
    {
140 3
        $new = clone $this;
141 3
        $new->namePrefix = $namePrefix;
142 3
        return $new;
143
    }
144
145 2
    public function host(string $host): GroupInterface
146
    {
147 2
        $new = clone $this;
148 2
        $new->host = rtrim($host, '/');
149 2
        return $new;
150
    }
151
152
    public function disableMiddleware($middlewareDefinition): GroupInterface
153
    {
154
        $new = clone $this;
155
        $new->disabledMiddlewareDefinitions[] = $middlewareDefinition;
156
        return $new;
157
    }
158
159
    /**
160
     * @return Group[]|Route[]
161
     */
162 20
    public function getItems(): array
163
    {
164 20
        return $this->items;
165
    }
166
167 19
    public function getPrefix(): ?string
168
    {
169 19
        return $this->prefix;
170
    }
171
172 19
    public function getNamePrefix(): ?string
173
    {
174 19
        return $this->namePrefix;
175
    }
176
177 19
    public function getHost(): ?string
178
    {
179 19
        return $this->host;
180
    }
181
182 16
    public function getMiddlewareDefinitions(): array
183
    {
184 16
        foreach ($this->middlewareDefinitions as $index => $definition) {
185 8
            if (in_array($definition, $this->disabledMiddlewareDefinitions, true)) {
186
                unset($this->middlewareDefinitions[$index]);
187
            }
188
        }
189
190 16
        return $this->middlewareDefinitions;
191
    }
192
}
193