Passed
Pull Request — master (#43)
by Glynn
07:58
created

Page_Middleware::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 1
rs 10
cc 1
nc 1
nop 0
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
54
	 * @return object
55
	 */
56
	public function process( object $class ): object {
57
		// If we have a valid SUB page.
58
		if (
59
			is_a( $class, Page::class )
60
			&& is_admin()
61
			&& ! is_null( $class->parent_slug() )
62
		) {
63
			$this->add_to_loader(
64
				function () use ( $class ) : void {
65
					$this->dispatcher->register_subpage( $class, $class->parent_slug() );
0 ignored issues
show
Bug introduced by
It seems like $class->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, /** @scrutinizer ignore-type */ $class->parent_slug() );
Loading history...
66
				}
67
			);
68
		}
69
70
		// If we have a valid SINGLE/PARENT page.
71
		if (
72
			is_a( $class, Page::class )
73
			&& is_admin()
74
			&& is_null( $class->parent_slug() )
75
		) {
76
			$this->add_to_loader(
77
				function () use ( $class ) : void {
78
					$this->dispatcher->register_single_page( $class );
79
				}
80
			);
81
		}
82
83
		// If we have a valid group.
84
		if (
85
			is_a( $class, Abstract_Group::class )
86
			&& is_admin()
87
		) {
88
89
			$this->add_to_loader(
90
				function () use ( $class ): void {
91
					$this->dispatcher->register_group( $class );
92
				}
93
			);
94
		}
95
		return $class;
96
	}
97
98
	/**
99
	 * Adds a page to the Hook Loader
100
	 *
101
	 * @param callable $callback
102
	 * @return void
103
	 */
104
	protected function add_to_loader( callable $callback ): void {
105
		$this->hook_loader->action( 'admin_menu', $callback );
106
	}
107
108
109
	/**
110
	 * Registers the new page hook subscribers
111
	 * Creates the page on a custom callback.
112
	 *
113
	 * @return void
114
	 */
115
	public function setup(): void {}
116
117
	/**
118
	 * Register all ajax calls.
119
	 *
120
	 * @return void
121
	 */
122
	public function tear_down(): void {
123
		$this->hook_loader->register_hooks();
124
	}
125
}
126