Passed
Push — master ( 07b020...fc35ef )
by Anatoly
04:14 queued 02:01
created

RouterBuilder::setPatterns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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\Container\ContainerInterface;
18
use Psr\Http\Server\MiddlewareInterface;
19
use Psr\SimpleCache\CacheInterface;
20
21
/**
22
 * RouterBuilder
23
 *
24
 * @since 2.9.0
25
 */
26
final class RouterBuilder
27
{
28
29
    /**
30
     * @var ContainerInterface|null
31
     */
32
    private $container = null;
33
34
    /**
35
     * @var CacheInterface|null
36
     */
37
    private $cache = null;
38
39
    /**
40
     * @var string|null
41
     */
42
    private $cacheKey = null;
43
44
    /**
45
     * @var array<string, string>|null
46
     */
47
    private $patterns = null;
48
49
    /**
50
     * @var array<string, string[]>|null
51
     */
52
    private $hosts = null;
53
54
    /**
55
     * @var MiddlewareInterface[]|null
56
     */
57
    private $middlewares = null;
58
59
    /**
60
     * @var Loader\ConfigLoader|null
61
     */
62
    private $configLoader = null;
63
64
    /**
65
     * @var Loader\DescriptorLoader|null
66
     */
67
    private $descriptorLoader = null;
68
69
    /**
70
     * Sets the given container to the builder
71
     *
72
     * @param ContainerInterface|null $container
73
     *
74
     * @return self
75
     */
76 1
    public function setContainer(?ContainerInterface $container) : self
77
    {
78 1
        $this->container = $container;
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * Sets the given cache to the builder
85
     *
86
     * @param CacheInterface|null $cache
87
     *
88
     * @return self
89
     */
90 1
    public function setCache(?CacheInterface $cache) : self
91
    {
92 1
        $this->cache = $cache;
93
94 1
        return $this;
95
    }
96
97
    /**
98
     * Sets the given cache key to the builder
99
     *
100
     * @param string|null $cacheKey
101
     *
102
     * @return self
103
     *
104
     * @since 2.10.0
105
     */
106 1
    public function setCacheKey(?string $cacheKey) : self
107
    {
108 1
        $this->cacheKey = $cacheKey;
109
110 1
        return $this;
111
    }
112
113
    /**
114
     * Uses the config loader when building
115
     *
116
     * @param string[] $resources
117
     *
118
     * @return self
119
     */
120 1
    public function useConfigLoader(array $resources) : self
121
    {
122 1
        $this->configLoader = new Loader\ConfigLoader();
123 1
        $this->configLoader->attachArray($resources);
124
125 1
        return $this;
126
    }
127
128
    /**
129
     * Uses the descriptor loader when building
130
     *
131
     * @param string[] $resources
132
     *
133
     * @return self
134
     */
135 1
    public function useDescriptorLoader(array $resources) : self
136
    {
137 1
        $this->descriptorLoader = new Loader\DescriptorLoader();
138 1
        $this->descriptorLoader->attachArray($resources);
139
140 1
        return $this;
141
    }
142
143
    /**
144
     * Uses the metadata loader when building
145
     *
146
     * Alias to the useDescriptorLoader method.
147
     *
148
     * @param string[] $resources
149
     *
150
     * @return self
151
     */
152 1
    public function useMetadataLoader(array $resources) : self
153
    {
154 1
        $this->useDescriptorLoader($resources);
155
156 1
        return $this;
157
    }
158
159
    /**
160
     * Sets the given patterns to the builder
161
     *
162
     * @param array<string, string>|null $patterns
163
     *
164
     * @return self
165
     *
166
     * @since 2.11.0
167
     */
168 1
    public function setPatterns(?array $patterns) : self
169
    {
170 1
        $this->patterns = $patterns;
171
172 1
        return $this;
173
    }
174
175
    /**
176
     * Sets the given hosts to the builder
177
     *
178
     * @param array<string, string[]>|null $hosts
179
     *
180
     * @return self
181
     */
182 1
    public function setHosts(?array $hosts) : self
183
    {
184 1
        $this->hosts = $hosts;
185
186 1
        return $this;
187
    }
188
189
    /**
190
     * Sets the given middlewares to the builder
191
     *
192
     * @param MiddlewareInterface[]|null $middlewares
193
     *
194
     * @return self
195
     */
196 1
    public function setMiddlewares(?array $middlewares) : self
197
    {
198 1
        $this->middlewares = $middlewares;
199
200 1
        return $this;
201
    }
202
203
    /**
204
     * Builds the router
205
     *
206
     * @return Router
207
     */
208 1
    public function build() : Router
209
    {
210 1
        $router = new Router();
211
212 1
        if (isset($this->configLoader)) {
213 1
            $this->configLoader->setContainer($this->container);
0 ignored issues
show
Bug introduced by
The method setContainer() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

213
            $this->configLoader->/** @scrutinizer ignore-call */ 
214
                                 setContainer($this->container);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
214 1
            $router->load($this->configLoader);
0 ignored issues
show
Bug introduced by
It seems like $this->configLoader can also be of type null; however, parameter $loaders of Sunrise\Http\Router\Router::load() does only seem to accept Sunrise\Http\Router\Loader\LoaderInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

214
            $router->load(/** @scrutinizer ignore-type */ $this->configLoader);
Loading history...
215
        }
216
217 1
        if (isset($this->descriptorLoader)) {
218 1
            $this->descriptorLoader->setContainer($this->container);
0 ignored issues
show
Bug introduced by
The method setContainer() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

218
            $this->descriptorLoader->/** @scrutinizer ignore-call */ 
219
                                     setContainer($this->container);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
219 1
            $this->descriptorLoader->setCache($this->cache);
220 1
            $this->descriptorLoader->setCacheKey($this->cacheKey);
221 1
            $router->load($this->descriptorLoader);
222
        }
223
224 1
        if (!empty($this->patterns)) {
225 1
            $router->addPatterns($this->patterns);
226
        }
227
228 1
        if (!empty($this->hosts)) {
229 1
            $router->addHosts($this->hosts);
230
        }
231
232 1
        if (!empty($this->middlewares)) {
233 1
            $router->addMiddleware(...$this->middlewares);
234
        }
235
236 1
        return $router;
237
    }
238
}
239