Passed
Push — master ( 517cb3...2c2725 )
by Atanas
02:15
created

Factory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 15 4
A conditionTypeRegistered() 0 8 2
A getConditionTypeAndArguments() 0 18 3
A makeFromUrl() 0 3 1
A makeFromArrayOfConditions() 0 3 1
A makeFromArray() 0 16 3
A makeFromClosure() 0 3 1
1
<?php
2
3
namespace WPEmerge\Routing\Conditions;
4
5
use Closure;
6
use Exception;
7
use WPEmerge;
8
use WPEmerge\Routing\Conditions\Custom as CustomCondition;
9
use WPEmerge\Routing\Conditions\Multiple as MultipleCondition;
10
use WPEmerge\Routing\Conditions\Url as UrlCondition;
11
use ReflectionClass;
12
13
/**
14
 * Check against the current url
15
 */
16
class Factory {
17
	/**
18
	 * Create a new condition
19
	 *
20
	 * @throws InvalidRouteConditionException
21
	 * @param  string|array|Closure           $options
22
	 * @return ConditionInterface
23
	 */
24 12
	public static function make( $options ) {
25 12
		if ( is_string( $options ) ) {
26 2
			return static::makeFromUrl( $options );
27
		}
28
29 10
		if ( is_array( $options ) ) {
30 8
			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 = WPEmerge::resolve( WP_EMERGE_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 6
	protected static function getConditionTypeAndArguments( $options ) {
63 6
		$type = $options[0];
64 6
		$arguments = array_slice( $options, 1 );
65
66 6
		if ( ! static::conditionTypeRegistered( $type ) ) {
67 3
			if ( is_callable( $type ) ) {
68 2
				$type = 'custom';
69 2
				$arguments = $options;
70 2
			} else {
71 1
				throw new Exception( 'Unknown condition type specified: ' . $type );
72
			}
73 2
		}
74
75
		return array(
76 5
			'type' => $type,
77 5
			'arguments' => $arguments,
78 5
		);
79
	}
80
81
	/**
82
	 * Create a new condition from a url
83
	 *
84
	 * @param  string             $url
85
	 * @return ConditionInterface
86
	 */
87 1
	protected static function makeFromUrl( $url ) {
88 1
		return new UrlCondition( $url );
89
	}
90
91
	/**
92
	 * Create a new condition from an array of conditions
93
	 *
94
	 * @param  array               $options
95
	 * @return ConditionInterface
96
	 */
97 1
	protected static function makeFromArrayOfConditions( $options ) {
98 1
		return new MultipleCondition( $options );
99
	}
100
101
	/**
102
	 * Create a new condition from an array
103
	 *
104
	 * @throws Exception
105
	 * @param  array               $options
106
	 * @return ConditionInterface
107
	 */
108 8
	protected static function makeFromArray( $options ) {
109 8
		if ( count( $options ) === 0 ) {
110 1
			throw new Exception( 'No condition type specified.' );
111
		}
112
113 7
		if ( is_array( $options[0] ) ) {
114 1
			return static::makeFromArrayOfConditions( $options );
115
		}
116
117 7
		$condition_options = static::getConditionTypeAndArguments( $options );
118 6
		$condition_class = WPEmerge::resolve( WP_EMERGE_ROUTING_CONDITIONS_KEY . $condition_options['type'] );
119
120 6
		$reflection = new ReflectionClass( $condition_class );
121 6
		$condition = $reflection->newInstanceArgs( $condition_options['arguments'] );
122 6
		return $condition;
123
	}
124
125
	/**
126
	 * Create a new condition from a closure
127
	 *
128
	 * @param  Closure            $closure
129
	 * @return ConditionInterface
130
	 */
131 1
	protected static function makeFromClosure( Closure $closure ) {
132 1
		return new CustomCondition( $closure );
133
	}
134
}
135