Passed
Pull Request — master (#831)
by Maxim
07:40
created

RouteGroup::setNamePrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Router;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Spiral\Core\Container\Autowire;
10
use Spiral\Core\CoreInterface;
11
use Spiral\Core\FactoryInterface;
12
use Spiral\Router\Target\AbstractTarget;
13
14
/**
15
 * RouteGroup provides the ability to configure multiple routes to controller/actions using same presets.
16
 */
17
final class RouteGroup
18
{
19
    private string $prefix = '';
20
    private string $namePrefix = '';
21
22
    /** @var array<non-empty-string, Route> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, Route> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, Route>.
Loading history...
23
    private array $routes = [];
24
25
    /** @var array<class-string<MiddlewareInterface>|MiddlewareInterface|Autowire> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<Middl...wareInterface|Autowire> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<MiddlewareInterface>|MiddlewareInterface|Autowire>.
Loading history...
26
    private array $middleware = [];
27
28
    private Autowire|CoreInterface|string|null $core = null;
29
30 285
    public function __construct(
31
        /** @deprecated since v3.3.0 */
32
        private readonly ?ContainerInterface $container = null,
33
        /** @deprecated since v3.3.0 */
34
        private readonly ?RouterInterface $router = null,
35
        /** @deprecated since v3.3.0 */
36
        private readonly ?UriHandler $handler = null
37
    ) {
38
    }
39
40
    /**
41
     * Check if group has a route with given name
42
     */
43 4
    public function hasRoute(string $name): bool
44
    {
45 4
        return \array_key_exists($name, $this->routes);
46
    }
47
48
    /**
49
     * Prefix added to all the routes.
50
     */
51 267
    public function setPrefix(string $prefix): self
52
    {
53 267
        $this->prefix = $prefix;
54
55 267
        return $this;
56
    }
57
58
    /**
59
     * Route name prefix added to all routes.
60
     */
61 1
    public function setNamePrefix(string $prefix): self
62
    {
63 1
        $this->namePrefix = $prefix;
64
65 1
        return $this;
66
    }
67
68 2
    public function setCore(Autowire|CoreInterface|string $core): self
69
    {
70 2
        $this->core = $core;
71
72 2
        return $this;
73
    }
74
75
    /**
76
     * @param MiddlewareInterface|Autowire|class-string<MiddlewareInterface>|non-empty-string $middleware
0 ignored issues
show
Documentation Bug introduced by
The doc comment MiddlewareInterface|Auto...rface>|non-empty-string at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in MiddlewareInterface|Autowire|class-string<MiddlewareInterface>|non-empty-string.
Loading history...
77
     */
78 271
    public function addMiddleware(MiddlewareInterface|Autowire|string $middleware): self
79
    {
80 271
        $this->middleware[] = $middleware;
81
82 271
        return $this;
83
    }
84
85
    /**
86
     * Push routes to router.
87
     *
88
     * @internal
89
     */
90 282
    public function register(RouterInterface $router, FactoryInterface $factory): void
91
    {
92 282
        foreach ($this->routes as $name => $route) {
93 282
            if ($this->core !== null) {
94 2
                if (!$this->core instanceof CoreInterface) {
95 1
                    $this->core = $factory->make($this->core);
0 ignored issues
show
Bug introduced by
It seems like $this->core can also be of type Spiral\Core\Container\Autowire; however, parameter $alias of Spiral\Core\FactoryInterface::make() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
                    $this->core = $factory->make(/** @scrutinizer ignore-type */ $this->core);
Loading history...
96
                }
97
98 2
                $target = $route->getTarget();
99 2
                if ($target instanceof AbstractTarget) {
100 2
                    $route = $route->withTarget($target->withCore($this->core));
0 ignored issues
show
Bug introduced by
$this->core of type Spiral\Core\Container\Autowire|string is incompatible with the type Spiral\Core\CoreInterface expected by parameter $core of Spiral\Router\Target\AbstractTarget::withCore(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
                    $route = $route->withTarget($target->withCore(/** @scrutinizer ignore-type */ $this->core));
Loading history...
101
                }
102
            }
103
104
            try {
105 282
                $uriHandler = $route->getUriHandler();
106 282
            } catch (\Throwable) {
107 282
                $uriHandler = $factory->make(UriHandler::class);
108
            }
109
110 282
            $router->setRoute(
111
                $name,
112 282
                $route->withUriHandler($uriHandler->withPrefix($this->prefix))->withMiddleware(...$this->middleware)
113
            );
114
        }
115
    }
116
117
    /**
118
     * Add a route to a route group.
119
     */
120 284
    public function addRoute(string $name, Route $route): self
121
    {
122 284
        $this->routes[$this->namePrefix . $name] = $route;
123
124 284
        return $this;
125
    }
126
}
127