Completed
Push — master ( 722949...84d505 )
by J.D.
27:47 queued 26:57
created

WordPoints_Class_Registry   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 110
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_all() 0 3 1
A get_all_slugs() 0 3 1
A get() 0 19 3
A register() 0 6 1
A deregister() 0 4 1
A is_registered() 0 4 1
B construct_with_args() 0 24 4
1
<?php
2
3
/**
4
 * Class registry class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since   1.0.0
8
 */
9
10
/**
11
 * A class registry which creates new objects on-the-fly as they are requested.
12
 *
13
 * In other words, each time get() is called, a new object will be returned.
14
 *
15
 * Objects are passed the slug as the first parameter when they are constructed.
16
 *
17
 * @since 1.0.0
18
 */
19
class WordPoints_Class_Registry implements WordPoints_Class_RegistryI {
20
21
	/**
22
	 * The registered classes, indexed by slug.
23
	 *
24
	 * @since 1.0.0
25
	 *
26
	 * @var string[]
27
	 */
28
	protected $classes = array();
29
30
	/**
31
	 * @since 1.0.0
32
	 */
33
	public function get_all( array $args = array() ) {
34
		return self::construct_with_args( $this->classes, $args );
35
	}
36
37
	/**
38
	 * @since 1.0.0
39
	 */
40
	public function get_all_slugs() {
41
		return array_keys( $this->classes );
42
	}
43
44
	/**
45
	 * @since 1.0.0
46
	 */
47
	public function get( $slug, array $args = array() ) {
48
49
		if ( ! isset( $this->classes[ $slug ] ) ) {
50
			return false;
51
		}
52
53
		if ( ! empty( $args ) ) {
54
55
			array_unshift( $args, $slug );
56
57
			return wordpoints_construct_class_with_args(
58
				$this->classes[ $slug ]
59
				, $args
60
			);
61
62
		} else {
63
			return new $this->classes[ $slug ]( $slug );
64
		}
65
	}
66
67
	/**
68
	 * @since 1.0.0
69
	 */
70
	public function register( $slug, $class, array $args = array() ) {
71
72
		$this->classes[ $slug ] = $class;
73
74
		return true;
75
	}
76
77
	/**
78
	 * @since 1.0.0
79
	 */
80
	public function deregister( $slug ) {
81
82
		unset( $this->classes[ $slug ] );
83
	}
84
85
	/**
86
	 * @since 1.0.0
87
	 */
88
	public function is_registered( $slug ) {
89
90
		return isset( $this->classes[ $slug ] );
91
	}
92
93
	/**
94
	 * Construct an array of classes with the given arguments.
95
	 *
96
	 * @since 1.0.0
97
	 *
98
	 * @param string[] $classes The classes, indexed by slug (which will be the first
99
	 *                          arg passed to the constructor before the other args).
100
	 * @param array    $args    An array of args to pass to the class constructor.
101
	 *
102
	 * @return object[] An array of the constructed objects.
103
	 */
104
	public static function construct_with_args( array $classes, array $args ) {
105
106
		$objects = array();
107
108
		if ( empty( $args ) ) {
109
110
			foreach ( $classes as $slug => $class ) {
111
				$objects[ $slug ] = new $class( $slug );
112
			}
113
114
		} else {
115
116
			array_unshift( $args, null );
117
118
			foreach ( $classes as $slug => $class ) {
119
				$objects[ $slug ] = wordpoints_construct_class_with_args(
120
					$class
121
					, array( $slug ) + $args
122
				);
123
			}
124
		}
125
126
		return $objects;
127
	}
128
}
129
130
// EOF
131