Menu_Page   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 31
c 1
b 0
f 0
dl 0
loc 194
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A parent_slug() 0 2 1
A page_title() 0 2 1
A slug() 0 5 2
A page_hook() 0 2 1
A set_page_hook() 0 2 1
A set_view() 0 3 1
A enqueue() 0 1 1
A load() 0 1 1
A capability() 0 2 1
A menu_title() 0 5 2
A position() 0 2 1
A render_view() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * The abstract class used to create Page Groups within WP-Admin
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\Page;
26
27
use PinkCrab\Perique_Admin_Menu\Page\Page;
28
use PinkCrab\Perique\Services\View\View;
29
use PinkCrab\Perique_Admin_Menu\Exception\Page_Exception;
30
31
abstract class Menu_Page implements Page {
32
33
	/**
34
	 * The pages menu slug.
35
	 *
36
	 * @var string|null
37
	 */
38
	protected ?string $parent_slug = null;
39
40
	/**
41
	 * The pages menu slug.
42
	 *
43
	 * @var string
44
	 */
45
	protected string $page_slug = '';
46
47
	/**
48
	 * The menu title
49
	 *
50
	 * @var string
51
	 */
52
	protected string $menu_title = '';
53
54
	/**
55
	 * The pages title
56
	 *
57
	 * @var string
58
	 */
59
	protected string $page_title = '';
60
61
	/**
62
	 * The pages position, in relation to other pages in group.
63
	 *
64
	 * @var int|null
65
	 */
66
	protected ?int $position = null;
67
68
	/**
69
	 * The min capabilities required to access page.
70
	 *
71
	 * @var string
72
	 */
73
	protected string $capability = 'manage_options';
74
75
	/**
76
	 * The template to be rendered.
77
	 *
78
	 * @var string
79
	 */
80
	protected string $view_template = '';
81
82
	/**
83
	 * Data to be used to render the page.
84
	 *
85
	 * @var array<string, mixed>
86
	 */
87
	protected array $view_data = array();
88
89
	/**
90
	 * Holds the page hook.
91
	 *
92
	 * @var ?string
93
	 */
94
	protected ?string $page_hook = null;
95
96
	/**
97
	 * View
98
	 *
99
	 * @var View
100
	 */
101
	protected ?View $view = null;
102
103
	/**
104
	 * Set view
105
	 *
106
	 * @param View $view  View
107
	 * @return self
108
	 */
109
	public function set_view( View $view ): self {
110
		$this->view = $view;
111
		return $this;
112
	}
113
114
	/**
115
	 * @return string|null
116
	 */
117
	public function parent_slug(): ?string {
118
		return $this->parent_slug;
119
	}
120
121
	/**
122
	 * @return string
123
	 */
124
	public function slug(): string {
125
		if ( $this->page_slug === '' ) {
126
			throw Page_Exception::undefined_property( 'page_slug', $this ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped, escaped in exception.
127
		}
128
		return $this->page_slug;
129
	}
130
131
	/**
132
	 * @return string
133
	 */
134
	public function menu_title(): string {
135
		if ( $this->menu_title === '' ) {
136
			throw Page_Exception::undefined_property( 'menu_title', $this ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped, escaped in exception.
137
		}
138
		return $this->menu_title;
139
	}
140
141
	/**
142
	 * @return string|null
143
	 */
144
	public function page_title(): ?string {
145
		return $this->page_title;
146
	}
147
148
	/**
149
	 * @return int|null
150
	 */
151
	public function position(): ?int {
152
		return $this->position;
153
	}
154
155
	/**
156
	 * @return string
157
	 */
158
	public function capability(): string {
159
		return $this->capability;
160
	}
161
162
	/**
163
	 * Renders the page view.
164
	 *
165
	 * @return callable
166
	 * @throws Page_Exception code 200 if view not defined.
167
	 * @throws Page_Exception code 201 if template not defined.
168
	 */
169
	public function render_view(): callable {
170
		if ( null === $this->view ) {
171
			throw Page_Exception::view_not_set( $this ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped, escaped in exception.
172
		}
173
174
		if ( '' === $this->view_template ) {
175
			throw Page_Exception::undefined_property( 'view_template', $this ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped, escaped in exception.
176
		}
177
178
		return function () {
179
			// @phpstan-ignore-next-line, as we have already checked for null.
180
			$this->view->render( $this->view_template, $this->view_data );
0 ignored issues
show
Bug introduced by
The method render() does not exist on null. ( Ignorable by Annotation )

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

180
			$this->view->/** @scrutinizer ignore-call */ 
181
                render( $this->view_template, $this->view_data );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
181
		};
182
	}
183
184
	/**
185
	 * Callback for enqueuing scripts and styles at a page level.
186
	 *
187
	 * @param Page $page
188
	 * @return void
189
	 * @codeCoverageIgnore This can be tested as it does nothing and is extended only
190
	 */
191
	public function enqueue( Page $page ): void {
192
		// Do nothing.
193
		// Can be extended in any child class that extends.
194
	}
195
196
	/**
197
	 * Callback for the pre-load of the page
198
	 *
199
	 * @param Page $page
200
	 * @return void
201
	 * @codeCoverageIgnore This can be tested as it does nothing and is extended only
202
	 */
203
	public function load( Page $page ): void {
204
		// Do nothing.
205
		// Can be extended in any child class that extends.
206
	}
207
208
	/**
209
	 * Sets the page hook
210
	 *
211
	 * @param string $page_hook
212
	 * @return void
213
	 */
214
	final public function set_page_hook( string $page_hook ): void {
215
		$this->page_hook = $page_hook;
216
	}
217
218
	/**
219
	 * Gets the page hook
220
	 *
221
	 * @return string|null
222
	 */
223
	final public function page_hook(): ?string {
224
		return $this->page_hook;
225
	}
226
}
227