Passed
Push — master ( 431198...0d9638 )
by
unknown
01:44
created

Factory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 81
rs 10
ccs 0
cts 27
cp 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 15 4
A makeFromUrl() 0 3 1
A makeFromClosure() 0 3 1
A makeFromArray() 0 21 4
A makeFromArrayOfConditions() 0 3 1
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\Routing\Conditions\Url as UrlCondition;
10
use CarbonFramework\Routing\Conditions\Custom as CustomCondition;
11
use CarbonFramework\Routing\Conditions\Multiple as MultipleCondition;
12
13
/**
14
 * Check against the current url
15
 */
16
class Factory {
17
	/**
18
	 * Create a new condition
19
	 *
20
	 * @param  string|Condition|array $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_a( $options, Closure::class ) ) {
29
			return static::makeFromClosure( $options );
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 23 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...
30
		}
31
32
		if ( is_array( $options ) ) {
33
			return static::makeFromArray( $options );
34
		}
35
36
		throw new InvalidRouteConditionException( 'Invalid condition options supplied.' );
37
	}
38
39
	/**
40
	 * Create a new condition from a url
41
	 *
42
	 * @param  string             $url
43
	 * @return ConditionInterface
44
	 */
45
	protected static function makeFromUrl( $url ) {
46
		return new UrlCondition( $url );
47
	}
48
49
	/**
50
	 * Create a new condition from a closure
51
	 *
52
	 * @param  Closure            $closure
53
	 * @return ConditionInterface
54
	 */
55
	protected static function makeFromClosure( Closure $closure ) {
56
		return new CustomCondition( $closure );
57
	}
58
59
	/**
60
	 * Create a new condition from an array
61
	 *
62
	 * @param  array               $options
63
	 * @return ConditionInterface
64
	 */
65
	protected static function makeFromArray( $options ) {
66
		if ( count( $options ) === 0 ) {
67
			throw new Exception( 'No condition type specified.' );
68
		}
69
70
		if ( is_array( $options[0] ) ) {
71
			return static::makeFromArrayOfConditions( $options );
72
		}
73
74
		$condition_type = $options[0];
75
		$arguments = array_slice( $options, 1 );
76
77
		$condition_class = Framework::resolve( 'framework.routing.conditions.' . $condition_type );
78
		if ( $condition_class === null ) {
79
			throw new Exception( 'Unknown condition type specified: ' . $condition_type );
80
		}
81
82
		$reflection = new ReflectionClass( $condition_class );
83
		$condition = $reflection->newInstanceArgs( $arguments );
84
		return $condition;
85
	}
86
87
	/**
88
	 * Create a new condition from an array of conditions
89
	 *
90
	 * @param  array               $options
91
	 * @return ConditionInterface
92
	 */
93
	protected static function makeFromArrayOfConditions( $options ) {
94
		return new MultipleCondition( $options );
95
	}
96
}
97