Total Complexity | 85 |
Total Lines | 505 |
Duplicated Lines | 0 % |
Changes | 16 | ||
Bugs | 7 | Features | 6 |
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 |
||
19 | class Page { |
||
20 | |||
21 | protected $pageID = -1; |
||
22 | protected $alias = null; |
||
23 | protected $row = null; |
||
24 | protected $pagetype = ""; |
||
25 | |||
26 | protected $author = null; |
||
27 | |||
28 | //changed columns to save with save() |
||
29 | protected $changes = array(); |
||
30 | |||
31 | public function __construct() { |
||
32 | // |
||
33 | } |
||
34 | |||
35 | public function load ($alias = null) { |
||
36 | if ($alias == null) { |
||
37 | if (isset($_REQUEST['page']) && !empty($_REQUEST['page'])) { |
||
38 | $alias = Validator_String::get($_REQUEST['page']); |
||
39 | } else { |
||
40 | $alias = $this->getDomain()->getHomePage(); |
||
41 | } |
||
42 | } else { |
||
43 | //$alias = Database::getInstance()->escape($alias); |
||
44 | } |
||
45 | |||
46 | Events::throwEvent("get_alias", array( |
||
47 | 'alias' => &$alias, |
||
48 | 'page' => &$this, |
||
49 | 'domain' => $this->getDomain() |
||
50 | )); |
||
51 | |||
52 | $this->alias = $alias; |
||
53 | |||
54 | if (Cache::contains("pages", "page_" . $alias)) { |
||
55 | $this->row = Cache::get("pages", "page_" . $alias); |
||
56 | } else { |
||
57 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias AND `activated` = '1'; ", array('alias' => $alias)); |
||
58 | |||
59 | if (!$row) { |
||
60 | if (PHPUtils::strEqs("error404", $alias)) { |
||
61 | throw new IllegalStateException("No page with alias 'error404' exists (requested alias: " . $alias . ")."); |
||
62 | } |
||
63 | |||
64 | //page not found |
||
65 | $new_alias = "error404"; |
||
66 | |||
67 | Events::throwEvent("load_page_error404", array( |
||
68 | 'alias' => &$new_alias, |
||
69 | 'original_alias' => $alias, |
||
70 | 'page' => &$this, |
||
71 | 'domain' => $this->getDomain() |
||
72 | )); |
||
73 | |||
74 | $this->load($new_alias); |
||
75 | return null; |
||
76 | } |
||
77 | |||
78 | $this->row = $row; |
||
79 | |||
80 | //cache result |
||
81 | Cache::put("pages", "page_" . $alias, $row); |
||
82 | } |
||
83 | |||
84 | //get pageID |
||
85 | $this->pageID = $this->row['id']; |
||
86 | |||
87 | //get name of page type (class name) |
||
88 | $this->pagetype = $this->row['page_type']; |
||
89 | } |
||
90 | |||
91 | public function loadByID (int $pageID, bool $use_cache = true) { |
||
92 | if ($use_cache && Cache::contains("pages", "pageID_" . $pageID)) { |
||
93 | $this->row = Cache::get("pages", "pageID_" . $pageID); |
||
94 | } else { |
||
95 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `id` = :pageID; ", array('pageID' => $pageID)); |
||
96 | |||
97 | if (!$row) { |
||
98 | throw new IllegalStateException("Page with pageID " . $pageID . " doesnt exists!"); |
||
99 | } |
||
100 | |||
101 | $this->row = $row; |
||
102 | |||
103 | //cache result |
||
104 | Cache::put("pages", "pageID_" . $pageID, $row); |
||
105 | } |
||
106 | |||
107 | $this->pageID = $this->row['id']; |
||
108 | $this->alias = $this->row['alias']; |
||
109 | |||
110 | //get name of page type (class name) |
||
111 | $this->pagetype = $this->row['page_type']; |
||
112 | } |
||
113 | |||
114 | protected function getDomain () : Domain { |
||
115 | return Registry::singleton()->getObject("domain"); |
||
116 | } |
||
117 | |||
118 | public function reloadCache () { |
||
119 | Cache::clear("pages"); |
||
120 | } |
||
121 | |||
122 | public function getPageID () : int { |
||
123 | return $this->row['id']; |
||
124 | } |
||
125 | |||
126 | public function getAlias () : string { |
||
127 | return $this->alias; |
||
128 | } |
||
129 | |||
130 | public function getPageType () : string { |
||
131 | return $this->pagetype; |
||
132 | } |
||
133 | |||
134 | public function getTitle () : string { |
||
135 | return $this->row['title']; |
||
136 | } |
||
137 | |||
138 | public function setTitle (string $title) { |
||
139 | $this->row['title'] = $title; |
||
140 | $this->changes[] = "title"; |
||
141 | } |
||
142 | |||
143 | public function getContent () : string { |
||
144 | return $this->row['content']; |
||
145 | } |
||
146 | |||
147 | public function setContent (string $content) { |
||
148 | $this->row['content'] = $content; |
||
149 | $this->changes[] = "content"; |
||
150 | $this->changes[] = "content"; |
||
151 | } |
||
152 | |||
153 | public function getGlobalMenuID () : int { |
||
155 | } |
||
156 | |||
157 | public function getLocalMenuID () : int { |
||
158 | return $this->row['local_menu']; |
||
159 | } |
||
160 | |||
161 | public function getStyle () : string { |
||
162 | return $this->row['design']; |
||
163 | } |
||
164 | |||
165 | public function getFolder () : string { |
||
166 | return $this->row['folder']; |
||
167 | } |
||
168 | |||
169 | public function getLastEdit () { |
||
170 | return $this->row['lastUpdate']; |
||
171 | } |
||
172 | |||
173 | public function hasCustomTemplate () : bool { |
||
174 | return $this->row['template'] !== "none"; |
||
175 | } |
||
176 | |||
177 | public function getCustomTemplate () : string { |
||
178 | return $this->row['template']; |
||
179 | } |
||
180 | |||
181 | public function hasCustomPermissions () : bool { |
||
183 | } |
||
184 | |||
185 | public function listCustomPermissions () : array { |
||
186 | return explode("|", $this->row['can_see_permissions']); |
||
187 | } |
||
188 | |||
189 | public function isPublished () : bool { |
||
190 | return $this->row['published'] == 1; |
||
191 | } |
||
192 | |||
193 | public function publish () { |
||
194 | $this->row['published'] = 1; |
||
195 | $this->changes[] = "published"; |
||
196 | } |
||
197 | |||
198 | public function getContentType () : string { |
||
199 | return $this->row['content_type']; |
||
200 | } |
||
201 | |||
202 | public function getParentID () : int { |
||
203 | return $this->row['parent']; |
||
204 | } |
||
205 | |||
206 | public function getLeftSidebarID () : int { |
||
207 | return $this->row['sidebar_left']; |
||
208 | } |
||
209 | |||
210 | public function getRightSidebarID () : int { |
||
211 | return $this->row['sidebar_right']; |
||
212 | } |
||
213 | |||
214 | public function getMetaDescription () : string { |
||
215 | return $this->row['meta_description']; |
||
216 | } |
||
217 | |||
218 | public function getMetaKeywords () : string { |
||
219 | return $this->row['meta_keywords']; |
||
220 | } |
||
221 | |||
222 | public function getMetaRobotsOptions () : string { |
||
223 | return $this->row['meta_robots']; |
||
224 | } |
||
225 | |||
226 | public function getMetaCanonicals () : string { |
||
227 | return $this->row['meta_canonicals']; |
||
228 | } |
||
229 | |||
230 | public function getOgType () : string { |
||
231 | return $this->row['og_type']; |
||
232 | } |
||
233 | |||
234 | public function getOgTitle () : string { |
||
235 | return !empty($this->row['og_title']) ? $this->row['og_title'] : $this->getTitle(); |
||
236 | } |
||
237 | |||
238 | public function getOgDescription () : string { |
||
239 | return !empty($this->row['og_description']) ? $this->row['og_description'] : $this->getMetaDescription(); |
||
240 | } |
||
241 | |||
242 | public function getOgImages () : array { |
||
248 | } |
||
249 | |||
250 | public function getAuthorID () : int { |
||
251 | return $this->row['author']; |
||
252 | } |
||
253 | |||
254 | public function getAuthor () : User { |
||
267 | } |
||
268 | |||
269 | public function isSitemapEnabled () : bool { |
||
270 | return $this->row['sitemap'] == 1; |
||
271 | } |
||
272 | |||
273 | public function getSitemapChangeFreq () : string { |
||
274 | return $this->row['sitemap_changefreq']; |
||
275 | } |
||
276 | |||
277 | public function getSitemapPriority () : string { |
||
278 | return $this->row['sitemap_priority']; |
||
279 | } |
||
280 | |||
281 | public function activate (bool $bool = true) { |
||
282 | $this->row['activated'] = ($bool ? 1 : 0); |
||
283 | } |
||
284 | |||
285 | public function isTrash () : bool { |
||
286 | return $this->row['activated'] == 2; |
||
287 | } |
||
288 | |||
289 | public function isEditable () : bool { |
||
290 | return $this->row['editable'] == 1; |
||
291 | } |
||
292 | |||
293 | public function isDeletable () : bool { |
||
294 | return $this->row['deletable'] == 1; |
||
295 | } |
||
296 | |||
297 | public function isActivated () : bool { |
||
298 | return $this->row['activated'] == 1; |
||
299 | } |
||
300 | |||
301 | public function moveToTrash () { |
||
302 | self::movePageToTrash($this->pageID); |
||
303 | |||
304 | //clear cache |
||
305 | $this->clearCache(); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * restore page from trash |
||
310 | */ |
||
311 | public function restore () { |
||
312 | self::restorePage($this->pageID); |
||
313 | |||
314 | //clear cache |
||
315 | $this->clearCache(); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * save changes into database |
||
320 | */ |
||
321 | public function save () { |
||
322 | //TODO: add code here |
||
323 | } |
||
324 | |||
325 | public function clearCache () { |
||
326 | if (!is_int($this->getPageID())) { |
||
|
|||
327 | throw new IllegalStateException("pageID isn't set."); |
||
328 | } |
||
329 | |||
330 | //clear cache |
||
331 | Cache::clear("pages", "pageID_" . $this->getPageID()); |
||
332 | Cache::clear("pages", "page_" . $this->getAlias()); |
||
333 | } |
||
334 | |||
335 | 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, bool $deletable = true, string $author = "system") : int { |
||
403 | } |
||
404 | |||
405 | public static function delete (string $alias) { |
||
406 | $delete = true; |
||
407 | |||
408 | //plugins can avoid deletion or change alias |
||
409 | Events::throwEvent("delete_page_alias", array( |
||
410 | 'alias' => &$alias, |
||
411 | 'delete' => &$delete |
||
412 | )); |
||
413 | |||
414 | if ($delete) { |
||
415 | //remove page from database |
||
416 | Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias)); |
||
417 | |||
418 | Cache::clear("pages"); |
||
419 | } |
||
420 | } |
||
421 | |||
422 | public static function deleteByID (int $id) { |
||
423 | $delete = true; |
||
424 | |||
425 | //plugins can avoid deletion or change alias |
||
426 | Events::throwEvent("delete_page_id", array( |
||
427 | 'alias' => &$id, |
||
428 | 'delete' => &$delete |
||
429 | )); |
||
430 | |||
431 | if ($delete) { |
||
432 | //remove page from database |
||
433 | Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `id` = :id; ", array('id' => $id)); |
||
434 | |||
435 | Cache::clear("pages"); |
||
436 | } |
||
437 | } |
||
438 | |||
439 | public static function get (string $alias) : Page { |
||
440 | $page = new Page(); |
||
441 | $page->load($alias); |
||
442 | |||
443 | return $page; |
||
444 | } |
||
445 | |||
446 | public static function setPageType (string $alias, string $page_type) { |
||
447 | Events::throwEvent("set_pagetype", array( |
||
448 | 'alias' => &$alias, |
||
449 | 'page_type' => &$page_type |
||
450 | )); |
||
451 | |||
452 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `page_type` = :page_type WHERE `alias` = :alias; ", array( |
||
453 | 'alias' => $alias, |
||
454 | 'page_type' => $page_type |
||
455 | )); |
||
456 | |||
457 | Cache::clear("pages"); |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * get id of page by alias |
||
462 | * |
||
463 | * only use this method for database upgrade, because their is no caching for this method! |
||
464 | * |
||
465 | * @param string $alias alias of page |
||
466 | * |
||
467 | * @throws IllegalStateException if alias doesnt exists |
||
468 | * |
||
469 | * @return int pageID |
||
470 | */ |
||
471 | public static function getPageIDByAlias (string $alias) : int { |
||
472 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias)); |
||
473 | |||
474 | if (!$row) { |
||
475 | throw new IllegalStateException("page with alias '" . $alias . "' doesnt exists."); |
||
476 | } |
||
477 | |||
478 | return $row['id']; |
||
479 | } |
||
480 | |||
481 | public static function lockPage (int $pageID, int $userID) { |
||
482 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `locked_by` = :userID, `locked_timestamp` = CURRENT_TIMESTAMP WHERE `id` = :pageID; ", array( |
||
483 | 'userID' => $userID, |
||
484 | 'pageID' => $pageID |
||
485 | )); |
||
486 | |||
487 | //clear cache |
||
488 | Cache::clear("pages", "pageID_" . $pageID); |
||
489 | } |
||
490 | |||
491 | public static function unlockPage (int $pageID) { |
||
492 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `locked_by` = '-1' WHERE `id` = :pageID; ", array( |
||
493 | 'pageID' => $pageID |
||
494 | )); |
||
495 | |||
496 | //clear cache |
||
497 | Cache::clear("pages", "pageID_" . $pageID); |
||
498 | } |
||
499 | |||
500 | protected static function movePageToTrash (int $pageID) { |
||
501 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `activated` = 2 WHERE `id` = :pageID; ", array( |
||
502 | 'pageID' => $pageID |
||
503 | )); |
||
504 | |||
505 | //clear cache |
||
506 | Cache::clear("pages", "pageID_" . $pageID); |
||
507 | } |
||
508 | |||
509 | protected static function restorePage (int $pageID) { |
||
510 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `activated` = 1 WHERE `id` = :pageID; ", array( |
||
511 | 'pageID' => $pageID |
||
512 | )); |
||
513 | |||
514 | //clear cache |
||
515 | Cache::clear("pages", "pageID_" . $pageID); |
||
516 | } |
||
517 | |||
518 | public static function exists (string $alias) : bool { |
||
524 | } |
||
525 | |||
526 | } |
||
527 | |||
528 | ?> |
||
529 |