Passed
Pull Request — master (#74)
by Anatoly
02:08
created

RouteCollection::setHost()   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
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
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
 * RouteCollection
26
 *
27
 * Use the factory to create this class.
28
 */
29
class RouteCollection implements RouteCollectionInterface
30
{
31
32
    /**
33
     * The collection routes
34
     *
35
     * @var RouteInterface[]
36
     */
37
    private $routes;
38
39
    /**
40
     * Constructor of the class
41
     *
42
     * @param RouteInterface ...$routes
43
     */
44 54
    public function __construct(RouteInterface ...$routes)
45
    {
46 54
        $this->routes = $routes;
47 54
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 39
    public function add(RouteInterface ...$routes) : void
53
    {
54 39
        foreach ($routes as $route) {
55 39
            $this->routes[] = $route;
56
        }
57 39
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 17
    public function all() : array
63
    {
64 17
        return $this->routes;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     *
70
     * @since 2.9.0
71
     */
72 1
    public function setHost(string $host) : RouteCollectionInterface
73
    {
74 1
        foreach ($this->routes as $route) {
75 1
            $route->setHost($host);
76
        }
77
78 1
        return $this;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     *
84
     * @since 2.9.0
85
     */
86 2
    public function addPrefix(string $prefix) : RouteCollectionInterface
87
    {
88 2
        foreach ($this->routes as $route) {
89 2
            $route->addPrefix($prefix);
90
        }
91
92 2
        return $this;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     *
98
     * @since 2.9.0
99
     */
100 1
    public function addSuffix(string $suffix) : RouteCollectionInterface
101
    {
102 1
        foreach ($this->routes as $route) {
103 1
            $route->addSuffix($suffix);
104
        }
105
106 1
        return $this;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     *
112
     * @since 2.9.0
113
     */
114 1
    public function addMethod(string ...$methods) : RouteCollectionInterface
115
    {
116 1
        foreach ($this->routes as $route) {
117 1
            $route->addMethod(...$methods);
118
        }
119
120 1
        return $this;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     *
126
     * @since 2.9.0
127
     */
128 1
    public function appendMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface
129
    {
130 1
        foreach ($this->routes as $route) {
131 1
            $route->addMiddleware(...$middlewares);
132
        }
133
134 1
        return $this;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     *
140
     * @since 2.9.0
141
     */
142 1
    public function prependMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface
143
    {
144 1
        foreach ($this->routes as $route) {
145 1
            $route->setMiddlewares(...array_merge($middlewares, $route->getMiddlewares()));
146
        }
147
148 1
        return $this;
149
    }
150
151
    /**
152
     * BC (backward compatibility) for version less than 2.9.0
153
     *
154
     * @see appendMiddleware
155
     *
156
     * @deprecated 2.9.0 Use the `appendMiddleware` method.
157
     */
158 1
    public function addMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface
159
    {
160 1
        $this->appendMiddleware(...$middlewares);
161
162 1
        return $this;
163
    }
164
165
    /**
166
     * BC (backward compatibility) for version less than 2.9.0
167
     *
168
     * @see prependMiddleware
169
     *
170
     * @deprecated 2.9.0 Use the `prependMiddleware` method.
171
     */
172 1
    public function unshiftMiddleware(MiddlewareInterface ...$middlewares) : RouteCollectionInterface
173
    {
174 1
        $this->prependMiddleware(...$middlewares);
175
176 1
        return $this;
177
    }
178
}
179