| Total Complexity | 42 |
| Total Lines | 285 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 $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 { |
||
| 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 { |
||
| 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 getLeftSidebarID () : int { |
||
| 155 | return $this->row['sidebar_left']; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function getRightSidebarID () : int { |
||
| 159 | return $this->row['sidebar_right']; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function activate (bool $bool = true) { |
||
| 163 | $this->row['activated'] = $bool; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * save changes into database |
||
| 168 | */ |
||
| 169 | public function save () { |
||
| 170 | //TODO: add code here |
||
| 171 | } |
||
| 172 | |||
| 173 | 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 { |
||
| 228 | } |
||
| 229 | |||
| 230 | public static function delete (string $alias) { |
||
| 231 | $delete = true; |
||
| 232 | |||
| 233 | //plugins can avoid deletion or change alias |
||
| 234 | Events::throwEvent("delete_page_alias", array( |
||
| 235 | 'alias' => &$alias, |
||
| 236 | 'delete' => &$delete |
||
| 237 | )); |
||
| 238 | |||
| 239 | if ($delete) { |
||
|
|
|||
| 240 | //remove page from database |
||
| 241 | Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `alias` = :alias; ", array('alias' => $alias)); |
||
| 242 | |||
| 243 | Cache::clear("pages"); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | public static function deleteByID (int $id) { |
||
| 248 | $delete = true; |
||
| 249 | |||
| 250 | //plugins can avoid deletion or change alias |
||
| 251 | Events::throwEvent("delete_page_id", array( |
||
| 252 | 'alias' => &$id, |
||
| 253 | 'delete' => &$delete |
||
| 254 | )); |
||
| 255 | |||
| 256 | if ($delete) { |
||
| 257 | //remove page from database |
||
| 258 | Database::getInstance()->execute("DELETE FROM `{praefix}pages` WHERE `id` = :id; ", array('id' => $id)); |
||
| 259 | |||
| 260 | Cache::clear("pages"); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | public static function get (string $alias) : Page { |
||
| 265 | $page = new Page(); |
||
| 266 | $page->load($alias); |
||
| 267 | |||
| 268 | return $page; |
||
| 269 | } |
||
| 270 | |||
| 271 | public static function setPageType (string $alias, string $page_type) { |
||
| 272 | Events::throwEvent("set_pagetype", array( |
||
| 273 | 'alias' => &$alias, |
||
| 274 | 'page_type' => &$page_type |
||
| 275 | )); |
||
| 276 | |||
| 277 | Database::getInstance()->execute("UPDATE `{praefix}pages` SET `page_type` = :page_type WHERE `alias` = :alias; ", array( |
||
| 278 | 'alias' => $alias, |
||
| 279 | 'page_type' => $page_type |
||
| 280 | )); |
||
| 281 | |||
| 282 | Cache::clear("pages"); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * get id of page by alias |
||
| 287 | * |
||
| 288 | * only use this method for database upgrade, because their is no caching for this method! |
||
| 289 | * |
||
| 290 | * @param string $alias alias of page |
||
| 291 | * |
||
| 292 | * @throws IllegalStateException if alias doesnt exists |
||
| 293 | * |
||
| 294 | * @return int pageID |
||
| 295 | */ |
||
| 296 | public static function getPageIDByAlias (string $alias) : int { |
||
| 304 | } |
||
| 305 | |||
| 306 | } |
||
| 307 | |||
| 308 | ?> |
||
| 309 |