Page_Middleware   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 30
c 0
b 0
f 0
dl 0
loc 89
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A add_to_loader() 0 2 1
B process() 0 39 9
A setup() 0 1 1
A tear_down() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Registration Middleware for dispatching pages.
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_Admin_Menu
23
 */
24
25
namespace PinkCrab\Perique_Admin_Menu\Module;
26
27
use PinkCrab\Loader\Hook_Loader;
28
use PinkCrab\Perique_Admin_Menu\Page\Page;
29
use PinkCrab\Perique_Admin_Menu\Group\Abstract_Group;
30
use PinkCrab\Perique\Interfaces\Registration_Middleware;
31
use PinkCrab\Perique_Admin_Menu\Registrar\Page_Dispatcher;
32
use PinkCrab\Perique_Admin_Menu\Validator\Group_Validator;
33
34
class Page_Middleware implements Registration_Middleware {
35
36
	public Page_Dispatcher $dispatcher;
37
	public Group_Validator $group_validator;
38
	public Hook_Loader $hook_loader;
39
40
	public function __construct(
41
		Page_Dispatcher $dispatcher,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 5 spaces but found 2
Loading history...
42
		Group_Validator $group_validator,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 5 spaces but found 2
Loading history...
43
		Hook_Loader $hook_loader
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 5 spaces but found 2
Loading history...
44
	) {
45
		$this->dispatcher      = $dispatcher;
46
		$this->group_validator = $group_validator;
47
		$this->hook_loader     = $hook_loader;
48
	}
49
50
	/**
51
	 * Add all valid ajax calls to the dispatcher.
52
	 *
53
	 * @param object $class_instance
54
	 * @return object
55
	 */
56
	public function process( object $class_instance ): object {
57
		// If we have a valid SUB page.
58
		if (
59
			is_a( $class_instance, Page::class )
60
			&& is_admin()
61
			&& ! is_null( $class_instance->parent_slug() )
62
		) {
63
			$this->add_to_loader(
64
				function () use ( $class_instance ): void {
65
					$this->dispatcher->register_subpage( $class_instance, $class_instance->parent_slug() );
0 ignored issues
show
Bug introduced by
It seems like $class_instance->parent_slug() can also be of type null; however, parameter $parent_slug of PinkCrab\Perique_Admin_M...her::register_subpage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
					$this->dispatcher->register_subpage( $class_instance, /** @scrutinizer ignore-type */ $class_instance->parent_slug() );
Loading history...
66
				}
67
			);
68
		}
69
70
		// If we have a valid SINGLE/PARENT page.
71
		if (
72
			is_a( $class_instance, Page::class )
73
			&& is_admin()
74
			&& is_null( $class_instance->parent_slug() )
75
		) {
76
			$this->add_to_loader(
77
				function () use ( $class_instance ): void {
78
					$this->dispatcher->register_single_page( $class_instance );
79
				}
80
			);
81
		}
82
83
		// If we have a valid group.
84
		if (
85
			is_a( $class_instance, Abstract_Group::class )
86
			&& is_admin()
87
		) {
88
			$this->add_to_loader(
89
				function () use ( $class_instance ): void {
90
					$this->dispatcher->register_group( $class_instance );
91
				}
92
			);
93
		}
94
		return $class_instance;
95
	}
96
97
	/**
98
	 * Adds a page to the Hook Loader
99
	 *
100
	 * @param callable $callback
101
	 * @return void
102
	 */
103
	protected function add_to_loader( callable $callback ): void {
104
		$this->hook_loader->action( 'admin_menu', $callback );
105
	}
106
107
108
	/**
109
	 * Registers the new page hook subscribers
110
	 * Creates the page on a custom callback.
111
	 *
112
	 * @return void
113
	 */
114
	public function setup(): void {}
115
116
	/**
117
	 * Register all ajax calls.
118
	 *
119
	 * @return void
120
	 */
121
	public function tear_down(): void {
122
		$this->hook_loader->register_hooks();
123
	}
124
}
125