Completed
Branch milestone/2_0/react-ui (57d10c)
by htmlBurger
02:47
created

Factory::make()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 1
dl 0
loc 22
ccs 0
cts 14
cp 0
crap 20
rs 8.9197
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
use Carbon_Fields\Helper\Helper;
6
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7
8
class Factory {
9
10
	/**
11
	 * Get the type for the specified class
12
	 * 
13
	 * @param  string $class
14
	 * @return string
15
	 */
16
	public function get_type( $class ) {
17
		return Helper::class_to_type( $class, '_Condition' );
18
	}
19
20
	/**
21
	 * Get an instance of the specified type
22
	 * 
23
	 * @param  string $type
24
	 * @return mixed
25
	 */
26
	public function make( $type ) {
27
		$condition_type_superclass = 'Carbon_Fields\\Container\\Condition\\Condition';
28
		$normalized_type = Helper::normalize_type( $type );
29
		
30
		$identifier = 'container_condition_type_' . $normalized_type;
31
		if ( \Carbon_Fields\Carbon_Fields::has( $identifier ) ) {
32
			return \Carbon_Fields\Carbon_Fields::resolve( $identifier );
33
		}
34
35
		if ( class_exists( $type ) ) {
36
			$reflection = new \ReflectionClass( $type );
37
			if ( $reflection->isSubclassOf( $condition_type_superclass ) ) {
38
				return new $type();
39
			} else {
40
				Incorrect_Syntax_Exception::raise( 'Condition must be of type ' . $condition_type_superclass );
41
				return null;
42
			}
43
		}
44
45
		Incorrect_Syntax_Exception::raise( 'Unknown condition type "' . $type . '".' );
46
		return null;
47
	}
48
}