| Total Complexity | 45 |
| Total Lines | 317 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PageHandler 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 PageHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class PageHandler extends \XoopsPersistableObjectHandler |
||
| 28 | { |
||
| 29 | private $exclude = [ |
||
| 30 | 'backend.php', |
||
| 31 | 'footer.php', |
||
| 32 | 'header.php', |
||
| 33 | 'index.php', |
||
| 34 | 'page_comment.php', |
||
| 35 | 'page_like_dislike.php', |
||
| 36 | 'page_print.php', |
||
| 37 | 'page_rate.php', |
||
| 38 | 'qrcode.php', |
||
| 39 | 'xoops_version.php', |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param null|\Xoops\Core\Database\Connection $db |
||
| 44 | */ |
||
| 45 | public function __construct(Connection $db = null) |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param $xooghostUrl |
||
| 57 | * |
||
| 58 | * @return mixed |
||
| 59 | */ |
||
| 60 | public function getByURL($xooghostUrl) |
||
| 61 | { |
||
| 62 | $criteria = new \Criteria('xooghost_url', $xooghostUrl); |
||
| 63 | $page = $this->getObjects($criteria, false, true); |
||
| 64 | |||
| 65 | return $page[0]; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $sort |
||
| 70 | * @param string $order |
||
| 71 | * @param int $start |
||
| 72 | * @param int $limit |
||
| 73 | * |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public function getPublished($sort = 'published', $order = 'desc', $start = 0, $limit = 0) |
||
| 77 | { |
||
| 78 | $criteria = new \CriteriaCompo(); |
||
| 79 | $criteria->add(new \Criteria('xooghost_online', 1)); |
||
| 80 | $criteria->add(new \Criteria('xooghost_published', time(), '<=')); |
||
| 81 | if ('random' === $sort) { |
||
| 82 | $criteria->setSort('rand()'); |
||
| 83 | } else { |
||
| 84 | $criteria->setSort('xooghost_' . $sort); |
||
| 85 | } |
||
| 86 | $criteria->setOrder($order); |
||
| 87 | $criteria->setStart($start); |
||
| 88 | $criteria->setLimit($limit); |
||
| 89 | |||
| 90 | return $this->getObjects($criteria, true, false); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | public function getUrls() |
||
| 97 | { |
||
| 98 | $ret = []; |
||
| 99 | $pages = $this->getPublished(); |
||
| 100 | foreach ($pages as $page) { |
||
| 101 | $ret[] = $page['xooghost_url']; |
||
| 102 | } |
||
| 103 | |||
| 104 | return $ret; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param $Xooghost_id |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function setOnline($Xooghost_id) |
||
| 113 | { |
||
| 114 | if (0 != $Xooghost_id) { |
||
| 115 | $page = $this->get($Xooghost_id); |
||
| 116 | if (1 == $page->getVar('xooghost_online')) { |
||
| 117 | $page->setVar('xooghost_online', 0); |
||
| 118 | } else { |
||
| 119 | $page->setVar('xooghost_online', 1); |
||
| 120 | } |
||
| 121 | $this->insert($page); |
||
| 122 | |||
| 123 | return true; |
||
| 124 | } |
||
| 125 | |||
| 126 | return false; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param $pageObj |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function setRead($pageObj) |
||
| 135 | { |
||
| 136 | $read = $pageObj->getVar('xooghost_hits') + 1; |
||
| 137 | $pageObj->setVar('xooghost_hits', $read); |
||
| 138 | $this->insert($pageObj); |
||
| 139 | |||
| 140 | return true; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param $page_id |
||
| 145 | * @param $like_dislike |
||
| 146 | * |
||
| 147 | * @return array|bool |
||
| 148 | */ |
||
| 149 | public function setLikeDislike($page_id, $like_dislike) |
||
| 150 | { |
||
| 151 | if (0 != $page_id) { |
||
| 152 | $page = $this->get($page_id); |
||
| 153 | if (is_object($page) && 0 != count($page)) { |
||
| 154 | $xoops = \Xoops::getInstance(); |
||
| 155 | |||
| 156 | if ($ret = $this->rldHandler->setLikeDislike($page_id, $like_dislike)) { |
||
| 157 | if (0 == $like_dislike) { |
||
| 158 | $xooghost_dislike = $page->getVar('xooghost_dislike') + 1; |
||
| 159 | $page->setVar('xooghost_dislike', $xooghost_dislike); |
||
| 160 | } elseif (1 == $like_dislike) { |
||
| 161 | $xooghost_like = $page->getVar('xooghost_like') + 1; |
||
| 162 | $page->setVar('xooghost_like', $xooghost_like); |
||
| 163 | } |
||
| 164 | $this->insert($page); |
||
| 165 | |||
| 166 | return $page->getValues(); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | return false; |
||
| 171 | } |
||
| 172 | |||
| 173 | return false; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param $page_id |
||
| 178 | * @param $rate |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | public function setRate($page_id, $rate) |
||
| 183 | { |
||
| 184 | if (0 != $page_id) { |
||
| 185 | $page = $this->get($page_id); |
||
| 186 | if (is_object($page) && 0 != count($page)) { |
||
| 187 | $xoops = \Xoops::getInstance(); |
||
| 188 | |||
| 189 | if ($ret = $this->rldHandler->setRate($page_id, $rate)) { |
||
| 190 | if (is_array($ret) && 3 == count($ret)) { |
||
| 191 | $page->setVar('xooghost_rates', $ret['average']); |
||
| 192 | $this->insert($page); |
||
| 193 | |||
| 194 | return $ret; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | return false; |
||
| 200 | } |
||
| 201 | |||
| 202 | return false; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function selectPage() |
||
| 209 | { |
||
| 210 | $pages = $this->getPublished(); |
||
| 211 | $form = new \Xoops\Form\Select('', 'xooghost_url'); |
||
| 212 | $form->setExtra("onChange='javascript:window.location.href=this.value'"); |
||
| 213 | $form->addOption('index.php', _XOO_GHOST_CHOOSE); |
||
| 214 | foreach ($pages as $page) { |
||
| 215 | $form->addOption($page['xooghost_link'], $page['xooghost_title']); |
||
| 216 | } |
||
| 217 | |||
| 218 | return $form->render(); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param int $online |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function renderAdminList($online = -1) |
||
| 227 | { |
||
| 228 | $criteria = new \CriteriaCompo(); |
||
| 229 | $criteria->setSort('xooghost_published'); |
||
| 230 | $criteria->setOrder('DESC'); |
||
| 231 | if ($online >= 0) { |
||
| 232 | $criteria->add(new \Criteria('xooghost_online', $online)); |
||
| 233 | } |
||
| 234 | $criteria->setOrder('asc'); |
||
| 235 | |||
| 236 | return $this->getObjects($criteria, true, false); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param \Xoops\Core\Kernel\XoopsObject $object |
||
| 241 | * @param bool $force |
||
| 242 | * |
||
| 243 | * @return bool|mixed |
||
| 244 | */ |
||
| 245 | public function insert(\Xoops\Core\Kernel\XoopsObject $object, $force = true) |
||
| 246 | { |
||
| 247 | $xoops = \Xoops::getInstance(); |
||
| 248 | if (parent::insert($object, $force)) { |
||
| 249 | $object->createPage(); |
||
| 250 | if ($object->isNew()) { |
||
| 251 | return $xoops->db()->getInsertId(); |
||
| 252 | } |
||
| 253 | |||
| 254 | return $object->getVar('xooghost_id'); |
||
| 255 | } |
||
| 256 | |||
| 257 | return false; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param $image_name |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public function uploadImages($image_name) |
||
| 266 | { |
||
| 267 | $xoops = \Xoops::getInstance(); |
||
| 268 | $autoload = \XoopsLoad::loadConfig('xooghost'); |
||
| 269 | |||
| 270 | $uploader = new \XoopsMediaUploader(\XoopsBaseConfig::get('uploads-path') . '/xooghost/images', $autoload['mimetypes'], $this->config['xooghost_image_size'], $this->config['xooghost_image_width'], $this->config['xooghost_image_height']); |
||
| 271 | |||
| 272 | $ret = []; |
||
| 273 | foreach (Request::getArray('xoops_upload_file', [], 'POST') as $k => $input_image) { |
||
| 274 | if ('' != Request::getArray($input_image, [], 'FILES')['tmp_name'] || is_readable(Request::getArray($input_image, [], 'FILES')['tmp_name'])) { |
||
| 275 | $path_parts = pathinfo(Request::getArray($input_image, [], 'FILES')['name']); |
||
| 276 | $uploader->setTargetFileName($this->cleanImage(mb_strtolower($image_name . '.' . $path_parts['extension']))); |
||
| 277 | if ($uploader->fetchMedia(Request::getArray('xoops_upload_file', [], 'POST')[$k])) { |
||
| 278 | if ($uploader->upload()) { |
||
| 279 | $ret[$input_image] = ['filename' => $uploader->getSavedFileName(), 'error' => false, 'message' => '']; |
||
| 280 | } else { |
||
| 281 | $ret[$input_image] = ['filename' => Request::getArray($input_image, [], 'FILES')['name'], 'error' => true, 'message' => $uploader->getErrors()]; |
||
| 282 | } |
||
| 283 | } else { |
||
| 284 | $ret[$input_image] = ['filename' => Request::getArray($input_image, [], 'FILES')['name'], 'error' => true, 'message' => $uploader->getErrors()]; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | return $ret; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param $filename |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function cleanImage($filename) |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function getPhpListAsArray() |
||
| 344 | } |
||
| 345 | } |
||
| 346 |