Passed
Branch refactor/kernels (d70466)
by Atanas
01:58
created

Route::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 4
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\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  array<string>      $methods
45
	 * @param  ConditionInterface $condition
46
	 * @param  Handler            $handler
47
	 */
48
	public function __construct( $methods, $condition, Handler $handler ) {
49
		$this->methods = $methods;
50
		$this->setCondition( $condition );
51
		$this->handler = $handler;
52
	}
53
54
	/**
55
	 * Get allowed methods.
56
	 *
57
	 * @codeCoverageIgnore
58
	 * @return array<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
	 * {@inheritDoc}
76
	 */
77 2
	public function isSatisfied( RequestInterface $request ) {
78 2
		if ( ! in_array( $request->getMethod(), $this->methods ) ) {
79 1
			return false;
80
		}
81
82 2
		return $this->condition->isSatisfied( $request );
83
	}
84
85
	/**
86
	 * {@inheritDoc}
87
	 */
88 1
	public function getArguments( RequestInterface $request ) {
89 1
		return $this->getCondition()->getArguments( $request );
90
	}
91
92
	/**
93
	 * {@inheritDoc}
94
	 */
95 1
	public function handle( RequestInterface $request, $arguments = [] ) {
96 1
		$arguments = array_merge(
97 1
			[$request],
98 1
			$arguments,
99 1
			array_values( $this->condition->getArguments( $request ) )
100
		);
101
102 1
		return call_user_func_array( [$this->getHandler(), 'execute'], $arguments );
103
	}
104
}
105