Completed
Push — master ( 7b30a8...0b84fa )
by Marin
02:35
created

Datastore::__construct()   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 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php 
2
3
namespace Carbon_Fields\Datastore;
4
5
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
6
7
/**
8
 * Base datastore.
9
 * Defines the key datastore methods and their default implementations.
10
 */
11
abstract class Datastore implements Datastore_Interface {
12
	/**
13
	 * Initialize the datastore.
14
	 **/
15
	public function __construct() {
16
		$this->init();
17
	}
18
19
	/**
20
	 * Initialization tasks for concrete datastores.
21
	 *
22
	 * @abstract
23
	 **/
24
	abstract public function init();
25
26
	/**
27
	 * Create a new datastore of type $type.
28
	 *
29
	 * @param string $type
30
	 * @return Datastore $datastore
31
	 **/
32
	public static function factory( $type ) {
33
		$type = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $type ) ) );
34
35
		$class = __NAMESPACE__ . '\\' . $type . '_Datastore';
36
37
		if ( ! class_exists( $class ) ) {
38
			Incorrect_Syntax_Exception::raise( 'Unknown data store type "' . $type . '".' );
39
		}
40
41
		$field = new $class();
42
43
		return $field;
44
	}
45
46
	/**
47
	 * An alias of factory().
48
	 *
49
	 * @see Datastore::factory()
50
	 **/
51
	public static function make( $type ) {
52
		return self::factory( $type );
53
	}
54
}
55