Passed
Push — master ( 2d893f...5de32f )
by Atanas
02:41
created

Route::applyQueryFilter()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 4
rs 9.8666
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\Helpers\Handler;
13
use WPEmerge\Middleware\HasMiddlewareTrait;
14
use WPEmerge\Requests\RequestInterface;
15
use WPEmerge\Routing\Conditions\ConditionInterface;
16
17
/**
18
 * Represent a route
19
 */
20
class Route implements RouteInterface, HasQueryFilterInterface {
21
	use HasMiddlewareTrait;
22
	use HasQueryFilterTrait;
23
24
	/**
25
	 * Allowed methods.
26
	 *
27
	 * @var string[]
28
	 */
29
	protected $methods = [];
30
31
	/**
32
	 * Route handler.
33
	 *
34
	 * @var Handler
35
	 */
36
	protected $handler = null;
37
38
	/**
39
	 * Constructor.
40
	 *
41
	 * @codeCoverageIgnore
42
	 * @param  array<string>      $methods
43
	 * @param  ConditionInterface $condition
44
	 * @param  Handler            $handler
45
	 */
46
	public function __construct( $methods, $condition, Handler $handler ) {
47
		$this->methods = $methods;
48
		$this->setCondition( $condition );
49
		$this->handler = $handler;
50
	}
51
52
	/**
53
	 * Get allowed methods.
54
	 *
55
	 * @codeCoverageIgnore
56
	 * @return array<string>
57
	 */
58
	public function getMethods() {
59
		return $this->methods;
60
	}
61
62
	/**
63
	 * Get handler.
64
	 *
65
	 * @codeCoverageIgnore
66
	 * @return Handler
67
	 */
68
	public function getHandler() {
69
		return $this->handler;
70
	}
71
72
	/**
73
	 * {@inheritDoc}
74
	 */
75 2
	public function isSatisfied( RequestInterface $request ) {
76 2
		if ( ! in_array( $request->getMethod(), $this->methods ) ) {
77 1
			return false;
78
		}
79
80 2
		return $this->condition->isSatisfied( $request );
81
	}
82
83
	/**
84
	 * {@inheritDoc}
85
	 */
86 1
	public function getArguments( RequestInterface $request ) {
87 1
		return $this->getCondition()->getArguments( $request );
88
	}
89
90
	/**
91
	 * {@inheritDoc}
92
	 */
93 1
	public function handle( RequestInterface $request, $arguments = [] ) {
94 1
		$arguments = array_merge(
95 1
			[$request],
96 1
			$arguments,
97 1
			array_values( $this->condition->getArguments( $request ) )
98
		);
99
100 1
		return call_user_func_array( [$this->getHandler(), 'execute'], $arguments );
101
	}
102
}
103