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

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