Test Failed
Pull Request — master (#63)
by Anatoly
10:52
created

RouteCollectorGroupAction::setHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 3
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
     * Sets the given host to all routes in the collection
49
     *
50
     * @param string $host
51
     *
52
     * @return self
53
     *
54 2
     * @since 2.6.0
55
     */
56 2
    public function setHost(string $host) : self
57 2
    {
58
        foreach ($this->collection->all() as $route) {
59
            $route->setHost($host);
60 2
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * Adds the given path prefix to all routes in the collection
67
     *
68
     * @param string $prefix
69
     *
70 1
     * @return self
71
     */
72 1
    public function addPrefix(string $prefix) : self
73 1
    {
74
        foreach ($this->collection->all() as $route) {
75
            $route->addPrefix($prefix);
76 1
        }
77
78
        return $this;
79
    }
80
81
    /**
82
     * Adds the given path suffix to all routes in the collection
83
     *
84
     * @param string $suffix
85
     *
86 1
     * @return self
87
     */
88 1
    public function addSuffix(string $suffix) : self
89 1
    {
90
        foreach ($this->collection->all() as $route) {
91
            $route->addSuffix($suffix);
92 1
        }
93
94
        return $this;
95
    }
96
97
    /**
98
     * Adds the given method(s) to all routes in the collection
99
     *
100
     * @param string ...$methods
101
     *
102 1
     * @return self
103
     */
104 1
    public function addMethod(string ...$methods) : self
105 1
    {
106
        foreach ($this->collection->all() as $route) {
107
            $route->addMethod(...$methods);
108 1
        }
109
110
        return $this;
111
    }
112
113
    /**
114
     * Adds the given middleware(s) to all routes in the collection
115
     *
116
     * @param MiddlewareInterface ...$middlewares
117
     *
118 1
     * @return self
119
     */
120 1
    public function addMiddleware(MiddlewareInterface ...$middlewares) : self
121 1
    {
122 1
        foreach ($this->collection->all() as $route) {
123 1
            $route->addMiddleware(...$middlewares);
124
        }
125
126
        return $this;
127 1
    }
128
129
    /**
130
     * Adds the given middleware(s) to the beginning of all routes in the collection
131
     *
132
     * @param MiddlewareInterface ...$middlewares
133
     *
134
     * @return self
135
     */
136
    public function unshiftMiddleware(MiddlewareInterface ...$middlewares) : self
137
    {
138
        foreach ($this->collection->all() as $route) {
139
            $route->setMiddlewares(...array_merge(
140
                $middlewares,
141
                $route->getMiddlewares()
142
            ));
143
        }
144
145
        return $this;
146
    }
147
}
148