Passed
Pull Request — master (#831)
by Maxim
06:08
created

ImportConfigurator::namePrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Router\Loader\Configurator;
6
7
use Psr\Http\Server\MiddlewareInterface;
8
use Spiral\Core\CoreInterface;
9
use Spiral\Router\RouteCollection;
10
11
final class ImportConfigurator
12
{
13 270
    public function __construct(
14
        private readonly RouteCollection $parent,
15
        private readonly RouteCollection $routes
16
    ) {
17
    }
18
19 270
    public function __destruct()
20
    {
21 270
        $this->parent->addCollection($this->routes);
22
    }
23
24
    public function __sleep(): array
25
    {
26
        throw new \BadMethodCallException('Cannot unserialize ' . self::class);
27
    }
28
29
    public function __wakeup()
30
    {
31
        throw new \BadMethodCallException('Cannot unserialize ' . self::class);
32
    }
33
34
    public function defaults(array $defaults): self
35
    {
36
        foreach ($this->routes->all() as $configurator) {
37
            $configurator->defaults($defaults);
38
        }
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param non-empty-string $group
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
45
     */
46 267
    public function group(string $group): self
47
    {
48 267
        foreach ($this->routes->all() as $configurator) {
49 267
            $configurator->group($group);
50
        }
51
52 267
        return $this;
53
    }
54
55
    /**
56
     * @param non-empty-string $prefix
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
57
     */
58 267
    public function prefix(string $prefix): self
59
    {
60 267
        foreach ($this->routes->all() as $configurator) {
61 267
            $configurator->prefix($prefix);
62
        }
63
64 267
        return $this;
65
    }
66
67
    /**
68
     * @param non-empty-string $prefix
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
69
     */
70 267
    public function namePrefix(string $prefix): self
71
    {
72 267
        foreach ($this->routes->all() as $name => $configurator) {
73 267
            $this->routes->add($prefix . $name, $configurator);
74 267
            $this->routes->remove($name);
75
        }
76
77 267
        return $this;
78
    }
79
80
    public function core(CoreInterface $core): self
81
    {
82
        foreach ($this->routes->all() as $configurator) {
83
            $configurator->core($core);
84
        }
85
86
        return $this;
87
    }
88
89
    public function middleware(MiddlewareInterface|string|array $middleware): self
90
    {
91
        foreach ($this->routes->all() as $configurator) {
92
            $configurator->middleware($middleware);
93
        }
94
95
        return $this;
96
    }
97
}
98