Passed
Push — master ( 43db54...cc5a21 )
by Atanas
02:03
created

Factory::makeFromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 16
ccs 0
cts 10
cp 0
crap 12
rs 9.4285
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
		return ( $condition_class !== null );
52
	}
53
54
	/**
55
	 * Create a new condition from a url
56
	 *
57
	 * @param  string             $url
58
	 * @return ConditionInterface
59
	 */
60
	protected static function makeFromUrl( $url ) {
61
		return new UrlCondition( $url );
62
	}
63
64
	/**
65
	 * Create a new condition from a closure
66
	 *
67
	 * @param  Closure            $closure
68
	 * @return ConditionInterface
69
	 */
70
	protected static function makeFromClosure( Closure $closure ) {
71
		return new CustomCondition( $closure );
72
	}
73
74
	/**
75
	 * Create a new condition from an array of conditions
76
	 *
77
	 * @param  array               $options
78
	 * @return ConditionInterface
79
	 */
80
	protected static function makeFromArrayOfConditions( $options ) {
81
		return new MultipleCondition( $options );
82
	}
83
84
	/**
85
	 * Resolve the condition type and it's arguments from an options array
86
	 *
87
	 * @param  array $options
88
	 * @return array
89
	 */
90
	protected static function getConditionTypeAndArguments( $options ) {
91
		$type = $options[0];
92
		$arguments = array_slice( $options, 1 );
93
94
		if ( ! static::conditionTypeRegistered( $type ) ) {
95
			if ( is_callable( $type ) ) {
96
				$type = 'custom';
97
				$arguments = $options;
98
			} else {
99
				throw new Exception( 'Unknown condition type specified: ' . $type );
100
			}
101
		}
102
103
		return array(
104
			'type' => $type,
105
			'arguments' => $arguments,
106
		);
107
	}
108
109
	/**
110
	 * Create a new condition from an array
111
	 *
112
	 * @param  array               $options
113
	 * @return ConditionInterface
114
	 */
115
	protected static function makeFromArray( $options ) {
116
		if ( count( $options ) === 0 ) {
117
			throw new Exception( 'No condition type specified.' );
118
		}
119
120
		if ( is_array( $options[0] ) ) {
121
			return static::makeFromArrayOfConditions( $options );
122
		}
123
124
		$condition_options = static::getConditionTypeAndArguments( $options );
125
		$condition_class = Framework::resolve( 'framework.routing.conditions.' . $condition_options['type'] );
126
127
		$reflection = new ReflectionClass( $condition_class );
128
		$condition = $reflection->newInstanceArgs( $condition_options['arguments'] );
129
		return $condition;
130
	}
131
}
132