| Total Complexity | 52 |
| Total Lines | 426 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like EventHandler 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 EventHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 34 | class EventHandler extends \XoopsPersistableObjectHandler |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Constructor |
||
| 38 | * |
||
| 39 | * @param \XoopsDatabase $db |
||
| 40 | */ |
||
| 41 | public function __construct(\XoopsDatabase $db) |
||
| 42 | { |
||
| 43 | parent::__construct($db, 'wgevents_event', Event::class, 'id', 'name'); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param bool $isNew |
||
| 48 | * |
||
| 49 | * @return object |
||
| 50 | */ |
||
| 51 | public function create($isNew = true) |
||
| 52 | { |
||
| 53 | return parent::create($isNew); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * retrieve a field |
||
| 58 | * |
||
| 59 | * @param int $id field id |
||
| 60 | * @param null fields |
||
|
|
|||
| 61 | * @return \XoopsObject|null reference to the {@link Get} object |
||
| 62 | */ |
||
| 63 | public function get($id = null, $fields = null) |
||
| 64 | { |
||
| 65 | return parent::get($id, $fields); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * get inserted id |
||
| 70 | * |
||
| 71 | * @param null |
||
| 72 | * @return int reference to the {@link Get} object |
||
| 73 | */ |
||
| 74 | public function getInsertId() |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get Count Event in the database |
||
| 81 | * @param int $start |
||
| 82 | * @param int $limit |
||
| 83 | * @param string $sort |
||
| 84 | * @param string $order |
||
| 85 | * @return int |
||
| 86 | */ |
||
| 87 | public function getCountEvents($start = 0, $limit = 0, $sort = 'id', $order = 'DESC') |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get All Event in the database |
||
| 96 | * @param int $start |
||
| 97 | * @param int $limit |
||
| 98 | * @param string $sort |
||
| 99 | * @param string $order |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | public function getAllEvents($start = 0, $limit = 0, $sort = 'id', $order = 'DESC') |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get Criteria Event |
||
| 111 | * @param $crEvent |
||
| 112 | * @param int $start |
||
| 113 | * @param int $limit |
||
| 114 | * @param string $sort |
||
| 115 | * @param string $order |
||
| 116 | * @return int |
||
| 117 | */ |
||
| 118 | private function getEventsCriteria($crEvent, int $start, int $limit, string $sort, string $order) |
||
| 119 | { |
||
| 120 | $crEvent->setStart($start); |
||
| 121 | $crEvent->setLimit($limit); |
||
| 122 | $crEvent->setSort($sort); |
||
| 123 | $crEvent->setOrder($order); |
||
| 124 | return $crEvent; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @public function getForm |
||
| 129 | * @param bool $action |
||
| 130 | * @return \XoopsThemeForm |
||
| 131 | */ |
||
| 132 | public function getFormEventSelect($action = false) |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @public function getForm |
||
| 157 | * @param array $params |
||
| 158 | * @param string $action |
||
| 159 | * @return \XoopsThemeForm |
||
| 160 | */ |
||
| 161 | public function getFormPageNavCounter($params = [], $action = '') |
||
| 162 | { |
||
| 163 | if (!$action) { |
||
| 164 | $action = $_SERVER['REQUEST_URI']; |
||
| 165 | } |
||
| 166 | // Get Theme Form |
||
| 167 | \xoops_load('XoopsFormLoader'); |
||
| 168 | $form = new Forms\FormInline('', 'formPageNavCounter', $action, 'post', true); |
||
| 169 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 170 | // Form Table categories |
||
| 171 | $pageNavTray = new Forms\FormElementTray('', ''); |
||
| 172 | $evidSelect = new \XoopsFormSelect(\_MA_WGEVENTS_EVENT_ID, 'limit', $params['limit']); |
||
| 173 | $evidSelect->addOption(10); |
||
| 174 | $evidSelect->addOption(20); |
||
| 175 | $evidSelect->addOption(30); |
||
| 176 | $evidSelect->addOption(40); |
||
| 177 | $evidSelect->addOption(50); |
||
| 178 | $evidSelect->addOption(100); |
||
| 179 | $evidSelect->addOption(0, _ALL); |
||
| 180 | $evidSelect->setExtra("onchange='submit()'"); |
||
| 181 | $pageNavTray->addElement($evidSelect); |
||
| 182 | $form->addElement($pageNavTray); |
||
| 183 | // To list |
||
| 184 | $form->addElement(new \XoopsFormHidden('op', $params['op'])); |
||
| 185 | $form->addElement(new \XoopsFormHidden('start', 0)); |
||
| 186 | $form->addElement(new \XoopsFormHidden('cat_id', $params['cat_id'])); |
||
| 187 | $form->addElement(new \XoopsFormHidden('filterCats', $params['filterCats'])); |
||
| 188 | |||
| 189 | return $form; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @public function to get events for given params |
||
| 194 | * |
||
| 195 | * @param int $start |
||
| 196 | * @param int $limit |
||
| 197 | * @param int $dateFrom // filter date created from (timestamp) |
||
| 198 | * @param int $dateTo // filter date created to (timestamp) |
||
| 199 | * @param string $sortBy |
||
| 200 | * @param string $orderBy |
||
| 201 | * @param string $op |
||
| 202 | * @param int $evId |
||
| 203 | * @param string $filter |
||
| 204 | * @param array $filterCats |
||
| 205 | * @param int $dateCreated |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function getEvents($start = 0, $limit = 0, $dateFrom = 0, $dateTo = 0, $sortBy = 'datefrom', $orderBy = 'ASC', $op = 'list', $evId = 0, $filter = '', $filterCats = [], $dateCreated = 0) |
||
| 209 | { |
||
| 210 | $helper = Helper::getInstance(); |
||
| 211 | |||
| 212 | /* |
||
| 213 | echo '<br>start:'.$start; |
||
| 214 | echo '<br>limit:'.$limit; |
||
| 215 | echo '<br>datefrom:'.\formatTimestamp($dateFrom, 'm').'('.$dateFrom.')'; |
||
| 216 | echo '<br>dateto:'.\formatTimestamp($dateTo, 'm').'('.$dateTo.')'; |
||
| 217 | echo '<br>sortBy:'.$sortBy; |
||
| 218 | echo '<br>orderBy:'.$orderBy; |
||
| 219 | echo '<br>op:'.$op; |
||
| 220 | echo '<br>evId:'.$evId; |
||
| 221 | echo '<br>filter:'.$filter; |
||
| 222 | foreach ($filterCats as $filterCat) { |
||
| 223 | echo '<br>filterCat:'.$filterCat; |
||
| 224 | } |
||
| 225 | */ |
||
| 226 | |||
| 227 | $showItem = ($evId > 0); |
||
| 228 | $uidCurrent = 0; |
||
| 229 | $userIsAdmin = false; |
||
| 230 | if (\is_object($GLOBALS['xoopsUser'])) { |
||
| 231 | $uidCurrent = $GLOBALS['xoopsUser']->uid(); |
||
| 232 | $userIsAdmin = $GLOBALS['xoopsUser']->isAdmin(); |
||
| 233 | } |
||
| 234 | $useGroups = (bool)$helper->getConfig('use_groups'); |
||
| 235 | |||
| 236 | //apply criteria for events |
||
| 237 | $crEvent = new \CriteriaCompo(); |
||
| 238 | if ($showItem) { |
||
| 239 | $crEvent->add(new \Criteria('id', $evId)); |
||
| 240 | } else { |
||
| 241 | if ('me' == $filter && $uidCurrent > 0) { |
||
| 242 | $crEvent->add(new \Criteria('submitter', $uidCurrent)); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | //get only events which are online or from me |
||
| 246 | $crEventOnline = new \CriteriaCompo(); |
||
| 247 | $crEventOnline->add(new \Criteria('status', Constants::STATUS_OFFLINE, '>')); |
||
| 248 | $crEventOnline->add(new \Criteria('submitter', $uidCurrent), 'OR'); |
||
| 249 | $crEvent->add($crEventOnline); |
||
| 250 | |||
| 251 | if ($dateCreated > 0) { |
||
| 252 | $crEvent->add(new \Criteria('datecreated', $dateCreated, '>=')); |
||
| 253 | } |
||
| 254 | if ($useGroups) { |
||
| 255 | // current user |
||
| 256 | // - must have perm to see event or |
||
| 257 | // - must be event owner |
||
| 258 | // - is admin |
||
| 259 | if (!$userIsAdmin) { |
||
| 260 | $crEventGroup = new \CriteriaCompo(); |
||
| 261 | $crEventGroup->add(new \Criteria('groups', '%00000%', 'LIKE')); //all users |
||
| 262 | if ($uidCurrent > 0) { |
||
| 263 | // Get groups |
||
| 264 | $memberHandler = \xoops_getHandler('member'); |
||
| 265 | $xoopsGroups = $memberHandler->getGroupsByUser($uidCurrent); |
||
| 266 | foreach ($xoopsGroups as $group) { |
||
| 267 | $crEventGroup->add(new \Criteria('groups', '%' . substr('00000' . $group, -5) . '%', 'LIKE'), 'OR'); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | $crEventGroup->add(new \Criteria('submitter', $uidCurrent), 'OR'); |
||
| 271 | $crEvent->add($crEventGroup); |
||
| 272 | unset($crEventGroup); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | if (!$showItem) { |
||
| 276 | if ('past' == $op) { |
||
| 277 | // list events before now |
||
| 278 | $crEvent->add(new \Criteria('datefrom', $dateFrom, '<')); |
||
| 279 | $sortBy = 'datefrom'; |
||
| 280 | $orderBy = 'DESC'; |
||
| 281 | } else { |
||
| 282 | // calendar view: |
||
| 283 | // - event start is between dateFrom and dateTo |
||
| 284 | // - event end is between dateFrom and dateTo |
||
| 285 | // ==> dateFrom and dateTo needed |
||
| 286 | |||
| 287 | // index/event/block view: |
||
| 288 | // - event start or event end is greater than dateFrom |
||
| 289 | // ==> dateFrom needed, dateTo must be 0 |
||
| 290 | $crEventFromTo = new \CriteriaCompo(); |
||
| 291 | $crEventStart = new \CriteriaCompo(); |
||
| 292 | $crEventStart->add(new \Criteria('datefrom', $dateFrom, '>=')); |
||
| 293 | if ($dateTo > 0) { |
||
| 294 | $crEventStart->add(new \Criteria('datefrom', $dateTo, '<=')); |
||
| 295 | } |
||
| 296 | $crEventFromTo->add($crEventStart); |
||
| 297 | $crEventEnd = new \CriteriaCompo(); |
||
| 298 | $crEventEnd->add(new \Criteria('dateto', $dateFrom, '>=')); |
||
| 299 | if ($dateTo > 0) { |
||
| 300 | $crEventEnd->add(new \Criteria('dateto', $dateTo, '<=')); |
||
| 301 | } |
||
| 302 | $crEventFromTo->add($crEventEnd, 'OR'); |
||
| 303 | $crEvent->add($crEventFromTo); |
||
| 304 | |||
| 305 | unset($crEventStart, $crEventEnd, $crEventFromTo); |
||
| 306 | $sortBy = 'datefrom'; |
||
| 307 | $orderBy = 'ASC'; |
||
| 308 | } |
||
| 309 | if (\count($filterCats) > 0) { |
||
| 310 | $crEventCats = new \CriteriaCompo(); |
||
| 311 | $crEventCats->add(new \Criteria('catid', '(' . \implode(',', $filterCats) . ')', 'IN')); |
||
| 312 | foreach ($filterCats as $filterCat) { |
||
| 313 | $crEventCats->add(new \Criteria('subcats', '%"' . $filterCat . '"%', 'LIKE'), 'OR'); |
||
| 314 | } |
||
| 315 | $crEvent->add($crEventCats); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | $crEvent->setSort($sortBy); |
||
| 319 | $crEvent->setOrder($orderBy); |
||
| 320 | $eventsCount = $this->getCount($crEvent); |
||
| 321 | if ($eventsCount > 0) { |
||
| 322 | if ($limit > 0) { |
||
| 323 | $crEvent->setStart($start); |
||
| 324 | $crEvent->setLimit($limit); |
||
| 325 | } |
||
| 326 | // Get All Event |
||
| 327 | $eventsAll = $this->getAll($crEvent); |
||
| 328 | |||
| 329 | return ['count' => $eventsCount, 'eventsAll' => $eventsAll]; |
||
| 330 | } |
||
| 331 | |||
| 332 | return ['count' => 0, 'eventsAll' => []]; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * compare two versions of events |
||
| 337 | * @param $versionOld |
||
| 338 | * @param $versionNew |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getEventsCompare($versionOld, $versionNew) |
||
| 342 | { |
||
| 343 | $changedValues = []; |
||
| 344 | // find changes in important fields of table events |
||
| 345 | $fields = []; |
||
| 346 | $fields[] = ['name' => 'name', 'caption' => \_MA_WGEVENTS_EVENT_NAME, 'type' => 'text']; |
||
| 347 | $fields[] = ['name' => 'desc', 'caption' => \_MA_WGEVENTS_EVENT_DESC, 'type' => 'text']; |
||
| 348 | $fields[] = ['name' => 'datefrom', 'caption' => \_MA_WGEVENTS_EVENT_DATEFROM, 'type' => 'datetime']; |
||
| 349 | $fields[] = ['name' => 'dateto', 'caption' => \_MA_WGEVENTS_EVENT_DATETO, 'type' => 'datetime']; |
||
| 350 | $fields[] = ['name' => 'location', 'caption' => \_MA_WGEVENTS_EVENT_LOCATION, 'type' => 'text']; |
||
| 351 | $fields[] = ['name' => 'fee', 'caption' => \_MA_WGEVENTS_EVENT_FEE, 'type' => 'text']; |
||
| 352 | |||
| 353 | foreach ($fields as $field) { |
||
| 354 | $valueOld = $versionOld->getVar($field['name']); |
||
| 355 | $valueNew = $versionNew->getVar($field['name']); |
||
| 356 | if ($valueOld != $valueNew) { |
||
| 357 | if ('' == $valueNew) { |
||
| 358 | $valueNew = _MA_WGEVENTS_MAIL_REG_MODIFICATION_DELETED; |
||
| 359 | } |
||
| 360 | switch ($field['type']) { |
||
| 361 | case 'datetime': |
||
| 362 | $changedValues[] = [ |
||
| 363 | 'caption' => $field['caption'], |
||
| 364 | 'valueOld' => \formatTimestamp($valueOld, 'm'), |
||
| 365 | 'valueNew' => \formatTimestamp($valueNew, 'm') |
||
| 366 | ]; |
||
| 367 | break; |
||
| 368 | case'default': |
||
| 369 | default: |
||
| 370 | $changedValues[] = [ |
||
| 371 | 'caption' => $field['caption'], |
||
| 372 | 'valueOld' => $valueOld, |
||
| 373 | 'valueNew' => $valueNew |
||
| 374 | ]; |
||
| 375 | break; |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | if (\count($changedValues) > 0) { |
||
| 380 | $mailHandler = new MailHandler(); |
||
| 381 | return $mailHandler->array2table ($changedValues); |
||
| 382 | } |
||
| 383 | |||
| 384 | return ''; |
||
| 385 | } |
||
| 386 | /** |
||
| 387 | * get email recipients for noticiations |
||
| 388 | * @param $registerNotify |
||
| 389 | * @return array|false|string[] |
||
| 390 | */ |
||
| 391 | public function getRecipientsNotify($registerNotify) |
||
| 407 | } |
||
| 408 | /** |
||
| 409 | * get clean date from/to for displaying |
||
| 410 | * @param int $datefrom |
||
| 411 | * @param int $dateto |
||
| 412 | * @param bool $allday |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public function getDateFromToText($datefrom, $dateto, $allday) |
||
| 460 | } |
||
| 461 | } |
||
| 462 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths