Passed
Pull Request — master (#34)
by Anatoly
02:04
created

RouteCollectionGroupAction::unshiftMiddleware()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Http\Server\MiddlewareInterface;
18
19
/**
20
 * Import functions
21
 */
22
use function array_merge;
23
24
/**
25
 * RouteCollectionGroupAction
26
 */
27
class RouteCollectionGroupAction implements RouteCollectionGroupActionInterface
28
{
29
30
    /**
31
     * Route collection for group activities
32
     *
33
     * @var RouteCollectionInterface
34
     */
35
    private $collection;
36
37
    /**
38
     * Constructor of the class
39
     *
40
     * @param RouteCollectionInterface $collection
41
     */
42 9
    public function __construct(RouteCollectionInterface $collection)
43
    {
44 9
        $this->collection = $collection;
45 9
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 3
    public function addPrefix(string $prefix) : RouteCollectionGroupActionInterface
51
    {
52 3
        foreach ($this->collection->all() as $route) {
53 3
            $route->addPrefix($prefix);
54
        }
55
56 3
        return $this;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 1
    public function addSuffix(string $suffix) : RouteCollectionGroupActionInterface
63
    {
64 1
        foreach ($this->collection->all() as $route) {
65 1
            $route->addSuffix($suffix);
66
        }
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 1
    public function addMethod(string ...$methods) : RouteCollectionGroupActionInterface
75
    {
76 1
        foreach ($this->collection->all() as $route) {
77 1
            $route->addMethod(...$methods);
78
        }
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86 1
    public function addMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionGroupActionInterface
87
    {
88 1
        foreach ($this->collection->all() as $route) {
89 1
            $route->addMiddleware(...$middlewares);
90
        }
91
92 1
        return $this;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98 1
    public function unshiftMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionGroupActionInterface
99
    {
100 1
        foreach ($this->collection->all() as $route) {
101 1
            $route->setMiddlewares(...array_merge(
102 1
                $middlewares,
103 1
                $route->getMiddlewares()
104
            ));
105
        }
106
107 1
        return $this;
108
    }
109
}
110