Passed
Push — master ( d2008b...aa9f10 )
by Atanas
01:54
created

ConditionFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 124
rs 10
c 0
b 0
f 0
ccs 46
cts 46
cp 1
wmc 17
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 15 4
A conditionTypeRegistered() 0 8 2
B getConditionTypeAndArguments() 0 23 5
A makeFromUrl() 0 3 1
A makeFromArray() 0 16 3
A makeFromArrayOfConditions() 0 3 1
A makeFromClosure() 0 3 1
1
<?php
2
3
namespace WPEmerge\Routing\Conditions;
4
5
use Closure;
6
use Exception;
7
use WPEmerge\Facades\Framework;
8
use WPEmerge\Routing\Conditions\CustomCondition;
9
use WPEmerge\Routing\Conditions\MultipleCondition;
10
use WPEmerge\Routing\Conditions\UrlCondition;
11
use ReflectionClass;
12
13
/**
14
 * Check against the current url
15
 */
16
class ConditionFactory {
17
	/**
18
	 * Create a new condition.
19
	 *
20
	 * @throws InvalidRouteConditionException
21
	 * @param  string|array|Closure           $options
22
	 * @return ConditionInterface
23
	 */
24 13
	public static function make( $options ) {
25 13
		if ( is_string( $options ) ) {
26 2
			return static::makeFromUrl( $options );
27
		}
28
29 11
		if ( is_array( $options ) ) {
30 9
			return static::makeFromArray( $options );
31
		}
32
33 2
		if ( is_a( $options, Closure::class ) ) {
34 1
			return static::makeFromClosure( $options );
35
		}
36
37 1
		throw new InvalidRouteConditionException( 'Invalid condition options supplied.' );
38
	}
39
40
	/**
41
	 * Check if the passed argument is a registered condition type.
42
	 *
43
	 * @param  mixed   $condition_type
44
	 * @return boolean
45
	 */
46 6
	protected static function conditionTypeRegistered( $condition_type ) {
47 6
		if ( ! is_string( $condition_type ) ) {
48 1
			return false;
49
		}
50
51 5
		$condition_class = Framework::resolve( WPEMERGE_ROUTING_CONDITIONS_KEY . $condition_type );
52 5
		return ( $condition_class !== null );
53
	}
54
55
	/**
56
	 * Resolve the condition type and its arguments from an options array.
57
	 *
58
	 * @throws Exception
59
	 * @param  array $options
60
	 * @return array
61
	 */
62 7
	protected static function getConditionTypeAndArguments( $options ) {
63 7
		$type = $options[0];
64 7
		$arguments = array_values( array_slice( $options, 1 ) );
65
66 7
		if ( is_string( $type ) && substr( $type, 0, 1 ) === '!' ) {
67 1
			$arguments = array_merge( [ substr( $type, 1 ) ], $arguments );
68 1
			$type = 'negate';
69 1
		}
70
71 7
		if ( ! static::conditionTypeRegistered( $type ) ) {
72 3
			if ( is_callable( $type ) ) {
73 2
				$type = 'custom';
74 2
				$arguments = $options;
75 2
			} else {
76 1
				throw new Exception( 'Unknown condition type specified: ' . $type );
77
			}
78 2
		}
79
80
		return array(
81 6
			'type' => $type,
82 6
			'arguments' => $arguments,
83 6
		);
84
	}
85
86
	/**
87
	 * Create a new condition from a url.
88
	 *
89
	 * @param  string             $url
90
	 * @return ConditionInterface
91
	 */
92 1
	protected static function makeFromUrl( $url ) {
93 1
		return new UrlCondition( $url );
94
	}
95
96
	/**
97
	 * Create a new condition from an array.
98
	 *
99
	 * @throws Exception
100
	 * @param  array               $options
101
	 * @return ConditionInterface
102
	 */
103 9
	protected static function makeFromArray( $options ) {
104 9
		if ( count( $options ) === 0 ) {
105 1
			throw new Exception( 'No condition type specified.' );
106
		}
107
108 8
		if ( is_array( $options[0] ) ) {
109 1
			return static::makeFromArrayOfConditions( $options );
110
		}
111
112 8
		$condition_options = static::getConditionTypeAndArguments( $options );
113 7
		$condition_class = Framework::resolve( WPEMERGE_ROUTING_CONDITIONS_KEY . $condition_options['type'] );
114
115 7
		$reflection = new ReflectionClass( $condition_class );
116 7
		$condition = $reflection->newInstanceArgs( $condition_options['arguments'] );
117 7
		return $condition;
118
	}
119
120
	/**
121
	 * Create a new condition from an array of conditions.
122
	 *
123
	 * @param  array               $options
124
	 * @return ConditionInterface
125
	 */
126 1
	protected static function makeFromArrayOfConditions( $options ) {
127 1
		return new MultipleCondition( $options );
128
	}
129
130
	/**
131
	 * Create a new condition from a closure.
132
	 *
133
	 * @param  Closure            $closure
134
	 * @return ConditionInterface
135
	 */
136 1
	protected static function makeFromClosure( Closure $closure ) {
137 1
		return new CustomCondition( $closure );
138
	}
139
}
140