Registration_Service::process()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 26
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Registration loader
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\Perique\Registration
23
 */
24
25
namespace PinkCrab\Perique\Services\Registration;
26
27
use PinkCrab\Loader\Hook_Loader;
28
use PinkCrab\Perique\Application\Hooks;
29
use PinkCrab\Perique\Interfaces\DI_Container;
30
use PinkCrab\Perique\Interfaces\Registration_Middleware;
31
use PinkCrab\Perique\Exceptions\Module_Manager_Exception;
32
33
class Registration_Service {
34
35
	/**
36
	 * Holds all the defined registration middlewares
37
	 *
38
	 * @var array<Registration_Middleware>
39
	 */
40
	protected array $middleware = array();
41
42
	/**
43
	 * Holds all classes that are to be registered.
44
	 *
45
	 * @var array<string>
46
	 */
47
	protected array $class_list = array();
48
49
	/**
50
	 * Access to the DI Container
51
	 *
52
	 * @var DI_Container
53
	 */
54
	protected DI_Container $di_container;
55
56
	public function __construct( DI_Container $di_container ) {
57
		$this->di_container = $di_container;
58
	}
59
60
	/**
61
	 * Pushes a piece of middleware to the collection.
62
	 *
63
	 * @param Registration_Middleware $middleware
64
	 * @return self
65
	 */
66
	public function push_middleware( Registration_Middleware $middleware ): self {
67
		$this->middleware[ \get_class( $middleware ) ] = $middleware;
68
		return $this;
69
	}
70
71
	/**
72
	 * Adds a class to the list of classes to be registered.
73
	 *
74
	 * @template Class_Name of object
75
	 * @param class-string<Class_Name> $class_string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Class_Name> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Class_Name>.
Loading history...
76
	 */
77
	public function push_class( string $class_string ): self {
78
		// If the class is already in the list, skip.
79
		if ( \in_array( $class_string, $this->class_list, true ) ) {
80
			return $this;
81
		}
82
83
		// If $class_string is not a class, throw exception.
84
		if ( ! \class_exists( $class_string ) ) {
85
			throw Module_Manager_Exception::none_class_string_passed_to_registration( esc_html( $class_string ) );
86
		}
87
88
		$this->class_list[] = $class_string;
89
		return $this;
90
	}
91
92
	/**
93
	 * Runs all the defined classes through the middleware stack.
94
	 *
95
	 * @return void
96
	 */
97
	public function process(): void {
98
		// Filter all classes, before processing.
99
		$class_list = apply_filters( Hooks::APP_INIT_REGISTRATION_CLASS_LIST, $this->class_list );
100
101
		// If class list is empty, skip.
102
		if ( empty( $class_list ) ) {
103
			return;
104
		}
105
106
		foreach ( $this->middleware as $middleware ) {
107
			// Run middleware setup
108
			$middleware->setup();
109
110
			// Pass each class to the middleware.
111
			foreach ( $class_list as $class ) {
112
				// Construct class using container,
113
				$class_instance = $this->di_container->create( $class );
114
115
				// if valid object process via current middleware
116
				if ( is_object( $class_instance ) ) {
117
					$middleware->process( $class_instance );
118
				}
119
			}
120
121
			// Run middleware setup
122
			$middleware->tear_down();
123
		}
124
	}
125
}
126