Failed Conditions
Push — master ( 5de32f...4f0134 )
by Atanas
01:47
created

RouteBlueprint   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 279
ccs 62
cts 62
cp 1
rs 10
c 0
b 0
f 0
wmc 25

23 Methods

Rating   Name   Duplication   Size   Complexity  
A setNamespace() 0 2 1
A where() 0 11 2
A getAttribute() 0 2 1
A url() 0 2 1
A setAttribute() 0 4 1
A middleware() 0 7 1
A attribute() 0 4 1
A methods() 0 7 1
A getAttributes() 0 2 1
A setAttributes() 0 2 1
A __construct() 0 2 1
A attributes() 0 4 1
A query() 0 7 1
A any() 0 2 1
A delete() 0 2 1
A put() 0 2 1
A options() 0 2 1
A post() 0 2 1
A get() 0 2 1
A group() 0 2 1
A patch() 0 2 1
A all() 0 2 1
A handle() 0 6 2
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Routing;
11
12
use WPEmerge\Routing\Conditions\ConditionInterface;
13
use WPEmerge\Support\Arr;
14
15
/**
16
 * Provide a fluent interface for registering routes with the router.
17
 */
18
class RouteBlueprint {
19
	/**
20
	 * Router.
21
	 *
22
	 * @var Router
23
	 */
24
	protected $router = null;
25
26
	/**
27
	 * Attributes.
28
	 *
29
	 * @var array<string, mixed>
30
	 */
31
	protected $attributes = [];
32
33
	/**
34
	 * Constructor.
35
	 *
36
	 * @codeCoverageIgnore
37
	 * @param Router           $router
38
	 */
39
	public function __construct( Router $router ) {
40
		$this->router = $router;
41
	}
42
43
	/**
44
	 * Get attributes.
45
	 *
46
	 * @return array<string, mixed>
47
	 */
48 1
	public function getAttributes() {
49 1
		return $this->attributes;
50
	}
51
52
	/**
53
	 * Set the attributes.
54
	 *
55
	 * @param  array<string, mixed> $attributes
56
	 * @return void
57
	 */
58 1
	public function setAttributes( $attributes ) {
59 1
		$this->attributes = $attributes;
60 1
	}
61
62
	/**
63
	 * Fluent alias for setAttributes().
64
	 *
65
	 * @codeCoverageIgnore
66
	 * @param  array<string, mixed> $attributes
67
	 * @return static               $this
68
	 */
69
	public function attributes( $attributes ) {
70
		$this->setAttributes( $attributes );
71
72
		return $this;
73
	}
74
75
	/**
76
	 * Get attribute.
77
	 *
78
	 * @param  string $key
79
	 * @param  mixed  $default
80
	 * @return mixed
81
	 */
82 1
	public function getAttribute( $key, $default = '' ) {
83 1
		return Arr::get( $this->getAttributes(), $key, $default );
84
	}
85
86
	/**
87
	 * Set attribute.
88
	 *
89
	 * @param  string $key
90
	 * @param  mixed  $value
91
	 * @return void
92
	 */
93 1
	public function setAttribute( $key, $value ) {
94 1
		$this->setAttributes( array_merge(
95 1
			$this->getAttributes(),
96 1
			[$key => $value]
97
		) );
98 1
	}
99
100
	/**
101
	 * Set attribute.
102
	 *
103
	 * @codeCoverageIgnore
104
	 * @param  string $key
105
	 * @param  mixed  $value
106
	 * @return static $this
107
	 */
108
	public function attribute( $key, $value ) {
109
		$this->setAttribute( $key, $value );
110
111
		return $this;
112
	}
113
114
	/**
115
	 * Match requests using one of the specified methods.
116
	 *
117
	 * @param  array<string> $methods
118
	 * @return static        $this
119
	 */
120 1
	public function methods( $methods ) {
121 1
		$methods = array_merge(
122 1
			$this->getAttribute( 'methods', [] ),
123 1
			$methods
124
		);
125
126 1
		return $this->attribute( 'methods', $methods );
127
	}
128
129
	/**
130
	 * Set the condition attribute to a URL.
131
	 *
132
	 * @param  string                $url
133
	 * @param  array<string, string> $where
134
	 * @return static                $this
135
	 */
136 1
	public function url( $url, $where = [] ) {
137 1
		return $this->where( 'url', $url, $where );
138
	}
139
140
	/**
141
	 * Set the condition attribute.
142
	 *
143
	 * @param  string|array|ConditionInterface $condition
144
	 * @param  mixed                           ,...$arguments
145
	 * @return static                          $this
146
	 */
147 3
	public function where( $condition ) {
148 3
		if ( ! $condition instanceof ConditionInterface ) {
149 3
			$condition = func_get_args();
150
		}
151
152 3
		$condition = $this->router->mergeConditionAttribute(
153 3
			$this->getAttribute( 'condition' ),
154 3
			$condition
155
		);
156
157 3
		return $this->attribute( 'condition', $condition );
158
	}
159
160
	/**
161
	 * Set the middleware attribute.
162
	 *
163
	 * @param  string|array<string> $middleware
164
	 * @return static               $this
165
	 */
166 1
	public function middleware( $middleware ) {
167 1
		$middleware = array_merge(
168 1
			(array) $this->getAttribute( 'middleware', [] ),
169 1
			(array) $middleware
170
		);
171
172 1
		return $this->attribute( 'middleware', $middleware );
173
	}
174
175
	/**
176
	 * Set the namespace attribute.
177
	 * This should be renamed to namespace for consistency once minimum PHP
178
	 * version is increased to 7+.
179
	 *
180
	 * @param  string $namespace
181
	 * @return static $this
182
	 */
183 1
	public function setNamespace( $namespace ) {
184 1
		return $this->attribute( 'namespace', $namespace );
185
	}
186
187
	/**
188
	 * Set the query attribute.
189
	 *
190
	 * @param  callable $query
191
	 * @return static   $this
192
	 */
193 1
	public function query( $query ) {
194 1
 		$query = $this->router->mergeQueryAttribute(
195 1
			$this->getAttribute( 'query', null ),
196 1
			$query
197
		);
198
199 1
		return $this->attribute( 'query', $query );
200
	}
201
202
	/**
203
	 * Create a route group.
204
	 *
205
	 * @param \Closure|string $routes Closure or path to file.
206
	 * @return void
207
	 */
208 1
	public function group( $routes ) {
209 1
		$this->router->group( $this->getAttributes(), $routes );
210 1
	}
211
212
	/**
213
	 * Create a route.
214
	 *
215
	 * @param  string|\Closure $handler
216
	 * @return void
217
	 */
218 2
	public function handle( $handler = '' ) {
219 2
		if ( ! empty( $handler ) ) {
220 1
			$this->attribute( 'handler', $handler );
221
		}
222
223 2
		$this->router->addRoute( $this->router->route( $this->getAttributes() ) );
224 2
	}
225
226
	/**
227
	 * Match requests with a method of GET or HEAD.
228
	 *
229
	 * @return static $this
230
	 */
231 1
	public function get() {
232 1
		return $this->methods( ['GET', 'HEAD'] );
233
	}
234
235
	/**
236
	 * Match requests with a method of POST.
237
	 *
238
	 * @return static $this
239
	 */
240 1
	public function post() {
241 1
		return $this->methods( ['POST'] );
242
	}
243
244
	/**
245
	 * Match requests with a method of PUT.
246
	 *
247
	 * @return static $this
248
	 */
249 1
	public function put() {
250 1
		return $this->methods( ['PUT'] );
251
	}
252
253
	/**
254
	 * Match requests with a method of PATCH.
255
	 *
256
	 * @return static $this
257
	 */
258 1
	public function patch() {
259 1
		return $this->methods( ['PATCH'] );
260
	}
261
262
	/**
263
	 * Match requests with a method of DELETE.
264
	 *
265
	 * @return static $this
266
	 */
267 1
	public function delete() {
268 1
		return $this->methods( ['DELETE'] );
269
	}
270
271
	/**
272
	 * Match requests with a method of OPTIONS.
273
	 *
274
	 * @return static $this
275
	 */
276 1
	public function options() {
277 1
		return $this->methods( ['OPTIONS'] );
278
	}
279
280
	/**
281
	 * Match requests with any method.
282
	 *
283
	 * @return static $this
284
	 */
285 1
	public function any() {
286 1
		return $this->methods( ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] );
287
	}
288
289
	/**
290
	 * Match ALL requests.
291
	 *
292
	 * @param  string|\Closure $handler
293
	 * @return void
294
	 */
295 1
	public function all( $handler = '' ) {
296 1
		$this->any()->url( '*' )->handle( $handler );
297 1
	}
298
}
299