Passed
Push — master ( aa9f10...00a0be )
by Atanas
02:02
created

ConditionFactory::isNegatedCondition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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
	const NEGATE_CONDITION_PREFIX = '!';
18
19
	/**
20
	 * Create a new condition.
21
	 *
22
	 * @throws InvalidRouteConditionException
23
	 * @param  string|array|Closure           $options
24
	 * @return ConditionInterface
25
	 */
26 13
	public static function make( $options ) {
27 13
		if ( is_string( $options ) ) {
28 2
			return static::makeFromUrl( $options );
29
		}
30
31 11
		if ( is_array( $options ) ) {
32 9
			return static::makeFromArray( $options );
33
		}
34
35 2
		if ( $options instanceof Closure ) {
36 1
			return static::makeFromClosure( $options );
37
		}
38
39 1
		throw new InvalidRouteConditionException( 'Invalid condition options supplied.' );
40
	}
41
42
	/**
43
	 * Check if the passed argument is a registered condition type.
44
	 *
45
	 * @param  mixed   $condition_type
46
	 * @return boolean
47
	 */
48 6
	protected static function conditionTypeRegistered( $condition_type ) {
49 6
		if ( ! is_string( $condition_type ) ) {
50 1
			return false;
51
		}
52
53 5
		$condition_class = Framework::resolve( WPEMERGE_ROUTING_CONDITIONS_KEY . $condition_type );
54 5
		return ( $condition_class !== null );
55
	}
56
57
	/**
58
	 * Check if a condition is negated.
59
	 *
60
	 * @param  mixed   $condition
61
	 * @return boolean
62
	 */
63 1
	protected static function isNegatedCondition( $condition ) {
64
		return (
65 1
			is_string( $condition )
66 1
			&&
67 1
			substr( $condition, 0, strlen( static::NEGATE_CONDITION_PREFIX ) ) === static::NEGATE_CONDITION_PREFIX
68 1
		);
69
	}
70
71
	/**
72
	 * Parse the condition type and its arguments from an options array.
73
	 *
74
	 * @throws Exception
75
	 * @param  array $options
76
	 * @return array
77
	 */
78 7
	protected static function parseConditionOptions( $options ) {
79 7
		$type = $options[0];
80 7
		$arguments = array_values( array_slice( $options, 1 ) );
81
82 7
		if ( static::isNegatedCondition( $type ) ) {
83 1
			$negated_condition = substr( $type, strlen( static::NEGATE_CONDITION_PREFIX ) );
84 1
			$arguments = array_merge( [ $negated_condition ], $arguments );
85 1
			$type = 'negate';
86 1
		}
87
88 7
		if ( ! static::conditionTypeRegistered( $type ) ) {
89 3
			if ( is_callable( $type ) ) {
90 2
				$type = 'custom';
91 2
				$arguments = $options;
92 2
			} else {
93 1
				throw new Exception( 'Unknown condition type specified: ' . $type );
94
			}
95 2
		}
96
97
		return array(
98 6
			'type' => $type,
99 6
			'arguments' => $arguments,
100 6
		);
101
	}
102
103
	/**
104
	 * Create a new condition from a url.
105
	 *
106
	 * @param  string             $url
107
	 * @return ConditionInterface
108
	 */
109 1
	protected static function makeFromUrl( $url ) {
110 1
		return new UrlCondition( $url );
111
	}
112
113
	/**
114
	 * Create a new condition from an array.
115
	 *
116
	 * @throws Exception
117
	 * @param  array               $options
118
	 * @return ConditionInterface
119
	 */
120 9
	protected static function makeFromArray( $options ) {
121 9
		if ( count( $options ) === 0 ) {
122 1
			throw new Exception( 'No condition type specified.' );
123
		}
124
125 8
		if ( is_array( $options[0] ) ) {
126 1
			return static::makeFromArrayOfConditions( $options );
127
		}
128
129 8
		$condition_options = static::parseConditionOptions( $options );
130 7
		$condition_class = Framework::resolve( WPEMERGE_ROUTING_CONDITIONS_KEY . $condition_options['type'] );
131
132 7
		$reflection = new ReflectionClass( $condition_class );
133 7
		$condition = $reflection->newInstanceArgs( $condition_options['arguments'] );
134 7
		return $condition;
135
	}
136
137
	/**
138
	 * Create a new condition from an array of conditions.
139
	 *
140
	 * @param  array               $options
141
	 * @return ConditionInterface
142
	 */
143 1
	protected static function makeFromArrayOfConditions( $options ) {
144 1
		return new MultipleCondition( $options );
145
	}
146
147
	/**
148
	 * Create a new condition from a closure.
149
	 *
150
	 * @param  Closure            $closure
151
	 * @return ConditionInterface
152
	 */
153 1
	protected static function makeFromClosure( Closure $closure ) {
154 1
		return new CustomCondition( $closure );
155
	}
156
}
157