Test Failed
Pull Request — master (#32)
by Anatoly
04:53
created

RouteCollectionCommand::addMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
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
 * RouteCollectionCommand
26
 */
27
class RouteCollectionCommand
28
{
29
30
    /**
31
     * Route collection
32
     *
33
     * @var RouteCollectionInterface
34
     */
35
    private $collection;
36
37
    /**
38
     * Constructor of the class
39
     *
40
     * @param RouteCollectionInterface $collection
41
     */
42
    public function __construct(RouteCollectionInterface $collection)
43
    {
44
        $this->collection = $collection;
45
    }
46
47
    /**
48
     * Adds the given prefix to all routes in the collection
49
     *
50
     * @param string $prefix
51
     *
52
     * @return self
53
     */
54
    public function addPrefix(string $prefix) : self
55
    {
56
        foreach ($this->collection as $route) {
57
            $route->addPrefix($prefix);
58
        }
59
60
        return $this;
61
    }
62
63
    /**
64
     * Adds the given suffix to all routes in the collection
65
     *
66
     * @param string $suffix
67
     *
68
     * @return self
69
     */
70
    public function addSuffix(string $suffix) : self
71
    {
72
        foreach ($this->collection as $route) {
73
            $route->addSuffix($suffix);
74
        }
75
76
        return $this;
77
    }
78
79
    /**
80
     * Adds the given method(s) to all routes in the collection
81
     *
82
     * @param string ...$methods
83
     *
84
     * @return self
85
     */
86
    public function addMethod(string ...$methods) : self
87
    {
88
        foreach ($this->collection as $route) {
89
            $route->addMethod(...$methods);
90
        }
91
92
        return $this;
93
    }
94
95
    /**
96
     * Adds the given middleware(s) to all routes in the collection
97
     *
98
     * @param MiddlewareInterface ...$middlewares
99
     *
100
     * @return self
101
     */
102
    public function addMiddleware(MiddlewareInterface ...$middlewares) : self
103
    {
104
        foreach ($this->collection as $route) {
105
            $route->addMiddleware(...$middlewares);
106
        }
107
108
        return $this;
109
    }
110
111
    /**
112
     * Adds the given middleware(s) to the beginning of all routes in the collection
113
     *
114
     * @param MiddlewareInterface ...$middlewares
115
     *
116
     * @return self
117
     */
118
    public function unshiftMiddleware(MiddlewareInterface ...$middlewares) : self
119
    {
120
        foreach ($this->collection as $route) {
121
            $route->setMiddlewares(...array_merge(
122
                $middlewares,
123
                $route->getMiddlewares()
124
            ));
125
        }
126
127
        return $this;
128
    }
129
}
130