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

Route::decorate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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