Test Failed
Push — master ( 1e2594...8f7b3b )
by Justin
04:05
created

Page   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 277
Duplicated Lines 0 %

Importance

Changes 16
Bugs 7 Features 4
Metric Value
wmc 40
c 16
b 7
f 4
dl 0
loc 277
rs 8.2608

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getGlobalMenuID() 0 2 1
A getContent() 0 2 1
A hasCustomPermissions() 0 2 1
A getLocalMenuID() 0 2 1
A getStyle() 0 2 1
A getPageType() 0 2 1
A getCustomTemplate() 0 2 1
A hasCustomTemplate() 0 2 1
A isPublished() 0 2 1
A getLastEdit() 0 2 1
A listCustomPermissions() 0 2 1
A reloadCache() 0 2 1
A getFolder() 0 2 1
A getAlias() 0 2 1
A __construct() 0 1 1
A getPageID() 0 2 1
A getDomain() 0 2 1
A createIfAbsent() 0 55 4
A getContentType() 0 2 1
A getPageIDByAlias() 0 8 2
A get() 0 5 1
A setPageType() 0 12 1
A deleteByID() 0 14 2
A activate() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like Page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Page, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
class Page {
20
21
	protected $alias = null;
22
	protected $row = null;
23
	protected $pagetype = "";
24
25
	public function __construct() {
26
		//
27
	}
28
29
	public function load ($alias = null) {
30
		if ($alias == null) {
31
			if (isset($_REQUEST['page']) && !empty($_REQUEST['page'])) {
32
				$alias = Validator_String::get($_REQUEST['page']);
33
			} else {
34
				$alias = $this->getDomain()->getHomePage();
35
			}
36
		} else {
37
			//$alias = Database::getInstance()->escape($alias);
38
		}
39
40
		Events::throwEvent("get_alias", array(
41
			'alias' => &$alias,
42
			'page' => &$this,
43
			'domain' => $this->getDomain()
44
		));
45
46
		$this->alias = $alias;
47
48
		if (Cache::contains("pages", "page_" . $alias)) {
49
			$this->row = Cache::get("pages", "page_" . $alias);
50
		} else {
51
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias AND `activated` = '1'; ", array('alias' => $alias));
52
53
			if (!$row) {
54
				if (PHPUtils::strEqs("error404", $alias)) {
55
					throw new IllegalStateException("No page with alias 'error404' exists (requested alias: " . $alias . ").");
56
				}
57
58
				//page not found
59
				$new_alias = "error404";
60
61
				Events::throwEvent("load_page_error404", array(
62
					'alias' => &$new_alias,
63
					'original_alias' => $alias,
64
					'page' => &$this,
65
					'domain' => $this->getDomain()
66
				));
67
68
				$this->load($new_alias);
69
				return null;
70
			}
71
72
			$this->row = $row;
73
74
			//cache result
75
			Cache::put("pages", "page_" . $alias, $row);
76
		}
77
78
		//get name of page type (class name)
79
		$this->pagetype = $this->row['page_type'];
80
	}
81
82
	protected function getDomain () : Domain {
83
		return Registry::singleton()->getObject("domain");
84
	}
85
86
	public function reloadCache () {
87
		Cache::clear("pages");
88
	}
89
90
	public function getPageID () : int {
91
		return $this->row['id'];
92
	}
93
94
	public function getAlias () : string {
95
		return $this->alias;
96
	}
97
98
	public function getPageType () : string {
99
		return $this->pagetype;
100
	}
101
102
	public function getTitle () : string {
103
		return $this->row['title'];
104
	}
105
106
	public function getContent () : string {
107
		return $this->row['content'];
108
	}
109
110
	public function getGlobalMenuID () : int {
111
		return $this->row['global_menu'];
112
	}
113
114
	public function getLocalMenuID () : int {
115
		return $this->row['local_menu'];
116
	}
117
118
	public function getStyle () : string {
119
		return $this->row['design'];
120
	}
121
122
	public function getFolder () : string {
123
		return $this->row['folder'];
124
	}
125
126
	public function getLastEdit () {
127
		return $this->row['lastUpdate'];
128
	}
129
130
	public function hasCustomTemplate () : bool {
131
		return $this->row['template'] !== "none";
132
	}
133
134
	public function getCustomTemplate () : string {
135
		return $this->row['template'];
136
	}
137
138
	public function hasCustomPermissions () : bool {
139
		return $this->row['can_see_permissions'] !== "none";
140
	}
141
142
	public function listCustomPermissions () : array {
143
		return explode("|", $this->row['can_see_permissions']);
144
	}
145
146
	public function isPublished () : bool {
147
		return $this->row['published'] == 1;
148
	}
149
150
	public function getContentType () : string {
151
		return $this->row['content_type'];
152
	}
153
154
	public function activate (bool $bool = true) {
155
		$this->row['activated'] = $bool;
156
	}
157
158
	/**
159
	 * save changes into database
160
	 */
161
	public function save () {
162
		//TODO: add code here
163
	}
164
165
	public static function createIfAbsent (string $alias, string $title, string $page_type, string $content = "", string $folder = "/", int $globalMenu = -1, int $localMenu = -1, int $parentID = -1, bool $sitemap = true, bool $published = true, bool $editable = true, string $author = "system") : int {
166
		//throw event
167
		Events::throwEvent("create_page", array(
168
			'alias' => &$alias,
169
			'title' => &$title,
170
			'page_type' => &$page_type,
171
			'content' => &$content,
172
			'folder' => &$folder,
173
			'global_menu' => &$globalMenu,
174
			'local_menu' => &$localMenu,
175
			'parentID' => &$parentID,
176
			'sitemap' => &$sitemap,
177
			'published' => &$published,
178
			'editable' => &$editable,
179
			'author' => &$author
180
		));
181
182
		Database::getInstance()->execute("INSERT INTO `{praefix}pages` (
183
			`id`, `alias`, `title`, `content`, `parent`, `folder`, `global_menu`, `local_menu`, `page_type`, `design`, `sitemap`, `published`, `version`, `last_update`, `created`, `editable`, `author`, `activated`
184
		) VALUES (
185
			NULL, :alias, :title, :content, :parent, :folder, :globalMenu, :localMenu, :pageType, 'none', :sitemap, :published, '1', '0000-00-00 00:00:00', CURRENT_TIMESTAMP, :editable, :author, '1'
186
		) ON DUPLICATE KEY UPDATE `alias` = :alias; ", array(
187
			'alias' => $alias,
188
			'title' => $title,
189
			'content' => $content,
190
			'parent' => $parentID,
191
			'folder' => $folder,
192
			'globalMenu' => $globalMenu,
193
			'localMenu' => $localMenu,
194
			'pageType' => $page_type,
195
			'sitemap' => ($sitemap ? 1 : 0),
196
			'published' => ($published ? 1 : 0),
197
			'editable' => ($editable ? 1 : 0),
198
			'author' => $author
199
		));
200
201
		Cache::clear("pages");
202
203
		//return page id
204
		$insertID = Database::getInstance()->lastInsertId();
205
206
		//throw event
207
		Events::throwEvent("created_page", array(
208
			'alias' => $alias,
209
			'title' => $title,
210
			'insertID' => $insertID
211
		));
212
213
		//get pageID by alias
214
		$pageID = Page::getPageIDByAlias($alias);
215
216
		//set default rights, allow page for administrators, registered users, guests and bots
217
		PageRights::setDefaultAllowedGroups($pageID, array(1, 2, 3, 4));
218
219
		return $pageID;
220
	}
221
222
	public static function delete (string $alias) {
223
		$delete = true;
224
225
		//plugins can avoid deletion or change alias
226
		Events::throwEvent("delete_page_alias", array(
227
			'alias' => &$alias,
228
			'delete' => &$delete
229
		));
230
231
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
232
			//remove page from database
233
			Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias));
234
235
			Cache::clear("pages");
236
		}
237
	}
238
239
	public static function deleteByID (int $id) {
240
		$delete = true;
241
242
		//plugins can avoid deletion or change alias
243
		Events::throwEvent("delete_page_id", array(
244
			'alias' => &$id,
245
			'delete' => &$delete
246
		));
247
248
		if ($delete) {
0 ignored issues
show
introduced by
The condition $delete is always true.
Loading history...
249
			//remove page from database
250
			Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `id` = :id; ", array('id' => $id));
251
252
			Cache::clear("pages");
253
		}
254
	}
255
256
	public static function get (string $alias) : Page {
257
		$page = new Page();
258
		$page->load($alias);
259
260
		return $page;
261
	}
262
263
	public static function setPageType (string $alias, string $page_type) {
264
		Events::throwEvent("set_pagetype", array(
265
			'alias' => &$alias,
266
			'page_type' => &$page_type
267
		));
268
269
		Database::getInstance()->execute("UPDATE `{praefix}pages` SET `page_type` = :page_type WHERE `alias` = :alias; ", array(
270
			'alias' => $alias,
271
			'page_type' => $page_type
272
		));
273
274
		Cache::clear("pages");
275
	}
276
277
	/**
278
	 * get id of page by alias
279
	 *
280
	 * only use this method for database upgrade, because their is no caching for this method!
281
	 *
282
	 * @param string $alias alias of page
283
	 *
284
	 * @throws IllegalStateException if alias doesnt exists
285
	 *
286
	 * @return int pageID
287
	 */
288
	public static function getPageIDByAlias (string $alias) : int {
289
		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias));
290
291
		if (!$row) {
292
			throw new IllegalStateException("page with alias '" . $alias . "' doesnt exists.");
293
		}
294
295
		return $row['id'];
296
	}
297
298
}
299
300
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
301