Passed
Push — master ( e07512...4b3fd5 )
by Atanas
03:23
created

RouteGroup::setMiddleware()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
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 Closure;
13
use WPEmerge\Facades\RouteCondition;
14
use WPEmerge\Helpers\Arguments;
15
use WPEmerge\Middleware\HasMiddlewareTrait;
16
use WPEmerge\Requests\RequestInterface;
17
use WPEmerge\Routing\Conditions\ConditionInterface;
18
use WPEmerge\Routing\Conditions\UrlCondition;
19
use WPEmerge\Routing\Conditions\MultipleCondition;
20
21
class RouteGroup implements RouteInterface, HasRoutesInterface {
22
	use HasRoutesTrait {
23
		route as traitRoute;
24
		group as traitGroup;
25
	}
26
27
	use HasMiddlewareTrait {
28
		setMiddleware as traitSetMiddleware;
29
		addMiddleware as traitAddMiddleware;
30
	}
31
32
	/**
33
	 * Route condition
34
	 *
35
	 * @var ConditionInterface
36
	 */
37
	protected $condition = null;
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param string|Closure|ConditionInterface $condition
43
	 * @param Closure|null                      $routes
44
	 */
45
	public function __construct( $condition, $routes = null ) {
46
		list( $condition, $routes ) = Arguments::flip( $condition, $routes );
47
48
		if ( $condition !== null && ! $condition instanceof ConditionInterface ) {
49
			$condition = RouteCondition::make( $condition );
50
		}
51
52
		$this->condition = $condition;
53
54
		$routes( $this );
55
	}
56
57
	/**
58
	 * Get the first child route which is satisfied
59
	 *
60
	 * @param  RequestInterface $request
61
	 * @return RouteInterface|null
62
	 */
63
	protected function getSatisfiedRoute( RequestInterface $request ) {
64
		$routes = $this->getRoutes();
65
		foreach ( $routes as $route ) {
66
			if ( $route->isSatisfied( $request ) ) {
67
				return $route;
68
			}
69
		}
70
		return null;
71
	}
72
73
	/**
74
	 * Merge 2 conditions.
75
	 * If $parent is a UrlCondition and $child is a UrlCondition, concatenate them.
76
	 * In all other cases, combine conditions into a MultipleCondition.
77
	 *
78
	 * @param  ConditionInterface $parent
79
	 * @param  ConditionInterface $child
80
	 * @return ConditionInterface
81
	 */
82
	protected function mergeConditions( $parent, $child ) {
83
		if ( $parent instanceof UrlCondition && $child instanceof UrlCondition ) {
84
			return $parent->concatenate( $child );
85
		}
86
87
		return new MultipleCondition( [$parent, $child] );
88
	}
89
90
	/**
91
	 * {@inheritDoc}
92
	 */
93
	public function isSatisfied( RequestInterface $request ) {
94
		$route = $this->getSatisfiedRoute( $request );
95
		return $route !== null;
96
	}
97
98
	/**
99
	 * {@inheritDoc}
100
	 */
101
	public function handle( RequestInterface $request, $view ) {
102
		$route = $this->getSatisfiedRoute( $request );
103
		return $route ? $route->handle( $request, $view ) : null;
104
	}
105
106
	/**
107
	 * {@inheritDoc}
108
	 */
109
	public function getArguments( RequestInterface $request ) {
110
		$route = $this->getSatisfiedRoute( $request );
111
		return $route ? $route->getArguments( $request ) : [];
112
	}
113
114
	/**
115
	 * {@inheritDoc}
116
	 * @throws \WPEmerge\Exceptions\Exception
117
	 */
118
	public function route( $methods, $condition, $handler ) {
119
		if ( ! $condition instanceof ConditionInterface ) {
120
			$condition = RouteCondition::make( $condition );
121
		}
122
123
		if ( $this->condition !== null ) {
124
			$condition = $this->mergeConditions( $this->condition, $condition );
125
		}
126
127
		return $this->traitRoute( $methods, $condition, $handler );
128
	}
129
130
	/**
131
	 * {@inheritDoc}
132
	 */
133
	public function group( $condition, $routes = null ) {
134
		list( $condition, $routes ) = Arguments::flip( $condition, $routes );
135
136
		if ( $condition !== null && ! $condition instanceof ConditionInterface ) {
137
			$condition = RouteCondition::make( $condition );
138
		}
139
140
		if ( $this->condition !== null ) {
141
			$condition = $this->mergeConditions( $this->condition, $condition );
142
		}
143
144
		return $this->traitGroup( $condition, $routes );
145
	}
146
147
	/**
148
	 * {@inheritDoc}
149
	 */
150
	public function setMiddleware( $middleware ) {
151
		$routes = $this->getRoutes();
152
153
		foreach ( $routes as $route ) {
154
			$route->setMiddleware( $middleware );
155
		}
156
157
		$this->traitSetMiddleware( $middleware );
158
	}
159
160
	/**
161
	 * {@inheritDoc}
162
	 * @throws \WPEmerge\Exceptions\Exception
163
	 */
164
	public function addMiddleware( $middleware ) {
165
		$routes = $this->getRoutes();
166
167
		foreach ( $routes as $route ) {
168
			// Prepend group middleware.
169
			$route->setMiddleware( array_merge(
170
				$middleware,
171
				$route->getMiddleware()
172
			) );
173
		}
174
175
		return $this->traitAddMiddleware( $middleware );
176
	}
177
}
178