| Total Complexity | 43 |
| Total Lines | 353 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Category 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 Category, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Category extends \XoopsObject |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var Publisher\Helper |
||
| 37 | */ |
||
| 38 | public $helper; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | public $categoryPath = false; |
||
| 44 | public $categoryid; |
||
| 45 | public $parentid; |
||
| 46 | public $name; |
||
| 47 | public $description; |
||
| 48 | public $image; |
||
| 49 | public $total; |
||
| 50 | public $weight; |
||
| 51 | public $created; |
||
| 52 | public $template; |
||
| 53 | public $header; |
||
| 54 | public $meta_keywords; |
||
| 55 | public $meta_description; |
||
| 56 | public $short_url; |
||
| 57 | public $moderator; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * constructor |
||
| 61 | */ |
||
| 62 | public function __construct() |
||
| 63 | { |
||
| 64 | /** @var \XoopsModules\Publisher\Helper $this->helper */ |
||
| 65 | $this->helper = \XoopsModules\Publisher\Helper::getInstance(); |
||
| 66 | $this->initVar('categoryid', XOBJ_DTYPE_INT, null, false); |
||
| 67 | $this->initVar('parentid', XOBJ_DTYPE_INT, null, false); |
||
| 68 | $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100); |
||
| 69 | $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false, 255); |
||
| 70 | $this->initVar('image', XOBJ_DTYPE_TXTBOX, null, false, 255); |
||
| 71 | $this->initVar('total', XOBJ_DTYPE_INT, 1, false); |
||
| 72 | $this->initVar('weight', XOBJ_DTYPE_INT, 1, false); |
||
| 73 | $this->initVar('created', XOBJ_DTYPE_INT, null, false); |
||
| 74 | $this->initVar('template', XOBJ_DTYPE_TXTBOX, null, false, 255); |
||
| 75 | $this->initVar('header', XOBJ_DTYPE_TXTAREA, null, false); |
||
| 76 | $this->initVar('meta_keywords', XOBJ_DTYPE_TXTAREA, null, false); |
||
| 77 | $this->initVar('meta_description', XOBJ_DTYPE_TXTAREA, null, false); |
||
| 78 | $this->initVar('short_url', XOBJ_DTYPE_TXTBOX, null, false, 255); |
||
| 79 | $this->initVar('moderator', XOBJ_DTYPE_INT, null, false, 0); |
||
| 80 | //not persistent values |
||
| 81 | $this->initVar('itemcount', XOBJ_DTYPE_INT, 0, false); |
||
| 82 | $this->initVar('last_itemid', XOBJ_DTYPE_INT); |
||
| 83 | $this->initVar('last_title_link', XOBJ_DTYPE_TXTBOX); |
||
| 84 | $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $method |
||
| 89 | * @param array $args |
||
| 90 | * |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | public function __call($method, $args) |
||
| 94 | { |
||
| 95 | $arg = isset($args[0]) ? $args[0] : null; |
||
| 96 | |||
| 97 | return $this->getVar($method, $arg); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | public function notLoaded() |
||
| 104 | { |
||
| 105 | return (-1 == $this->getVar('categoryid')); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function checkPermission() |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $format |
||
| 133 | * |
||
| 134 | * @return mixed|string |
||
| 135 | */ |
||
| 136 | public function getImage($format = 's') |
||
| 137 | { |
||
| 138 | if ('' != $this->getVar('image')) { |
||
| 139 | return $this->getVar('image', $format); |
||
| 140 | } |
||
| 141 | |||
| 142 | return 'blank.png'; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $format |
||
| 147 | * |
||
| 148 | * @return mixed |
||
| 149 | */ |
||
| 150 | public function getTemplate($format = 'n') |
||
| 151 | { |
||
| 152 | return $this->getVar('template', $format); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param bool $withAllLink |
||
| 157 | * |
||
| 158 | * @return array|bool|string |
||
| 159 | */ |
||
| 160 | public function getCategoryPath($withAllLink = true) |
||
| 161 | { |
||
| 162 | if (empty($this->categoryPath)) { |
||
| 163 | if ($withAllLink) { |
||
| 164 | $ret = $this->getCategoryLink(); |
||
| 165 | } else { |
||
| 166 | $ret = $this->name(); |
||
| 167 | } |
||
| 168 | $parentid = $this->parentid(); |
||
| 169 | if (0 != $parentid) { |
||
| 170 | /** @var Publisher\CategoryHandler $categoryHandler */ |
||
| 171 | $categoryHandler = $this->helper->getHandler('Category'); |
||
| 172 | $parentObj = $categoryHandler->get($parentid); |
||
| 173 | // if ($parentObj->notLoaded()) { |
||
| 174 | // exit; |
||
| 175 | // } |
||
| 176 | |||
| 177 | try { |
||
| 178 | if ($parentObj->notLoaded()) { |
||
| 179 | throw new \RuntimeException(_NOPERM); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | catch (\Exception $e) { |
||
| 183 | $this->helper->addLog($e); |
||
| 184 | // redirect_header('javascript:history.go(-1)', 1, _NOPERM); |
||
| 185 | } |
||
| 186 | |||
| 187 | $ret = $parentObj->getCategoryPath($withAllLink) . ' <li> ' . $ret . '</li>'; |
||
| 188 | } |
||
| 189 | $this->categoryPath = $ret; |
||
| 190 | } |
||
| 191 | |||
| 192 | return $this->categoryPath; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return mixed|string |
||
| 197 | */ |
||
| 198 | public function getCategoryPathForMetaTitle() |
||
| 199 | { |
||
| 200 | $ret = ''; |
||
| 201 | $parentid = $this->parentid(); |
||
| 202 | if (0 != $parentid) { |
||
| 203 | /** @var Publisher\CategoryHandler $categoryHandler */ |
||
| 204 | $categoryHandler = $this->helper->getHandler('Category'); |
||
| 205 | $parentObj = $categoryHandler->get($parentid); |
||
| 206 | // if ($parentObj->notLoaded()) { |
||
| 207 | // exit('NOT LOADED'); |
||
| 208 | // } |
||
| 209 | |||
| 210 | try { |
||
| 211 | if ($parentObj->notLoaded()) { |
||
| 212 | throw new \RuntimeException('NOT LOADED'); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | catch (\Exception $e) { |
||
| 216 | $this->helper->addLog($e); |
||
| 217 | // redirect_header('javascript:history.go(-1)', 1, _NOPERM); |
||
| 218 | } |
||
| 219 | |||
| 220 | $ret = $parentObj->getCategoryPath(false); |
||
| 221 | $ret = str_replace(' >', ' -', $ret); |
||
| 222 | } |
||
| 223 | |||
| 224 | return $ret; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return array|null |
||
| 229 | */ |
||
| 230 | public function getGroupsRead() |
||
| 231 | { |
||
| 232 | /** @var Publisher\PermissionHandler $permissionHandler */ |
||
| 233 | $permissionHandler = $this->helper->getHandler('Permission'); |
||
| 234 | |||
| 235 | return $permissionHandler->getGrantedGroupsById('category_read', $this->categoryid()); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return array|null |
||
| 240 | */ |
||
| 241 | public function getGroupsSubmit() |
||
| 242 | { |
||
| 243 | /** @var Publisher\PermissionHandler $permissionHandler */ |
||
| 244 | $permissionHandler = $this->helper->getHandler('Permission'); |
||
| 245 | |||
| 246 | return $permissionHandler->getGrantedGroupsById('item_submit', $this->categoryid()); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return array|null |
||
| 251 | */ |
||
| 252 | public function getGroupsModeration() |
||
| 253 | { |
||
| 254 | /** @var Publisher\PermissionHandler $permissionHandler */ |
||
| 255 | $permissionHandler = $this->helper->getHandler('Permission'); |
||
| 256 | |||
| 257 | return $permissionHandler->getGrantedGroupsById('category_moderation', $this->categoryid()); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getCategoryUrl() |
||
| 264 | { |
||
| 265 | return Publisher\Seo::generateUrl('category', $this->categoryid(), $this->short_url()); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param bool $class |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getCategoryLink($class = false) |
||
| 274 | { |
||
| 275 | if ($class) { |
||
| 276 | return "<a class='$class' href='" . $this->getCategoryUrl() . "'>" . $this->name() . '</a>'; |
||
| 277 | } |
||
| 278 | |||
| 279 | return "<a href='" . $this->getCategoryUrl() . "'>" . $this->name() . '</a>'; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param bool $sendNotifications |
||
| 284 | * @param bool $force |
||
| 285 | * |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | public function store($sendNotifications = true, $force = true) |
||
| 289 | { |
||
| 290 | $ret = $this->helper->getHandler('Category')->insert($this, $force); |
||
| 291 | if ($sendNotifications && $ret && $this->isNew()) { |
||
| 292 | $this->sendNotifications(); |
||
| 293 | } |
||
| 294 | $this->unsetNew(); |
||
| 295 | |||
| 296 | return $ret; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Send notifications |
||
| 301 | */ |
||
| 302 | public function sendNotifications() |
||
| 303 | { |
||
| 304 | $tags = []; |
||
| 305 | $tags['MODULE_NAME'] = $this->helper->getModule()->getVar('name'); |
||
| 306 | $tags['CATEGORY_NAME'] = $this->name(); |
||
| 307 | $tags['CATEGORY_URL'] = $this->getCategoryUrl(); |
||
| 308 | /* @var \XoopsNotificationHandler $notificationHandler */ |
||
| 309 | $notificationHandler = xoops_getHandler('notification'); |
||
| 310 | $notificationHandler->triggerEvent('global_item', 0, 'category_created', $tags); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param array $category |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function toArraySimple($category = []) |
||
| 319 | { |
||
| 320 | $category['categoryid'] = $this->categoryid(); |
||
| 321 | $category['name'] = $this->name(); |
||
| 322 | $category['categorylink'] = $this->getCategoryLink(); |
||
| 323 | $category['categoryurl'] = $this->getCategoryUrl(); |
||
| 324 | $category['total'] = ($this->getVar('itemcount') > 0) ? $this->getVar('itemcount') : ''; |
||
| 325 | $category['description'] = $this->description(); |
||
| 326 | $category['header'] = $this->header(); |
||
| 327 | $category['meta_keywords'] = $this->meta_keywords(); |
||
| 328 | $category['meta_description'] = $this->meta_description(); |
||
| 329 | $category['short_url'] = $this->short_url(); |
||
| 330 | if ($this->getVar('last_itemid') > 0) { |
||
| 331 | $category['last_itemid'] = $this->getVar('last_itemid', 'n'); |
||
| 332 | $category['last_title_link'] = $this->getVar('last_title_link', 'n'); |
||
| 333 | } |
||
| 334 | if ('blank.png' !== $this->getImage()) { |
||
| 335 | $category['image_path'] = Publisher\Utility::getImageDir('category', false) . $this->getImage(); |
||
| 336 | } else { |
||
| 337 | $category['image_path'] = ''; |
||
| 338 | } |
||
| 339 | $category['lang_subcategories'] = sprintf(_CO_PUBLISHER_SUBCATEGORIES_INFO, $this->name()); |
||
| 340 | |||
| 341 | return $category; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param array $category |
||
| 346 | * |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public function toArrayTable($category = []) |
||
| 350 | { |
||
| 351 | $category['categoryid'] = $this->categoryid(); |
||
| 352 | $category['categorylink'] = $this->getCategoryLink(); |
||
| 353 | $category['total'] = ($this->getVar('itemcount') > 0) ? $this->getVar('itemcount') : ''; |
||
| 354 | $category['description'] = $this->description(); |
||
| 355 | if ($this->getVar('last_itemid') > 0) { |
||
| 356 | $category['last_itemid'] = $this->getVar('last_itemid', 'n'); |
||
| 357 | $category['last_title_link'] = $this->getVar('last_title_link', 'n'); |
||
| 358 | } |
||
| 359 | if ('blank.png' !== $this->getImage()) { |
||
| 360 | $category['image_path'] = Publisher\Utility::getImageDir('category', false) . $this->getImage(); |
||
| 361 | } else { |
||
| 362 | $category['image_path'] = ''; |
||
| 363 | } |
||
| 364 | $category['lang_subcategories'] = sprintf(_CO_PUBLISHER_SUBCATEGORIES_INFO, $this->name()); |
||
| 365 | |||
| 366 | return $category; |
||
| 367 | } |
||
| 368 | |||
| 369 | public function createMetaTags() |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param int $subCatsCount |
||
| 377 | * |
||
| 378 | * @return \XoopsModules\Publisher\Form\CategoryForm |
||
| 379 | */ |
||
| 380 | public function getForm($subCatsCount = 4) |
||
| 386 | } |
||
| 387 | } |
||
| 388 |