Additional_Meta_Data_Registrar::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Registration Registrar for Shared Meta Boxes.
7
 *
8
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
 *
20
 * @author Glynn Quelch <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @package PinkCrab\Registerables
23
 * @since 0.7.0
24
 */
25
26
namespace PinkCrab\Registerables\Registrar;
27
28
use Exception;
29
use PinkCrab\Registerables\Meta_Data;
30
use PinkCrab\Registerables\Registrar\Registrar;
31
use PinkCrab\Registerables\Registrar\Meta_Data_Registrar;
32
use PinkCrab\Registerables\Module\Middleware\Registerable;
33
use PinkCrab\Registerables\Additional_Meta_Data_Controller;
34
use PinkCrab\Registerables\Tests\Fixtures\Additional_Meta_Data;
35
36
class Additional_Meta_Data_Registrar implements Registrar {
37
38
	protected Meta_Data_Registrar $meta_data_registrar;
39
40
	public function __construct( Meta_Data_Registrar $meta_data_registrar ) {
41
		$this->meta_data_registrar = $meta_data_registrar;
42
	}
43
44
	/**
45
	 * Used to register a registerable
46
	 *
47
	 * @param \PinkCrab\Registerables\Module\Middleware\Registerable $registerable
48
	 * @return void
49
	 * @throws Exception If either post or term meta and the post type or taxonomy are not registered.
50
	 * @throws Exception If a none Additional_Meta_Data_Controller registerable is attempted to be registered.
51
	 * @throws Exception If a meta type which is not POST, USER, TERM or COMMENT is attempted to be registered.
52
	 */
53
	public function register( Registerable $registerable ): void {
54
		if ( ! is_a( $registerable, Additional_Meta_Data_Controller::class ) ) {
55
			throw new Exception( 'Registerable must be an instance of Additional_Meta_Data_Controller' );
56
		}
57
58
		/**
59
 * @var Additional_Meta_Data $registerable, Validation call below catches no Additional_Meta_Data Registerables
60
*/
61
		$meta_data = $this->filter_meta_data( $registerable->meta_data( array() ) );
62
63
		// Iterate through all meta data and register them.
64
		foreach ( $meta_data as $meta_data_item ) {
65
			switch ( $meta_data_item->get_meta_type() ) {
66
				case 'post':
67
					// Throw if post type not defined.
68
					if ( null === $meta_data_item->get_subtype() ) {
69
						throw new Exception(
70
							sprintf(
71
								'A post type must be defined when attempting to register post meta with meta key : %s',
72
								esc_attr( $meta_data_item->get_meta_key() )
73
							)
74
						);
75
					}
76
77
					$this->meta_data_registrar->register_for_post_type(
78
						$meta_data_item,
79
						$meta_data_item->get_subtype()
80
					);
81
					break;
82
83
				case 'term':
84
					// Throw if Taxonomy not defined.
85
					if ( null === $meta_data_item->get_subtype() ) {
86
						throw new Exception(
87
							sprintf(
88
								'A taxonomy must be defined when attempting to register tern meta with meta key : %s',
89
								esc_attr( $meta_data_item->get_meta_key() )
90
							)
91
						);
92
					}
93
94
					$this->meta_data_registrar->register_for_term(
95
						$meta_data_item,
96
						$meta_data_item->get_subtype()
97
					);
98
					break;
99
100
				case 'user':
101
					$this->meta_data_registrar->register_for_user(
102
						$meta_data_item
103
					);
104
					break;
105
106
				case 'comment':
107
					$this->meta_data_registrar->register_for_comment(
108
						$meta_data_item
109
					);
110
					break;
111
112
				default:
113
					throw new Exception( 'Unexpected meta type' );
114
			}
115
		}
116
	}
117
118
	/**
119
	 * Filters all non meta data from array.
120
	 *
121
	 * @param mixed[] $meta_data
122
	 * @return Meta_Data[]
123
	 */
124
	protected function filter_meta_data( array $meta_data ): array {
125
		return array_filter(
126
			$meta_data,
127
			function ( $e ) {
128
				return is_a( $e, Meta_Data::class );
129
			}
130
		);
131
	}
132
}
133