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

RouteCollectorGroupAction::addSuffix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
 * RouteCollectorGroupAction
26
 */
27
class RouteCollectorGroupAction
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 7
    public function __construct(RouteCollectionInterface $collection)
43
    {
44 7
        $this->collection = $collection;
45 7
    }
46
47
    /**
48
     * Adds the given path prefix to all routes in the collection
49
     *
50
     * @param string $prefix
51
     *
52
     * @return self
53
     */
54 2
    public function addPrefix(string $prefix) : self
55
    {
56 2
        foreach ($this->collection->all() as $route) {
57 2
            $route->addPrefix($prefix);
58
        }
59
60 2
        return $this;
61
    }
62
63
    /**
64
     * Adds the given path suffix to all routes in the collection
65
     *
66
     * @param string $suffix
67
     *
68
     * @return self
69
     */
70 1
    public function addSuffix(string $suffix) : self
71
    {
72 1
        foreach ($this->collection->all() as $route) {
73 1
            $route->addSuffix($suffix);
74
        }
75
76 1
        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 1
    public function addMethod(string ...$methods) : self
87
    {
88 1
        foreach ($this->collection->all() as $route) {
89 1
            $route->addMethod(...$methods);
90
        }
91
92 1
        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 1
    public function addMiddleware(MiddlewareInterface ...$middlewares) : self
103
    {
104 1
        foreach ($this->collection->all() as $route) {
105 1
            $route->addMiddleware(...$middlewares);
106
        }
107
108 1
        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 1
    public function unshiftMiddleware(MiddlewareInterface ...$middlewares) : self
119
    {
120 1
        foreach ($this->collection->all() as $route) {
121 1
            $route->setMiddlewares(...array_merge(
122 1
                $middlewares,
123 1
                $route->getMiddlewares()
124
            ));
125
        }
126
127 1
        return $this;
128
    }
129
}
130