Passed
Branch refactor/kernels (d3615b)
by Atanas
01:46
created

Route::getMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\Exceptions\ConfigurationException;
13
use WPEmerge\Facades\Application;
14
use WPEmerge\Helpers\Handler;
15
use WPEmerge\Middleware\HasMiddlewareTrait;
16
use WPEmerge\Requests\RequestInterface;
17
use WPEmerge\Routing\Conditions\ConditionInterface;
18
use WPEmerge\Routing\Conditions\UrlCondition;
19
20
/**
21
 * Represent a route
22
 */
23
class Route implements RouteInterface {
24
	use HasMiddlewareTrait;
25
26
	/**
27
	 * Allowed methods.
28
	 *
29
	 * @var string[]
30
	 */
31
	protected $methods = [];
32
33
	/**
34
	 * Route condition.
35
	 *
36
	 * @var ConditionInterface
37
	 */
38
	protected $condition = null;
39
40
	/**
41
	 * Route handler.
42
	 *
43
	 * @var Handler
44
	 */
45
	protected $handler = null;
46
47
	/**
48
	 * Query filter.
49
	 *
50
	 * @var callable|null
51
	 */
52
	protected $query_filter = null;
53
54
	/**
55
	 * Query filter action priority.
56
	 *
57
	 * @var integer
58
	 */
59
	protected $query_filter_priority = 1000;
60
61
	/**
62
	 * Constructor.
63
	 *
64
	 * @codeCoverageIgnore
65
	 * @param  string[]           $methods
66
	 * @param  ConditionInterface $condition
67
	 * @param  string|\Closure    $handler
68
	 */
69
	public function __construct( $methods, $condition, $handler ) {
70
		$this->methods = $methods;
71
		$this->setCondition( $condition );
72
		$this->handler = new Handler( $handler, '', '\\App\\Controllers\\' );
73
	}
74
75
	/**
76
	 * Get allowed methods.
77
	 *
78
	 * @codeCoverageIgnore
79
	 * @return string[]
80
	 */
81
	public function getMethods() {
82
		return $this->methods;
83
	}
84
85
	/**
86
	 * {@inheritDoc}
87
	 * @codeCoverageIgnore
88
	 */
89
	public function getCondition() {
90
		return $this->condition;
91
	}
92
93
	/**
94
	 * {@inheritDoc}
95
	 * @codeCoverageIgnore
96
	 */
97
	public function setCondition( $condition ) {
98
		$this->condition = $condition;
99
	}
100
101
	/**
102
	 * Get handler.
103
	 *
104
	 * @codeCoverageIgnore
105
	 * @return Handler
106
	 */
107
	public function getHandler() {
108
		return $this->handler;
109
	}
110
111
	/**
112
	 * Set custom partial regex matching for the specified parameter.
113
	 *
114
	 * @throws ConfigurationException
115
	 * @param  string $parameter
116
	 * @param  string $regex
117
	 * @return static $this
118
	 */
119 2
	public function where( $parameter, $regex ) {
120 2
		$condition = $this->getCondition();
121
122 2
		if ( ! $condition instanceof UrlCondition ) {
123 1
			throw new ConfigurationException( 'Only routes with URL conditions can specify parameter regex matching.' );
124
		}
125
126 1
		$condition->setUrlWhere( array_merge(
127 1
			$condition->getUrlWhere(),
128 1
			[$parameter => $regex]
129
		) );
130
131 1
		return $this;
132
	}
133
134
	/**
135
	 * Get the main WordPress query vars filter, if any.
136
	 *
137
	 * @codeCoverageIgnore
138
	 * @return callable|null
139
	 */
140
	public function getQueryFilter() {
141
		return $this->query_filter;
142
	}
143
144
	/**
145
	 * Set the main WordPress query vars filter.
146
	 *
147
	 * @codeCoverageIgnore
148
	 * @param  callable|null $query_filter
149
	 * @return void
150
	 */
151
	public function setQueryFilter( $query_filter ) {
152
		$this->query_filter = $query_filter;
153
	}
154
155
	/**
156
	 * Apply the query filter, if any.
157
	 *
158
	 * @internal
159
	 * @throws ConfigurationException
160
	 * @param RequestInterface            $request
161
	 * @param  array<string, mixed>       $query_vars
162
	 * @return array<string, mixed>|false
163
	 */
164 4
	public function applyQueryFilter( $request, $query_vars ) {
165 4
		$condition = $this->getCondition();
166
167 4
		if ( $this->getQueryFilter() === null ) {
168 1
			return false;
169
		}
170
171 3
		if ( ! $condition instanceof UrlCondition ) {
172 1
			throw new ConfigurationException(
173
				'Only routes with URL condition can use queries. ' .
174 1
				'Make sure your route has a URL condition and it is not in a non-URL route group.'
175
			);
176
		}
177
178 2
		if ( ! $this->getCondition()->isSatisfied( $request ) ) {
179 1
			return false;
180
		}
181
182 1
		$arguments = $this->getCondition()->getArguments( $request );
183
184 1
		return call_user_func_array( $this->getQueryFilter(), array_merge( [$query_vars], array_values( $arguments ) ) );
185
	}
186
187
	/**
188
	 * Fluent alias of setQueryFilter().
189
	 *
190
	 * @codeCoverageIgnore
191
	 * @param  callable $query_filter
192
	 * @return static   $this
193
	 */
194
	public function query( $query_filter ) {
195
		$this->setQueryFilter( $query_filter );
196
197
		return $this;
198
	}
199
200
	/**
201
	 * {@inheritDoc}
202
	 */
203 2
	public function isSatisfied( RequestInterface $request ) {
204 2
		if ( ! in_array( $request->getMethod(), $this->methods ) ) {
205 1
			return false;
206
		}
207
208 2
		return $this->condition->isSatisfied( $request );
209
	}
210
211
	/**
212
	 * {@inheritDoc}
213
	 */
214 1
	public function getArguments( RequestInterface $request ) {
215 1
		return $this->getCondition()->getArguments( $request );
216
	}
217
218
	/**
219
	 * {@inheritDoc}
220
	 */
221 1
	public function handle( RequestInterface $request, $view ) {
222 1
		$arguments = array_merge(
223 1
			[$request, $view],
224 1
			array_values( $this->condition->getArguments( $request ) )
225
		);
226
227 1
		return call_user_func_array( [$this->getHandler(), 'execute'], $arguments );
228
	}
229
}
230