Passed
Push — master ( 4e8eae...431198 )
by
unknown
01:42
created

Factory::makeFromClosure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace CarbonFramework\Routing\Conditions;
4
5
use Closure;
6
use ReflectionClass;
7
use Exception;
8
use CarbonFramework\Framework;
9
use CarbonFramework\Request;
10
use CarbonFramework\Routing\Conditions\Url as UrlCondition;
11
use CarbonFramework\Routing\Conditions\Custom as CustomCondition;
12
use CarbonFramework\Routing\Conditions\Multiple as MultipleCondition;
13
14
/**
15
 * Check against the current url
16
 */
17
class Factory {
18
	/**
19
	 * Create a new condition
20
	 *
21
	 * @param  string|Condition|array $options
22
	 * @return ConditionInterface
23
	 */
24
	public static function make( $options ) {
25
		if ( is_string( $options ) ) {
26
			return static::makeFromUrl( $options );
27
		}
28
29
		if ( is_a( $options, Closure::class ) ) {
30
			return static::makeFromClosure( $options );
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 24 can also be of type array; however, CarbonFramework\Routing\...tory::makeFromClosure() does only seem to accept object<Closure>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
31
		}
32
33
		if ( is_array( $options ) ) {
34
			if ( count( $options ) > 0 && is_array( $options[0] ) ) {
35
				return static::makeFromArrayOfConditions( $options );
36
			}
37
38
			return static::makeFromArray( $options );
39
		}
40
41
		throw new InvalidRouteConditionException( 'Invalid condition options supplied.' );
42
	}
43
44
	/**
45
	 * Create a new condition from a url
46
	 *
47
	 * @param  string             $url
48
	 * @return ConditionInterface
49
	 */
50
	protected static function makeFromUrl( $url ) {
51
		return new UrlCondition( $url );
52
	}
53
54
	/**
55
	 * Create a new condition from a closure
56
	 *
57
	 * @param  Closure            $closure
58
	 * @return ConditionInterface
59
	 */
60
	protected static function makeFromClosure( Closure $closure ) {
61
		return new CustomCondition( $closure );
62
	}
63
64
	/**
65
	 * Create a new condition from an array
66
	 *
67
	 * @param  array               $options
68
	 * @return ConditionInterface
69
	 */
70
	protected static function makeFromArray( $options ) {
71
		if ( count( $options ) === 0 ) {
72
			throw new Exception( 'No condition type specified.' );
73
		}
74
75
		$condition_type = $options[0];
76
		$arguments = array_slice( $options, 1 );
77
78
		$condition_class = Framework::resolve( 'framework.routing.conditions.' . $condition_type );
79
		if ( $condition_class === null ) {
80
			throw new Exception( 'Unknown condition type specified: ' . $condition_type );
81
		}
82
83
		$reflection = new ReflectionClass( $condition_class );
84
		$condition = $reflection->newInstanceArgs( $arguments );
85
		return $condition;
86
	}
87
88
	/**
89
	 * Create a new condition from an array of conditions
90
	 *
91
	 * @param  array               $options
92
	 * @return ConditionInterface
93
	 */
94
	protected static function makeFromArrayOfConditions( $options ) {
95
		return new MultipleCondition( $options );
96
	}
97
}
98