Passed
Pull Request — master (#196)
by Rustam
02:50
created

RouteCollector::addRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use Yiisoft\Router\Provider\RoutesProviderInterface;
8
9
final class RouteCollector implements RouteCollectorInterface
10
{
11
    /**
12
     * @var Group[]|Route[]
13
     */
14
    private array $items = [];
15
16
    /**
17
     * @var RoutesProviderInterface[]
18
     */
19
    private array $providers = [];
20
21
    /**
22
     * @var array[]|callable[]|string[]
23
     */
24
    private array $middlewareDefinitions = [];
25
26 30
    public function addRoute(Route|Group ...$routes): RouteCollectorInterface
27
    {
28 30
        array_push(
29 30
            $this->items,
30 30
            ...array_values($routes)
31 30
        );
32 30
        return $this;
33
    }
34
35 1
    public function addProvider(RoutesProviderInterface ...$provider): RouteCollectorInterface
36
    {
37 1
        array_push(
38 1
            $this->providers,
39 1
            ...array_values($provider)
40 1
        );
41 1
        return $this;
42
    }
43
44 6
    public function middleware(array|callable|string ...$middlewareDefinition): RouteCollectorInterface
45
    {
46 6
        array_push(
47 6
            $this->middlewareDefinitions,
48 6
            ...array_values($middlewareDefinition)
49 6
        );
50 6
        return $this;
51
    }
52
53 4
    public function prependMiddleware(array|callable|string ...$middlewareDefinition): RouteCollectorInterface
54
    {
55 4
        array_unshift(
56 4
            $this->middlewareDefinitions,
57 4
            ...array_values($middlewareDefinition)
58 4
        );
59 4
        return $this;
60
    }
61
62 30
    public function getItems(): array
63
    {
64 30
        foreach ($this->providers as $provider) {
65 1
            array_push(
66 1
                $this->items,
67 1
                ...$provider->getRoutes()
68 1
            );
69
        }
70 30
        return $this->items;
71
    }
72
73 28
    public function getMiddlewareDefinitions(): array
74
    {
75 28
        return $this->middlewareDefinitions;
76
    }
77
}
78