Completed
Push — milestone/2_0/react-ui ( d404a3...73b2ee )
by
unknown
21:44
created

Factory::make()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 1
dl 0
loc 21
ccs 0
cts 14
cp 0
crap 20
rs 9.0534
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
use Carbon_Fields\App;
6
use Carbon_Fields\Helper\Helper;
7
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
8
9
class Factory {
10
11
	/**
12
	 * Get the type for the specified class
13
	 * 
14
	 * @param  string $class
15
	 * @return string
16
	 */
17
	public function get_type( $class ) {
18
		return Helper::class_to_type( $class, '_Condition' );
19
	}
20
21
	/**
22
	 * Get an instance of the specified type
23
	 * 
24
	 * @param  string $type
25
	 * @return mixed
26
	 */
27
	public function make( $type ) {
28
		$condition_type_superclass = 'Carbon_Fields\\Container\\Condition\\Condition';
1 ignored issue
show
Comprehensibility Naming introduced by
The variable name $condition_type_superclass exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
29
		$normalized_type = Helper::normalize_type( $type );
30
		
31
		$identifier = 'container_condition_type_' . $normalized_type;
32
		if ( App::has( $identifier ) ) {
33
			return App::resolve( $identifier );
34
		}
35
36
		if ( class_exists( $type ) ) {
37
			$reflection = new \ReflectionClass( $type );
38
			if ( $reflection->isSubclassOf( $condition_type_superclass ) ) {
39
				return new $type();
40
			} else {
41
				Incorrect_Syntax_Exception::raise( 'Field must be of type Carbon_Fields\\Field\\Field' );
42
			}
43
		}
44
45
		Incorrect_Syntax_Exception::raise( 'Unknown condition type "' . $type . '".' );
46
		return null;
47
	}
48
}