Passed
Pull Request — master (#135)
by Rustam
02:12
created

Group::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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 bool $preFlightAdded = false;
28
    private array $disabledMiddlewareDefinitions = [];
29
    private ?MiddlewareDispatcher $dispatcher;
30
31 22
    private function __construct(?string $prefix = null, MiddlewareDispatcher $dispatcher = null)
32
    {
33 22
        $this->dispatcher = $dispatcher;
34 22
        $this->prefix = $prefix;
35 22
    }
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 22
    public static function create(
46
        ?string $prefix = null,
47
        MiddlewareDispatcher $dispatcher = null
48
    ): GroupInterface {
49 22
        return new self($prefix, $dispatcher);
50
    }
51
52 19
    public function routes(...$routes): GroupInterface
53
    {
54 19
        if ($this->middlewareAdded) {
55 1
            throw new RuntimeException('routes() can not be used after prependMiddleware().');
56
        }
57 18
        $new = clone $this;
58 18
        foreach ($routes as $route) {
59 18
            if ($route instanceof Route || $route instanceof self) {
60 17
                if (!$route->hasDispatcher() && $new->hasDispatcher()) {
61 5
                    $route = $route->withDispatcher($new->dispatcher);
62
                }
63 17
                $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 17
        $new->routesAdded = true;
73
74 17
        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 17
    public function hasDispatcher(): bool
92
    {
93 17
        return $this->dispatcher !== null;
94
    }
95
96 7
    public function middleware($middlewareDefinition): GroupInterface
97
    {
98 7
        if ($this->routesAdded) {
99
            throw new RuntimeException('middleware() can not be used after routes().');
100
        }
101 7
        $new = clone $this;
102 7
        array_unshift($new->middlewareDefinitions, $middlewareDefinition);
103 7
        return $new;
104
    }
105
106 3
    public function prependMiddleware($middlewareDefinition): GroupInterface
107
    {
108 3
        $new = clone $this;
109 3
        $new->middlewareDefinitions[] = $middlewareDefinition;
110 3
        $new->middlewareAdded = true;
111 3
        return $new;
112
    }
113
114 3
    public function namePrefix(string $namePrefix): GroupInterface
115
    {
116 3
        $new = clone $this;
117 3
        $new->namePrefix = $namePrefix;
118 3
        return $new;
119
    }
120
121 2
    public function host(string $host): GroupInterface
122
    {
123 2
        $new = clone $this;
124 2
        $new->host = rtrim($host, '/');
125 2
        return $new;
126
    }
127
128
    public function disableMiddleware($middlewareDefinition): GroupInterface
129
    {
130
        $new = clone $this;
131
        $new->disabledMiddlewareDefinitions[] = $middlewareDefinition;
132
        return $new;
133
    }
134
135 1
    public function preFlight(): GroupInterface
136
    {
137 1
        $new = clone $this;
138 1
        $new->preFlightAdded = true;
139 1
        return $new;
140
    }
141
142 14
    public function isPreFlight(): bool
143
    {
144 14
        return $this->preFlightAdded;
145
    }
146
147
    /**
148
     * @return Group[]|Route[]
149
     */
150 16
    public function getItems(): array
151
    {
152 16
        return $this->items;
153
    }
154
155 15
    public function getPrefix(): ?string
156
    {
157 15
        return $this->prefix;
158
    }
159
160 15
    public function getNamePrefix(): ?string
161
    {
162 15
        return $this->namePrefix;
163
    }
164
165 15
    public function getHost(): ?string
166
    {
167 15
        return $this->host;
168
    }
169
170 11
    public function getMiddlewareDefinitions(): array
171
    {
172 11
        foreach ($this->middlewareDefinitions as $index => $definition) {
173 8
            if (in_array($definition, $this->disabledMiddlewareDefinitions, true)) {
174
                unset($this->middlewareDefinitions[$index]);
175
            }
176
        }
177
178 11
        return $this->middlewareDefinitions;
179
    }
180
}
181