Passed
Push — master ( b7467b...db04eb )
by Atanas
02:41
created

Factory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 10
c 0
b 0
f 0
ccs 0
cts 42
cp 0
wmc 15
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 15 4
A conditionTypeRegistered() 0 8 2
A makeFromUrl() 0 3 1
A makeFromClosure() 0 3 1
A makeFromArrayOfConditions() 0 3 1
A getConditionTypeAndArguments() 0 18 3
A makeFromArray() 0 16 3
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
	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