Completed
Pull Request — staging (#840)
by
unknown
15:43
created

AssetsHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 3 1
A register() 0 5 2
A enqueue_handle() 0 6 2
A dequeue_handle() 0 6 2
A enqueue() 0 6 3
1
<?php
2
/**
3
 * YIKES Inc. Easy Mailchimp Forms Plugin.
4
 *
5
 * @package   YIKES\EasyForms
6
 * @author    Freddie Mixell
7
 * @license   GPL2
8
 */
9
10
namespace YIKES\EasyForms\Assets;
11
12
use YIKES\EasyForms\Exception\InvalidAssetHandle;
13
use YIKES\EasyForms\Registerable;
14
15
/**
16
 * Class AssetsHandler.
17
 *
18
 * @since   %VERSION%
19
 *
20
 * @package YIKES\EasyForms
21
 * @author  Freddie Mixell
22
 */
23
final class AssetsHandler implements Registerable {
24
25
	/**
26
	 * Assets known to this asset handler.
27
	 *
28
	 * @since %VERSION%
29
	 *
30
	 * @var Asset[]
31
	 */
32
	private $assets = [];
33
34
	/**
35
	 * Add a single asset to the asset handler.
36
	 *
37
	 * @since %VERSION%
38
	 *
39
	 * @param Asset $asset Asset to add.
40
	 */
41
	public function add( Asset $asset ) {
42
		$this->assets[ $asset->get_handle() ] = $asset;
43
	}
44
45
	/**
46
	 * Register the current Registerable.
47
	 *
48
	 * @since %VERSION%
49
	 */
50
	public function register() {
51
		foreach ( $this->assets as $asset ) {
52
			$asset->register();
53
		}
54
	}
55
56
	/**
57
	 * Enqueue a single asset based on its handle.
58
	 *
59
	 * @since %VERSION%
60
	 *
61
	 * @param string $handle Handle of the asset to enqueue.
62
	 *
63
	 * @throws InvalidAssetHandle If the passed-in asset handle is not valid.
64
	 */
65
	public function enqueue_handle( $handle ) {
66
		if ( ! array_key_exists( $handle, $this->assets ) ) {
67
			throw InvalidAssetHandle::from_handle( $handle );
68
		}
69
		$this->assets[ $handle ]->enqueue();
70
	}
71
72
	/**
73
	 * Dequeue a single asset based on its handle.
74
	 *
75
	 * @since %VERSION%
76
	 *
77
	 * @param string $handle Handle of the asset to enqueue.
78
	 *
79
	 * @throws InvalidAssetHandle If the passed-in asset handle is not valid.
80
	 */
81
	public function dequeue_handle( $handle ) {
82
		if ( ! array_key_exists( $handle, $this->assets ) ) {
83
			throw InvalidAssetHandle::from_handle( $handle );
84
		}
85
		$this->assets[ $handle ]->dequeue();
86
	}
87
88
	/**
89
	 * Enqueue all assets known to this asset handler.
90
	 *
91
	 * @since %VERSION%
92
	 *
93
	 * @param Asset|null $asset Optional. Asset to enqueue. If omitted, all
94
	 *                          known assets are enqueued.
95
	 */
96
	public function enqueue( Asset $asset = null ) {
97
		$assets = $asset ? [ $asset ] : $this->assets;
98
		foreach ( $assets as $asset_object ) {
99
			$asset_object->enqueue();
100
		}
101
	}
102
}
103