Passed
Branch refactor/kernels (b754ed)
by Atanas
01:59
created

Route::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
c 0
b 0
f 0
cc 1
crap 2
rs 10
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\Helpers\Handler;
14
use WPEmerge\Middleware\HasMiddlewareTrait;
15
use WPEmerge\Requests\RequestInterface;
16
use WPEmerge\Routing\Conditions\ConditionInterface;
17
use WPEmerge\Routing\Conditions\UrlCondition;
18
19
/**
20
 * Represent a route
21
 */
22
class Route implements RouteInterface, HasQueryFilterInterface {
23
	use HasMiddlewareTrait;
24
	use HasQueryFilterTrait;
25
26
	/**
27
	 * Allowed methods.
28
	 *
29
	 * @var string[]
30
	 */
31
	protected $methods = [];
32
33
	/**
34
	 * Route handler.
35
	 *
36
	 * @var Handler
37
	 */
38
	protected $handler = null;
39
40
	/**
41
	 * Constructor.
42
	 *
43
	 * @codeCoverageIgnore
44
	 * @param  string[]           $methods
45
	 * @param  ConditionInterface $condition
46
	 * @param  string|\Closure    $handler
47
	 */
48
	public function __construct( $methods, $condition, $handler ) {
49
		$this->methods = $methods;
50
		$this->setCondition( $condition );
51
		$this->handler = new Handler( $handler, '', '\\App\\Controllers\\' );
52
	}
53
54
	/**
55
	 * Get allowed methods.
56
	 *
57
	 * @codeCoverageIgnore
58
	 * @return string[]
59
	 */
60
	public function getMethods() {
61
		return $this->methods;
62
	}
63
64
	/**
65
	 * Get handler.
66
	 *
67
	 * @codeCoverageIgnore
68
	 * @return Handler
69
	 */
70
	public function getHandler() {
71
		return $this->handler;
72
	}
73
74
	/**
75
	 * Set custom partial regex matching for the specified parameter.
76
	 *
77
	 * @throws ConfigurationException
78
	 * @param  string $parameter
79
	 * @param  string $regex
80
	 * @return static $this
81
	 */
82 2
	public function where( $parameter, $regex ) {
83 2
		$condition = $this->getCondition();
84
85 2
		if ( ! $condition instanceof UrlCondition ) {
86 1
			throw new ConfigurationException( 'Only routes with URL conditions can specify parameter regex matching.' );
87
		}
88
89 1
		$condition->setUrlWhere( array_merge(
90 1
			$condition->getUrlWhere(),
91 1
			[$parameter => $regex]
92
		) );
93
94 1
		return $this;
95
	}
96
97
	/**
98
	 * {@inheritDoc}
99
	 */
100 2
	public function isSatisfied( RequestInterface $request ) {
101 2
		if ( ! in_array( $request->getMethod(), $this->methods ) ) {
102 1
			return false;
103
		}
104
105 2
		return $this->condition->isSatisfied( $request );
106
	}
107
108
	/**
109
	 * {@inheritDoc}
110
	 */
111 1
	public function getArguments( RequestInterface $request ) {
112 1
		return $this->getCondition()->getArguments( $request );
113
	}
114
115
	/**
116
	 * {@inheritDoc}
117
	 */
118 1
	public function handle( RequestInterface $request, $view ) {
119 1
		$arguments = array_merge(
120 1
			[$request, $view],
121 1
			array_values( $this->condition->getArguments( $request ) )
122
		);
123
124 1
		return call_user_func_array( [$this->getHandler(), 'execute'], $arguments );
125
	}
126
}
127