Registrar_Factory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 13
c 1
b 0
f 0
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A meta_box_registrar() 0 2 1
A new() 0 2 1
A create_from_registerable() 0 11 3
A meta_data_registrar() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Factory for creating Dispatchers
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
 */
24
25
namespace PinkCrab\Registerables\Registrar;
26
27
use Exception;
28
use PinkCrab\Loader\Hook_Loader;
29
use PinkCrab\Registerables\Taxonomy;
30
use PinkCrab\Registerables\Post_Type;
31
use PinkCrab\Perique\Interfaces\DI_Container;
32
use PinkCrab\Registerables\Registrar\Taxonomy_Registrar;
33
use PinkCrab\Registerables\Validator\Meta_Box_Validator;
34
use PinkCrab\Registerables\Validator\Taxonomy_Validator;
35
use PinkCrab\Registerables\Registrar\Post_Type_Registrar;
36
use PinkCrab\Registerables\Validator\Post_Type_Validator;
37
use PinkCrab\Registerables\Module\Middleware\Registerable;
38
39
class Registrar_Factory {
40
41
	/**
42
	 * Returns an instance of the factory.
43
	 *
44
	 * @return self
45
	 */
46
	public static function new(): self {
47
		return new self();
48
	}
49
50
	/**
51
	 * Creates the dispatcher based on the registerable passed.
52
	 *
53
	 * @param \PinkCrab\Registerables\Module\Middleware\Registerable $registerable
54
	 * @return Registrar
55
	 * @throws Exception If not valid registerable type passed.
56
	 */
57
	public function create_from_registerable( Registerable $registerable ): Registrar {
58
		switch ( true ) {
59
			case is_a( $registerable, Post_Type::class ):
60
				return new Post_Type_Registrar( new Post_Type_Validator(), new Meta_Data_Registrar() );
61
62
			case is_a( $registerable, Taxonomy::class ):
63
				return new Taxonomy_Registrar( new Taxonomy_Validator(), new Meta_Data_Registrar() );
64
65
			default:
66
				$type = get_class( $registerable );
67
				throw new Exception( esc_html( 'Invalid registerable (' . $type . ')type (no dispatcher exists)' ) );
68
		}
69
	}
70
71
	/**
72
	 * Returns an instance of the meta box registrar.
73
	 *
74
	 * @param \PinkCrab\Perique\Interfaces\DI_Container $container
75
	 * @param \PinkCrab\Loader\Hook_Loader              $loader
76
	 * @return Meta_Box_Registrar
77
	 */
78
	public function meta_box_registrar( DI_Container $container, Hook_Loader $loader ): Meta_Box_Registrar {
79
		return new Meta_Box_Registrar( new Meta_Box_Validator(), $container, $loader );
80
	}
81
82
	/**
83
	 * Returns and instance of the Meta Data registrar.
84
	 *
85
	 * @return Meta_Data_Registrar
86
	 */
87
	public function meta_data_registrar(): Meta_Data_Registrar {
88
		return new Meta_Data_Registrar();
89
	}
90
}
91