| Total Complexity | 67 |
| Total Lines | 428 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 3 |
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 | public function __construct() { |
||
| 29 | // |
||
| 30 | } |
||
| 31 | |||
| 32 | public function load ($alias = null) { |
||
| 33 | if ($alias == null) { |
||
| 34 | if (isset($_REQUEST['page']) && !empty($_REQUEST['page'])) { |
||
| 35 | $alias = Validator_String::get($_REQUEST['page']); |
||
| 36 | } else { |
||
| 37 | $alias = $this->getDomain()->getHomePage(); |
||
| 38 | } |
||
| 39 | } else { |
||
| 40 | //$alias = Database::getInstance()->escape($alias); |
||
| 41 | } |
||
| 42 | |||
| 43 | Events::throwEvent("get_alias", array( |
||
| 44 | 'alias' => &$alias, |
||
| 45 | 'page' => &$this, |
||
| 46 | 'domain' => $this->getDomain() |
||
| 47 | )); |
||
| 48 | |||
| 49 | $this->alias = $alias; |
||
| 50 | |||
| 51 | if (Cache::contains("pages", "page_" . $alias)) { |
||
| 52 | $this->row = Cache::get("pages", "page_" . $alias); |
||
| 53 | } else { |
||
| 54 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias AND `activated` = '1'; ", array('alias' => $alias)); |
||
| 55 | |||
| 56 | if (!$row) { |
||
| 57 | if (PHPUtils::strEqs("error404", $alias)) { |
||
| 58 | throw new IllegalStateException("No page with alias 'error404' exists (requested alias: " . $alias . ")."); |
||
| 59 | } |
||
| 60 | |||
| 61 | //page not found |
||
| 62 | $new_alias = "error404"; |
||
| 63 | |||
| 64 | Events::throwEvent("load_page_error404", array( |
||
| 65 | 'alias' => &$new_alias, |
||
| 66 | 'original_alias' => $alias, |
||
| 67 | 'page' => &$this, |
||
| 68 | 'domain' => $this->getDomain() |
||
| 69 | )); |
||
| 70 | |||
| 71 | $this->load($new_alias); |
||
| 72 | return null; |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->row = $row; |
||
| 76 | |||
| 77 | //cache result |
||
| 78 | Cache::put("pages", "page_" . $alias, $row); |
||
| 79 | } |
||
| 80 | |||
| 81 | //get pageID |
||
| 82 | $this->pageID = $this->row['id']; |
||
| 83 | |||
| 84 | //get name of page type (class name) |
||
| 85 | $this->pagetype = $this->row['page_type']; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function loadByID (int $pageID) { |
||
| 89 | if (Cache::contains("pages", "pageID_" . $pageID)) { |
||
| 90 | $this->row = Cache::get("pages", "pageID_" . $pageID); |
||
| 91 | } else { |
||
| 92 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `id` = :pageID; ", array('pageID' => $pageID)); |
||
| 93 | |||
| 94 | if (!$row) { |
||
| 95 | throw new IllegalStateException("Page with pageID " . $pageID . " doesnt exists!"); |
||
| 96 | } |
||
| 97 | |||
| 98 | $this->row = $row; |
||
| 99 | |||
| 100 | //cache result |
||
| 101 | Cache::put("pages", "pageID_" . $pageID, $row); |
||
| 102 | } |
||
| 103 | |||
| 104 | $this->pageID = $this->row['id']; |
||
| 105 | $this->alias = $this->row['alias']; |
||
| 106 | |||
| 107 | //get name of page type (class name) |
||
| 108 | $this->pagetype = $this->row['page_type']; |
||
| 109 | } |
||
| 110 | |||
| 111 | protected function getDomain () : Domain { |
||
| 112 | return Registry::singleton()->getObject("domain"); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function reloadCache () { |
||
| 116 | Cache::clear("pages"); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getPageID () : int { |
||
| 120 | return $this->row['id']; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function getAlias () : string { |
||
| 124 | return $this->alias; |
||
| 125 | } |
||
| 126 | |||
| 127 | public function getPageType () : string { |
||
| 128 | return $this->pagetype; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function getTitle () : string { |
||
| 132 | return $this->row['title']; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getContent () : string { |
||
| 136 | return $this->row['content']; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function getGlobalMenuID () : int { |
||
| 141 | } |
||
| 142 | |||
| 143 | public function getLocalMenuID () : int { |
||
| 144 | return $this->row['local_menu']; |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getStyle () : string { |
||
| 148 | return $this->row['design']; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function getFolder () : string { |
||
| 152 | return $this->row['folder']; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getLastEdit () { |
||
| 156 | return $this->row['lastUpdate']; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function hasCustomTemplate () : bool { |
||
| 160 | return $this->row['template'] !== "none"; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function getCustomTemplate () : string { |
||
| 164 | return $this->row['template']; |
||
| 165 | } |
||
| 166 | |||
| 167 | public function hasCustomPermissions () : bool { |
||
| 168 | return $this->row['can_see_permissions'] !== "none"; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function listCustomPermissions () : array { |
||
| 172 | return explode("|", $this->row['can_see_permissions']); |
||
| 173 | } |
||
| 174 | |||
| 175 | public function isPublished () : bool { |
||
| 176 | return $this->row['published'] == 1; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function getContentType () : string { |
||
| 180 | return $this->row['content_type']; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getLeftSidebarID () : int { |
||
| 184 | return $this->row['sidebar_left']; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function getRightSidebarID () : int { |
||
| 188 | return $this->row['sidebar_right']; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function getMetaDescription () : string { |
||
| 192 | return $this->row['meta_description']; |
||
| 193 | } |
||
| 194 | |||
| 195 | public function getMetaKeywords () : string { |
||
| 196 | return $this->row['meta_keywords']; |
||
| 197 | } |
||
| 198 | |||
| 199 | public function getMetaRobotsOptions () : string { |
||
| 200 | return $this->row['meta_robots']; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function getMetaCanonicals () : string { |
||
| 204 | return $this->row['meta_canonicals']; |
||
| 205 | } |
||
| 206 | |||
| 207 | public function getAuthorID () : int { |
||
| 208 | return $this->row['author']; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function getAuthor () : User { |
||
| 212 | if ($this->author == null) { |
||
| 213 | //load author |
||
| 214 | $this->author = new User(); |
||
| 215 | |||
| 216 | if ($this->getAuthorID() <= 0) { |
||
| 217 | throw new IllegalArgumentException("authorID has to be > 0."); |
||
| 218 | } |
||
| 219 | |||
| 220 | $this->author->load($this->getAuthorID()); |
||
| 221 | } |
||
| 222 | |||
| 223 | return $this->author; |
||
| 224 | } |
||
| 225 | |||
| 226 | public function activate (bool $bool = true) { |
||
| 227 | $this->row['activated'] = ($bool ? 1 : 0); |
||
| 228 | } |
||
| 229 | |||
| 230 | public function isTrash () : bool { |
||
| 231 | return $this->row['activated'] == 2; |
||
| 232 | } |
||
| 233 | |||
| 234 | public function isEditable () : bool { |
||
| 235 | return $this->row['editable'] == 1; |
||
| 236 | } |
||
| 237 | |||
| 238 | public function isDeletable () : bool { |
||
| 239 | return $this->row['deletable'] == 1; |
||
| 240 | } |
||
| 241 | |||
| 242 | public function isActivated () : bool { |
||
| 243 | return $this->row['activated'] == 1; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function moveToTrash () { |
||
| 247 | self::movePageToTrash($this->pageID); |
||
| 248 | |||
| 249 | //clear cache |
||
| 250 | Cache::clear("pages", "pageID_" . $this->pageID); |
||
| 251 | Cache::clear("pages", "page_" . $this->alias); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * restore page from trash |
||
| 256 | */ |
||
| 257 | public function restore () { |
||
| 258 | self::restorePage($this->pageID); |
||
| 259 | |||
| 260 | //clear cache |
||
| 261 | Cache::clear("pages", "pageID_" . $this->pageID); |
||
| 262 | Cache::clear("pages", "page_" . $this->alias); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * save changes into database |
||
| 267 | */ |
||
| 268 | public function save () { |
||
| 269 | //TODO: add code here |
||
| 270 | } |
||
| 271 | |||
| 272 | 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 { |
||
| 340 | } |
||
| 341 | |||
| 342 | public static function delete (string $alias) { |
||
| 343 | $delete = true; |
||
| 344 | |||
| 345 | //plugins can avoid deletion or change alias |
||
| 346 | Events::throwEvent("delete_page_alias", array( |
||
| 347 | 'alias' => &$alias, |
||
| 348 | 'delete' => &$delete |
||
| 349 | )); |
||
| 350 | |||
| 351 | if ($delete) { |
||
| 352 | //remove page from database |
||
| 353 | Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias)); |
||
| 354 | |||
| 355 | Cache::clear("pages"); |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | public static function deleteByID (int $id) { |
||
| 360 | $delete = true; |
||
| 361 | |||
| 362 | //plugins can avoid deletion or change alias |
||
| 363 | Events::throwEvent("delete_page_id", array( |
||
| 364 | 'alias' => &$id, |
||
| 365 | 'delete' => &$delete |
||
| 366 | )); |
||
| 367 | |||
| 368 | if ($delete) { |
||
| 369 | //remove page from database |
||
| 370 | Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `id` = :id; ", array('id' => $id)); |
||
| 371 | |||
| 372 | Cache::clear("pages"); |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | public static function get (string $alias) : Page { |
||
| 377 | $page = new Page(); |
||
| 378 | $page->load($alias); |
||
| 379 | |||
| 380 | return $page; |
||
| 381 | } |
||
| 382 | |||
| 383 | public static function setPageType (string $alias, string $page_type) { |
||
| 384 | Events::throwEvent("set_pagetype", array( |
||
| 385 | 'alias' => &$alias, |
||
| 386 | 'page_type' => &$page_type |
||
| 387 | )); |
||
| 388 | |||
| 389 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `page_type` = :page_type WHERE `alias` = :alias; ", array( |
||
| 390 | 'alias' => $alias, |
||
| 391 | 'page_type' => $page_type |
||
| 392 | )); |
||
| 393 | |||
| 394 | Cache::clear("pages"); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * get id of page by alias |
||
| 399 | * |
||
| 400 | * only use this method for database upgrade, because their is no caching for this method! |
||
| 401 | * |
||
| 402 | * @param string $alias alias of page |
||
| 403 | * |
||
| 404 | * @throws IllegalStateException if alias doesnt exists |
||
| 405 | * |
||
| 406 | * @return int pageID |
||
| 407 | */ |
||
| 408 | public static function getPageIDByAlias (string $alias) : int { |
||
| 409 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias)); |
||
| 410 | |||
| 411 | if (!$row) { |
||
| 412 | throw new IllegalStateException("page with alias '" . $alias . "' doesnt exists."); |
||
| 413 | } |
||
| 414 | |||
| 415 | return $row['id']; |
||
| 416 | } |
||
| 417 | |||
| 418 | public static function lockPage (int $pageID, int $userID) { |
||
| 419 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `locked_by` = :userID, `locked_timestamp` = CURRENT_TIMESTAMP WHERE `id` = :pageID; ", array( |
||
| 420 | 'userID' => $userID, |
||
| 421 | 'pageID' => $pageID |
||
| 422 | )); |
||
| 423 | } |
||
| 424 | |||
| 425 | public static function unlockPage (int $pageID) { |
||
| 426 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `locked_by` WHERE `id` = :pageID; ", array( |
||
| 427 | 'pageID' => $pageID |
||
| 428 | )); |
||
| 429 | } |
||
| 430 | |||
| 431 | protected static function movePageToTrash (int $pageID) { |
||
| 438 | } |
||
| 439 | |||
| 440 | protected static function restorePage (int $pageID) { |
||
| 441 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `activated` = 1 WHERE `id` = :pageID; ", array( |
||
| 442 | 'pageID' => $pageID |
||
| 443 | )); |
||
| 444 | |||
| 445 | //clear cache |
||
| 446 | Cache::clear("pages", "pageID_" . $pageID); |
||
| 447 | } |
||
| 448 | |||
| 449 | } |
||
| 450 | |||
| 451 | ?> |
||
| 452 |