Completed
Push — master ( 874476...6bd4da )
by Glynn
33s queued 12s
created

Additional_Meta_Data_Registrar::__construct()   A

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\Additional_Meta_Data_Controller;
33
use PinkCrab\Registerables\Tests\Fixtures\Additional_Meta_Data;
34
use PinkCrab\Registerables\Registration_Middleware\Registerable;
35
36
class Additional_Meta_Data_Registrar implements Registrar {
37
38
39
	/**
40
	 * The Meta Data Registrar
41
	 *
42
	 * @var Meta_Data_Registrar
43
	 */
44
	protected $meta_data_registrar;
45
46
	public function __construct( Meta_Data_Registrar $meta_data_registrar ) {
47
		$this->meta_data_registrar = $meta_data_registrar;
48
	}
49
50
	/**
51
	 * Used to register a registerable
52
	 *
53
	 * @param \PinkCrab\Registerables\Registration_Middleware\Registerable $registerable
54
	 * @return void
55
	 * @throws Exception If either post or term meta and the post type or taxonomy are not registered.
56
	 * @throws Exception If a none Additional_Meta_Data_Controller registerable is attempted to be registered.
57
	 * @throws Exception If a meta type which is not POST, USER, TERM or COMMENT is attempted to be registered.
58
	 */
59
	public function register( Registerable $registerable ): void {
60
		if ( ! is_a( $registerable, Additional_Meta_Data_Controller::class ) ) {
61
			throw new Exception( 'Registerable must be an instance of Additional_Meta_Data_Controller' );
62
		}
63
64
		/** @var Additional_Meta_Data $registerable, Validation call below catches no Additional_Meta_Data Registerables */
65
		$meta_data = $this->filter_meta_data( $registerable->meta_data( array() ) );
66
67
		// Iterate through all meta data and register them.
68
		foreach ( $meta_data as $meta_data_item ) {
69
			switch ( $meta_data_item->get_meta_type() ) {
70
				case 'post':
71
					// Throw if post type not defined.
72
					if ( null === $meta_data_item->get_subtype() ) {
73
						throw new Exception(
74
							sprintf(
75
								'A post type must be defined when attempting to register post meta with meta key : %s',
76
								$meta_data_item->get_meta_key()
77
							)
78
						);
79
					}
80
81
					$this->meta_data_registrar->register_for_post_type(
82
						$meta_data_item,
83
						$meta_data_item->get_subtype()
84
					);
85
					break;
86
87
				case 'term':
88
					// Throw if Taxonomy not defined.
89
					if ( null === $meta_data_item->get_subtype() ) {
90
						throw new Exception(
91
							sprintf(
92
								'A taxonomy must be defined when attempting to register tern meta with meta key : %s',
93
								$meta_data_item->get_meta_key()
94
							)
95
						);
96
					}
97
98
					$this->meta_data_registrar->register_for_term(
99
						$meta_data_item,
100
						$meta_data_item->get_subtype()
101
					);
102
					break;
103
104
				case 'user':
105
					$this->meta_data_registrar->register_for_user(
106
						$meta_data_item
107
					);
108
					break;
109
110
				case 'comment':
111
					$this->meta_data_registrar->register_for_comment(
112
						$meta_data_item
113
					);
114
					break;
115
116
				default:
117
					throw new Exception( 'Unexpected meta type' );
118
			}
119
		}
120
	}
121
122
	/**
123
	 * Filters all non meta data from array.
124
	 *
125
	 * @param mixed[] $meta_data
126
	 * @return Meta_Data[]
127
	 */
128
	protected function filter_meta_data( array $meta_data ): array {
129
		return array_filter(
130
			$meta_data,
131
			function( $e ) {
132
				return is_a( $e, Meta_Data::class );
133
			}
134
		);
135
	}
136
}
137