Passed
Pull Request — master (#115)
by Rustam
09:26
created

RouteCollector::prependMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
8
9
final class RouteCollector implements RouteCollectorInterface
10
{
11
    /**
12
     * @var Group[]|Route[]
13
     */
14
    private array $items = [];
15
    private array $middlewareDefinitions = [];
16
    private ?MiddlewareDispatcher $dispatcher;
17
18 12
    public function __construct(MiddlewareDispatcher $dispatcher = null)
19
    {
20 12
        $this->dispatcher = $dispatcher;
21 12
    }
22
23
    public function withDispatcher(MiddlewareDispatcher $dispatcher): RouteCollectorInterface
24
    {
25
        $group = clone $this;
26
        $group->dispatcher = $dispatcher;
27
        foreach ($group->items as $index => $item) {
28
            if (!$item->hasDispatcher()) {
29
                $item = $item->withDispatcher($dispatcher);
30
                $group->items[$index] = $item;
31
            }
32
        }
33
34
        return $group;
35
    }
36
37 8
    public function hasDispatcher(): bool
38
    {
39 8
        return $this->dispatcher !== null;
40
    }
41
42 1
    public function addRoute(Route $route): RouteCollectorInterface
43
    {
44 1
        if (!$route->hasDispatcher() && $this->hasDispatcher()) {
45 1
            $route->injectDispatcher($this->dispatcher);
46
        }
47 1
        $this->items[] = $route;
48 1
        return $this;
49
    }
50
51 10
    public function addGroup(GroupInterface $group): RouteCollectorInterface
52
    {
53 10
        if (!$group->hasDispatcher() && $this->hasDispatcher()) {
0 ignored issues
show
Bug introduced by
The method hasDispatcher() does not exist on Yiisoft\Router\GroupInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Yiisoft\Router\GroupInterface. ( Ignorable by Annotation )

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

53
        if (!$group->/** @scrutinizer ignore-call */ hasDispatcher() && $this->hasDispatcher()) {
Loading history...
54 7
            $group = $group->withDispatcher($this->dispatcher);
55
        }
56 10
        $this->items[] = $group;
57 10
        return $this;
58
    }
59
60
    /**
61
     * Appends a handler middleware definition that should be invoked for a matched route.
62
     * First added handler will be executed first.
63
     *
64
     * @param mixed $middlewareDefinition
65
     *
66
     * @return self
67
     */
68 1
    public function middleware($middlewareDefinition): RouteCollectorInterface
69
    {
70 1
        array_unshift($this->middlewareDefinitions, $middlewareDefinition);
71 1
        return $this;
72
    }
73
74
    /**
75
     * Prepends a handler middleware definition that should be invoked for a matched route.
76
     * First added handler will be executed last.
77
     *
78
     * @param mixed $middlewareDefinition
79
     *
80
     * @return self
81
     */
82 1
    public function prependMiddleware($middlewareDefinition): RouteCollectorInterface
83
    {
84 1
        $this->middlewareDefinitions[] = $middlewareDefinition;
85 1
        return $this;
86
    }
87
88
    /**
89
     * @return Group[]|Route[]
90
     */
91 11
    public function getItems(): array
92
    {
93 11
        return $this->items;
94
    }
95
96 10
    public function getMiddlewareDefinitions(): array
97
    {
98 10
        return $this->middlewareDefinitions;
99
    }
100
}
101