Passed
Push — master ( c5dc05...896c66 )
by Atanas
02:38
created

RouteGroup::route()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 9
loc 9
ccs 0
cts 6
cp 0
crap 6
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace WPEmerge\Routing;
4
5
use Closure;
6
use Exception;
7
use WPEmerge\Facades\RouteCondition;
8
use WPEmerge\Middleware\HasMiddlewareTrait;
9
use WPEmerge\Requests\Request;
10
use WPEmerge\Routing\Conditions\ConditionInterface;
11
use WPEmerge\Routing\Conditions\UrlCondition;
12
use WPEmerge\Routing\Conditions\MultipleCondition;
13
14
class RouteGroup implements RouteInterface, HasRoutesInterface {
15
	use HasRoutesTrait {
16
		route as traitRoute;
17
		group as traitGroup;
18
	}
19
20
	use HasMiddlewareTrait {
21
		addMiddleware as traitAddMiddleware;
22
	}
23
24
	/**
25
	 * Route condition
26
	 *
27
	 * @var ConditionInterface
28
	 */
29
	protected $condition = null;
30
31
	/**
32
	 * Constructor
33
	 *
34
	 * @throws Exception
35
	 * @param  mixed     $condition
36
	 * @param  Closure   $closure
37
	 */
38
	public function __construct( $condition, Closure $closure ) {
39
		if ( ! $condition instanceof ConditionInterface ) {
40
			$condition = RouteCondition::make( $condition );
41
		}
42
43
		$this->condition = $condition;
44
45
		$closure( $this );
46
	}
47
48
	/**
49
	 * Get the first child route which is satisfied
50
	 *
51
	 * @return RouteInterface|null
52
	 */
53
	protected function getSatisfiedRoute( Request $request ) {
54
		$routes = $this->getRoutes();
55
		foreach ( $routes as $route ) {
56
			if ( $route->isSatisfied( $request ) ) {
57
				return $route;
58
			}
59
		}
60
		return null;
61
	}
62
63
	/**
64
	 * Merge 2 conditions (in supplied order).
65
	 *
66
	 * @param  ConditionInterface|null $parent
67
	 * @param  ConditionInterface      $child
68
	 * @return ConditionInterface
69
	 */
70
	protected function mergeConditions( $parent, $child ) {
71
		if ( $parent === null ) {
72
			return $child;
73
		}
74
75
		if ( $parent instanceof UrlCondition ) {
76
			if ( $child instanceof UrlCondition ) {
77
				return $parent->concatenate( $child );
78
			}
79
80
			// Ignore parent if conditions are incompatible.
81
			return $child;
82
		}
83
84
		return new MultipleCondition( [ $parent, $child ] );
85
	}
86
87
	/**
88
	 * {@inheritDoc}
89
	 */
90
	public function isSatisfied( Request $request ) {
91
		$route = $this->getSatisfiedRoute( $request );
92
		return $route !== null;
93
	}
94
95
	/**
96
	 * {@inheritDoc}
97
	 */
98
	public function handle( Request $request, $view ) {
99
		$route = $this->getSatisfiedRoute( $request );
100
		return $route ? $route->handle( $request, $view ) : null;
101
	}
102
103
	/**
104
	 * {@inheritDoc}
105
	 */
106 View Code Duplication
	public function route( $methods, $condition, $handler ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
		if ( ! $condition instanceof ConditionInterface ) {
108
			$condition = RouteCondition::make( $condition );
109
		}
110
111
		$condition = $this->mergeConditions( $this->condition, $condition );
112
113
		return $this->traitRoute( $methods, $condition, $handler );
114
	}
115
116
	/**
117
	 * {@inheritDoc}
118
	 */
119 View Code Duplication
	public function group( $condition, Closure $closure ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
		if ( ! $condition instanceof ConditionInterface ) {
121
			$condition = RouteCondition::make( $condition );
122
		}
123
124
		$condition = $this->mergeConditions( $this->condition, $condition );
125
126
		return $this->traitGroup( $condition, $closure );
0 ignored issues
show
Documentation introduced by
$condition is of type object<WPEmerge\Routing\...ons\ConditionInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
	}
128
129
	/**
130
	 * {@inheritDoc}
131
	 */
132
	public function addMiddleware( $middleware ) {
133
		$routes = $this->getRoutes();
134
135
		foreach ( $routes as $route ) {
136
			$route->addMiddleware( $middleware );
137
		}
138
139
		return $this->traitAddMiddleware( $middleware );
140
	}
141
}
142