Passed
Pull Request — master (#16)
by Glynn
02:35
created

Menu_Page::enqueue()   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 1
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 $parent_slug;
39
40
	/**
41
	 * The pages menu slug.
42
	 *
43
	 * @var string
44
	 */
45
	protected $page_slug;
46
47
	/**
48
	 * The menu title
49
	 *
50
	 * @var string
51
	 */
52
	protected $menu_title;
53
54
	/**
55
	 * The pages title
56
	 *
57
	 * @var string
58
	 */
59
	protected $page_title;
60
61
	/**
62
	 * The pages position, in relation to other pages in group.
63
	 *
64
	 * @var int|null
65
	 */
66
	protected $position = null;
67
68
	/**
69
	 * The min capabilities required to access page.
70
	 *
71
	 * @var string
72
	 */
73
	protected $capability = 'manage_options';
74
75
	/**
76
	 * The template to be rendered.
77
	 *
78
	 * @var string
79
	 */
80
	protected $view_template;
81
82
	/**
83
	 * Data to be used to render the page.
84
	 *
85
	 * @var array<string, mixed>
86
	 */
87
	protected $view_data = array();
88
89
	/**
90
	 * View
91
	 *
92
	 * @var View
93
	 */
94
	protected $view;
95
96
	/**
97
	 * Set view
98
	 *
99
	 * @param View $view  View
100
	 * @return self
101
	 */
102
	public function set_view( View $view ): self {
103
		$this->view = $view;
104
		return $this;
105
	}
106
107
	/**
108
	 * @return string|null
109
	 */
110
	public function parent_slug(): ?string {
111
		return $this->parent_slug;
112
	}
113
114
	/**
115
	 * @return string
116
	 */
117
	public function slug(): string {
118
		if ( $this->page_slug === null ) {
119
			throw Page_Exception::undefined_property( 'page_slug', $this );
120
		}
121
		return $this->page_slug;
122
	}
123
124
	/**
125
	 * @return string
126
	 */
127
	public function menu_title(): string {
128
		if ( $this->menu_title === null ) {
129
			throw Page_Exception::undefined_property( 'menu_title', $this );
130
		}
131
		return $this->menu_title;
132
	}
133
134
	/**
135
	 * @return string|null
136
	 */
137
	public function page_title(): ?string {
138
		return $this->page_title;
139
	}
140
141
	/**
142
	 * @return int|null
143
	 */
144
	public function position(): ?int {
145
		return $this->position;
146
	}
147
148
	/**
149
	 * @return string
150
	 */
151
	public function capability(): string {
152
		return $this->capability;
153
	}
154
155
	/**
156
	 * Renders the page view.
157
	 *
158
	 * @return callable
159
	 * @throws Page_Exception code 200 if view not defined.
160
	 * @throws Page_Exception code 201 if template not defined.
161
	 */
162
	public function render_view(): callable {
163
		if ( null === $this->view ) {
164
			throw Page_Exception::view_not_set( $this );
165
		}
166
167
		if ( null === $this->view_template ) {
168
			throw Page_Exception::undefined_property( 'view_template', $this );
169
		}
170
171
		return function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
172
			$this->view->render( $this->view_template, $this->view_data );
173
		};
174
175
	}
176
177
	/**
178
	 * Callback for enqueuing scripts and styles at a page level.
179
	 *
180
	 * @param Page $page
181
	 * @return void
182
	 * @codeCoverageIgnore This can be tested as it does nothing and is extended only
183
	 */
184
	public function enqueue( Page $page ): void {
185
		// Do nothing.
186
		// Can be extended in any child class that extends.
187
	}
188
189
	/**
190
	 * Callback for the pre-load of the page
191
	 *
192
	 * @param Page $page
193
	 * @return void
194
	 * @codeCoverageIgnore This can be tested as it does nothing and is extended only
195
	 */
196
	public function load( Page $page ): void {
197
		// Do nothing.
198
		// Can be extended in any child class that extends.
199
	}
200
201
202
}
203