Passed
Push — master ( bdd604...f2c58d )
by Atanas
01:57
created

Factory::makeFromUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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