AbstractDependencyHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 98
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 3 1
get_register_function() 0 1 ?
A enqueue() 0 8 3
get_enqueue_function() 0 1 ?
A maybe_enqueue() 0 12 3
is_enqueued() 0 1 ?
is_registered() 0 1 ?
1
<?php
2
/**
3
 * Bright Nucleus Dependency Component.
4
 *
5
 * @package   BrightNucleus\Dependency
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2015-2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Dependency;
13
14
use BrightNucleus\Exception\InvalidArgumentException;
15
use BrightNucleus\Invoker\FunctionInvokerTrait;
16
17
/**
18
 * Abstract class AbstractDependencyHandler.
19
 *
20
 * @since   0.1.0
21
 *
22
 * @package BrightNucleus\Dependency
23
 */
24
abstract class AbstractDependencyHandler implements DependencyHandlerInterface {
25
26
	use FunctionInvokerTrait;
27
28
	/**
29
	 * Register the dependency's assets.
30
	 *
31
	 * @since 0.1.0
32
	 *
33
	 * @param array|null $args          Optional. Array of arguments that is
34
	 *                                  passed to the registration function.
35
	 * @throws InvalidArgumentException If the register function could not be
36
	 *                                  called.
37
	 */
38
	public function register( $args = null ) {
39
		$this->invokeFunction( $this->get_register_function(), (array) $args );
40
	}
41
42
	/**
43
	 * Get the name of the function that is used for registering the dependency.
44
	 *
45
	 * @since 0.1.0
46
	 *
47
	 * @return string Function name.
48
	 */
49
	abstract protected function get_register_function();
50
51
	/**
52
	 * Enqueue the dependency's assets.
53
	 *
54
	 * @since 0.1.0
55
	 *
56
	 * @param array|null $args          Optional. Array of arguments that is
57
	 *                                  passed to the enqueueing function.
58
	 * @throws InvalidArgumentException If the register function could not be
59
	 *                                  called.
60
	 */
61
	public function enqueue( $args = null ) {
62
		if ( array_key_exists( 'handle', $args )
63
		     && $this->is_enqueued( $args['handle'] ) ) {
64
			return;
65
		}
66
67
		$this->invokeFunction( $this->get_enqueue_function(), (array) $args );
68
	}
69
70
	/**
71
	 * Get the name of the function that is used for enqueueing the dependency.
72
	 *
73
	 * @since 0.1.0
74
	 *
75
	 * @return string Function name.
76
	 */
77
	abstract protected function get_enqueue_function();
78
79
	/**
80
	 * Maybe enqueue a dependency that has been registered outside of the
81
	 * Dependency Manager.
82
	 *
83
	 * @since 0.2.3
84
	 *
85
	 * @param string $handle Handle of the dependency to enqueue.
86
	 * @return bool Whether the handle was found or not.
87
	 */
88
	public function maybe_enqueue( $handle ) {
89
		if ( $this->is_enqueued( $handle ) ) {
90
			return true;
91
		}
92
93
		if ( $this->is_registered( $handle ) ) {
94
			$enqueue = $this->get_enqueue_function();
95
			$enqueue( $handle );
96
			return true;
97
		}
98
		return false;
99
	}
100
101
	/**
102
	 * Check whether a specific handle has been enqueued.
103
	 *
104
	 * @since 0.3.3
105
	 *
106
	 * @param string $handle The handle to check
107
	 * @return bool Whether it is enqueued or not.
108
	 */
109
	abstract public function is_enqueued( $handle );
110
111
	/**
112
	 * Check whether a specific handle has been registered.
113
	 *
114
	 * @since 0.2.3
115
	 * @since 0.3.3 Publicly accessible.
116
	 *
117
	 * @param string $handle The handle to check
118
	 * @return bool Whether it is registered or not.
119
	 */
120
	abstract public function is_registered( $handle );
121
}
122