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

RouteCollector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 67
rs 10
ccs 33
cts 33
cp 1
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addProvider() 0 7 1
A getItems() 0 9 2
A getMiddlewareDefinitions() 0 3 1
A prependMiddleware() 0 7 1
A addRoute() 0 7 1
A middleware() 0 7 1
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