Passed
Push — master ( 708113...83bad9 )
by
unknown
02:12
created

Factory::makeFromUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace CarbonFramework\Routing\Conditions;
4
5
use CarbonFramework\Framework;
6
use CarbonFramework\Routing\Conditions\Custom as CustomCondition;
7
use CarbonFramework\Routing\Conditions\Multiple as MultipleCondition;
8
use CarbonFramework\Routing\Conditions\Url as UrlCondition;
9
use Closure;
10
use Exception;
11
use ReflectionClass;
12
13
/**
14
 * Check against the current url
15
 */
16
class Factory {
17
	/**
18
	 * Create a new condition
19
	 *
20
	 * @param  string|array|Closure $options
21
	 * @return ConditionInterface
22
	 */
23
	public static function make( $options ) {
24
		if ( is_string( $options ) ) {
25
			return static::makeFromUrl( $options );
26
		}
27
28
		if ( is_array( $options ) ) {
29
			return static::makeFromArray( $options );
30
		}
31
32
		if ( is_a( $options, Closure::class ) ) {
33
			return static::makeFromClosure( $options );
34
		}
35
36
		throw new InvalidRouteConditionException( 'Invalid condition options supplied.' );
37
	}
38
39
	/**
40
	 * Check if the passed argument is a registered condition type
41
	 *
42
	 * @param  mixed   $condition_type
43
	 * @return boolean
44
	 */
45
	protected static function conditionTypeRegistered( $condition_type ) {
46
		if ( ! is_string( $condition_type ) ) {
47
			return false;
48
		}
49
50
		$condition_class = Framework::resolve( 'framework.routing.conditions.' . $condition_type );
51
		if ( $condition_class === null ) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($condition_class === null);.
Loading history...
52
			return false;
53
		}
54
55
		return true;
56
	}
57
58
	/**
59
	 * Create a new condition from a url
60
	 *
61
	 * @param  string             $url
62
	 * @return ConditionInterface
63
	 */
64
	protected static function makeFromUrl( $url ) {
65
		return new UrlCondition( $url );
66
	}
67
68
	/**
69
	 * Create a new condition from a closure
70
	 *
71
	 * @param  Closure            $closure
72
	 * @return ConditionInterface
73
	 */
74
	protected static function makeFromClosure( Closure $closure ) {
75
		return new CustomCondition( $closure );
76
	}
77
78
	/**
79
	 * Create a new condition from an array of conditions
80
	 *
81
	 * @param  array               $options
82
	 * @return ConditionInterface
83
	 */
84
	protected static function makeFromArrayOfConditions( $options ) {
85
		return new MultipleCondition( $options );
86
	}
87
88
	/**
89
	 * Create a new condition from an array
90
	 *
91
	 * @param  array               $options
92
	 * @return ConditionInterface
93
	 */
94
	protected static function makeFromArray( $options ) {
95
		if ( count( $options ) === 0 ) {
96
			throw new Exception( 'No condition type specified.' );
97
		}
98
99
		if ( is_array( $options[0] ) ) {
100
			return static::makeFromArrayOfConditions( $options );
101
		}
102
103
		$condition_type = $options[0];
104
		$arguments = array_slice( $options, 1 );
105
106
		if ( ! static::conditionTypeRegistered( $condition_type ) ) {
107
			if ( is_callable( $condition_type ) ) {
108
				$condition_type = 'custom';
109
				$arguments = $options;
110
			} else {
111
				throw new Exception( 'Unknown condition type specified: ' . $condition_type );
112
			}
113
		}
114
115
		$condition_class = Framework::resolve( 'framework.routing.conditions.' . $condition_type );
116
117
		$reflection = new ReflectionClass( $condition_class );
118
		$condition = $reflection->newInstanceArgs( $arguments );
119
		return $condition;
120
	}
121
}
122