| Total Complexity | 72 |
| Total Lines | 695 |
| Duplicated Lines | 8.92 % |
| Changes | 8 | ||
| Bugs | 1 | Features | 1 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PageController 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 PageController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class PageController extends Controller { |
||
| 57 | |||
| 58 | private $userId; |
||
| 59 | private $commentMapper; |
||
| 60 | private $dateMapper; |
||
| 61 | private $eventMapper; |
||
| 62 | private $notificationMapper; |
||
| 63 | private $participationMapper; |
||
| 64 | private $participationTextMapper; |
||
| 65 | private $textMapper; |
||
| 66 | private $urlGenerator; |
||
| 67 | private $manager; |
||
| 68 | private $avatarManager; |
||
| 69 | private $logger; |
||
| 70 | private $trans; |
||
| 71 | private $userMgr; |
||
| 72 | private $groupManager; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * PageController constructor. |
||
| 76 | * @param $appName |
||
| 77 | * @param IRequest $request |
||
| 78 | * @param IUserManager $manager |
||
| 79 | * @param IGroupManager $groupManager |
||
| 80 | * @param IAvatarManager $avatarManager |
||
| 81 | * @param ILogger $logger |
||
| 82 | * @param IL10N $trans |
||
| 83 | * @param IURLGenerator $urlGenerator |
||
| 84 | * @param $userId |
||
| 85 | * @param CommentMapper $commentMapper |
||
| 86 | * @param DateMapper $dateMapper |
||
| 87 | * @param EventMapper $eventMapper |
||
| 88 | * @param NotificationMapper $notificationMapper |
||
| 89 | * @param ParticipationMapper $ParticipationMapper |
||
| 90 | * @param ParticipationTextMapper $ParticipationTextMapper |
||
| 91 | * @param TextMapper $textMapper |
||
| 92 | */ |
||
| 93 | public function __construct( |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @NoAdminRequired |
||
| 131 | * @NoCSRFRequired |
||
| 132 | */ |
||
| 133 | public function index() { |
||
| 134 | $polls = $this->eventMapper->findAllForUserWithInfo($this->userId); |
||
| 135 | $comments = $this->commentMapper->findDistinctByUser($this->userId); |
||
| 136 | $partic = $this->participationMapper->findDistinctByUser($this->userId); |
||
| 137 | $particText = $this->participationTextMapper->findDistinctByUser($this->userId); |
||
| 138 | $response = new TemplateResponse('polls', 'main.tmpl', [ |
||
| 139 | 'polls' => $polls, |
||
| 140 | 'comments' => $comments, |
||
| 141 | 'participations' => $partic, |
||
| 142 | 'participations_text' => $particText, |
||
| 143 | 'userId' => $this->userId, |
||
| 144 | 'userMgr' => $this->manager, |
||
| 145 | 'urlGenerator' => $this->urlGenerator |
||
| 146 | ]); |
||
| 147 | if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) { |
||
| 148 | $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
||
| 149 | $response->setContentSecurityPolicy($csp); |
||
| 150 | } |
||
| 151 | return $response; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $pollId |
||
| 156 | * @param string $from |
||
| 157 | */ |
||
| 158 | private function sendNotifications($pollId, $from) { |
||
| 159 | $poll = $this->eventMapper->find($pollId); |
||
| 160 | $notifications = $this->notificationMapper->findAllByPoll($pollId); |
||
| 161 | foreach ($notifications as $notification) { |
||
| 162 | if ($from === $notification->getUserId()) { |
||
| 163 | continue; |
||
| 164 | } |
||
| 165 | $email = \OC::$server->getConfig()->getUserValue($notification->getUserId(), 'settings', 'email'); |
||
| 166 | if (strlen($email) === 0 || !isset($email)) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | $url = \OC::$server->getURLGenerator()->getAbsoluteURL(\OC::$server->getURLGenerator()->linkToRoute('polls.page.goto_poll', |
||
| 170 | array('hash' => $poll->getHash()))); |
||
| 171 | |||
| 172 | $recUser = $this->userMgr->get($notification->getUserId()); |
||
| 173 | $sendUser = $this->userMgr->get($from); |
||
| 174 | $rec = ""; |
||
| 175 | if ($recUser !== null) { |
||
| 176 | $rec = $recUser->getDisplayName(); |
||
| 177 | } |
||
| 178 | if ($sendUser !== null) { |
||
| 179 | $sender = $sendUser->getDisplayName(); |
||
| 180 | } else { |
||
| 181 | $sender = $from; |
||
| 182 | } |
||
| 183 | $msg = $this->trans->t('Hello %s,<br/><br/><strong>%s</strong> participated in the poll \'%s\'.<br/><br/>To go directly to the poll, you can use this <a href="%s">link</a>', |
||
| 184 | array( |
||
| 185 | $rec, |
||
| 186 | $sender, |
||
| 187 | $poll->getTitle(), |
||
| 188 | $url |
||
| 189 | )); |
||
| 190 | |||
| 191 | $msg .= "<br/><br/>"; |
||
| 192 | |||
| 193 | $toName = $this->userMgr->get($notification->getUserId())->getDisplayName(); |
||
| 194 | $subject = $this->trans->t('Polls App - New Comment'); |
||
| 195 | $fromAddress = Util::getDefaultEmailAddress('no-reply'); |
||
| 196 | $fromName = $this->trans->t("Polls App") . ' (' . $from . ')'; |
||
| 197 | |||
| 198 | try { |
||
| 199 | $mailer = \OC::$server->getMailer(); |
||
| 200 | $message = $mailer->createMessage(); |
||
| 201 | $message->setSubject($subject); |
||
| 202 | $message->setFrom(array($fromAddress => $fromName)); |
||
| 203 | $message->setTo(array($email => $toName)); |
||
| 204 | $message->setHtmlBody($msg); |
||
| 205 | $mailer->send($message); |
||
| 206 | } catch (\Exception $e) { |
||
| 207 | $message = 'Error sending mail to: ' . $toName . ' (' . $email . ')'; |
||
| 208 | Util::writeLog("polls", $message, Util::ERROR); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @NoAdminRequired |
||
| 215 | * @NoCSRFRequired |
||
| 216 | * @PublicPage |
||
| 217 | * @param string $hash |
||
| 218 | * @return TemplateResponse |
||
| 219 | */ |
||
| 220 | public function gotoPoll($hash) { |
||
| 221 | $poll = $this->eventMapper->findByHash($hash); |
||
| 222 | if ($poll->getType() == '0') { |
||
| 223 | $dates = $this->dateMapper->findByPoll($poll->getId()); |
||
| 224 | $votes = $this->participationMapper->findByPoll($poll->getId()); |
||
| 225 | } else { |
||
| 226 | $dates = $this->textMapper->findByPoll($poll->getId()); |
||
| 227 | $votes = $this->participationTextMapper->findByPoll($poll->getId()); |
||
| 228 | } |
||
| 229 | $comments = $this->commentMapper->findByPoll($poll->getId()); |
||
| 230 | try { |
||
| 231 | $notification = $this->notificationMapper->findByUserAndPoll($poll->getId(), $this->userId); |
||
| 232 | } catch (DoesNotExistException $e) { |
||
| 233 | $notification = null; |
||
| 234 | } |
||
| 235 | if ($this->hasUserAccess($poll)) { |
||
| 236 | return new TemplateResponse('polls', 'goto.tmpl', [ |
||
| 237 | 'poll' => $poll, |
||
| 238 | 'dates' => $dates, |
||
| 239 | 'comments' => $comments, |
||
| 240 | 'votes' => $votes, |
||
| 241 | 'notification' => $notification, |
||
| 242 | 'userId' => $this->userId, |
||
| 243 | 'userMgr' => $this->manager, |
||
| 244 | 'urlGenerator' => $this->urlGenerator, |
||
| 245 | 'avatarManager' => $this->avatarManager |
||
| 246 | ]); |
||
| 247 | } else { |
||
| 248 | User::checkLoggedIn(); |
||
| 249 | return new TemplateResponse('polls', 'no.acc.tmpl', []); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @NoAdminRequired |
||
| 255 | * @NoCSRFRequired |
||
| 256 | * @param string $pollId |
||
| 257 | * @return RedirectResponse |
||
| 258 | */ |
||
| 259 | public function deletePoll($pollId) { |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @NoAdminRequired |
||
| 274 | * @NoCSRFRequired |
||
| 275 | * @param string $hash |
||
| 276 | * @return TemplateResponse |
||
| 277 | */ |
||
| 278 | public function editPoll($hash) { |
||
| 279 | $poll = $this->eventMapper->findByHash($hash); |
||
| 280 | if ($this->userId !== $poll->getOwner()) { |
||
| 281 | return new TemplateResponse('polls', 'no.create.tmpl'); |
||
| 282 | } |
||
| 283 | if ($poll->getType() == '0') { |
||
| 284 | $dates = $this->dateMapper->findByPoll($poll->getId()); |
||
| 285 | } else { |
||
| 286 | $dates = $this->textMapper->findByPoll($poll->getId()); |
||
| 287 | } |
||
| 288 | return new TemplateResponse('polls', 'create.tmpl', [ |
||
| 289 | 'poll' => $poll, |
||
| 290 | 'dates' => $dates, |
||
| 291 | 'userId' => $this->userId, |
||
| 292 | 'userMgr' => $this->manager, |
||
| 293 | 'urlGenerator' => $this->urlGenerator |
||
| 294 | ]); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @NoAdminRequired |
||
| 299 | * @NoCSRFRequired |
||
| 300 | * @param $pollId |
||
| 301 | * @param $pollType |
||
| 302 | * @param $pollTitle |
||
| 303 | * @param $pollDesc |
||
| 304 | * @param $userId |
||
| 305 | * @param $chosenDates |
||
| 306 | * @param $expireTs |
||
| 307 | * @param $accessType |
||
| 308 | * @param $accessValues |
||
| 309 | * @return RedirectResponse |
||
| 310 | */ |
||
| 311 | public function updatePoll( |
||
| 312 | $pollId, |
||
| 313 | $pollType, |
||
| 314 | $pollTitle, |
||
| 315 | $pollDesc, |
||
| 316 | $userId, |
||
| 317 | $chosenDates, |
||
| 318 | $expireTs, |
||
| 319 | $accessType, |
||
| 320 | $accessValues |
||
| 321 | ) { |
||
| 322 | $event = $this->eventMapper->find($pollId); |
||
| 323 | $event->setTitle(htmlspecialchars($pollTitle)); |
||
| 324 | $event->setDescription(htmlspecialchars($pollDesc)); |
||
| 325 | |||
| 326 | View Code Duplication | if ($accessType === 'select') { |
|
| 327 | if (isset($accessValues)) { |
||
| 328 | $accessValues = json_decode($accessValues); |
||
| 329 | if ($accessValues !== null) { |
||
| 330 | $groups = array(); |
||
| 331 | $users = array(); |
||
| 332 | if ($accessValues->groups !== null) { |
||
| 333 | $groups = $accessValues->groups; |
||
| 334 | } |
||
| 335 | if ($accessValues->users !== null) { |
||
| 336 | $users = $accessValues->users; |
||
| 337 | } |
||
| 338 | $accessType = ''; |
||
| 339 | foreach ($groups as $gid) { |
||
| 340 | $accessType .= $gid . ';'; |
||
| 341 | } |
||
| 342 | foreach ($users as $uid) { |
||
| 343 | $accessType .= $uid . ';'; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | $event->setAccess($accessType); |
||
| 349 | |||
| 350 | $chosenDates = json_decode($chosenDates); |
||
| 351 | |||
| 352 | $expire = null; |
||
| 353 | View Code Duplication | if ($expireTs !== null && $expireTs !== '') { |
|
| 354 | $expire = date('Y-m-d H:i:s', $expireTs + 60 * 60 * 24); //add one day, so it expires at the end of a day |
||
| 355 | } |
||
| 356 | $event->setExpire($expire); |
||
| 357 | |||
| 358 | $this->dateMapper->deleteByPoll($pollId); |
||
| 359 | $this->textMapper->deleteByPoll($pollId); |
||
| 360 | if ($pollType === 'event') { |
||
| 361 | $event->setType(0); |
||
| 362 | $this->eventMapper->update($event); |
||
| 363 | sort($chosenDates); |
||
| 364 | View Code Duplication | foreach ($chosenDates as $el) { |
|
| 365 | $date = new Date(); |
||
| 366 | $date->setPollId($pollId); |
||
| 367 | $date->setDt(date('Y-m-d H:i:s', $el)); |
||
| 368 | $this->dateMapper->insert($date); |
||
| 369 | } |
||
| 370 | } else { |
||
| 371 | $event->setType(1); |
||
| 372 | $this->eventMapper->update($event); |
||
| 373 | foreach ($chosenDates as $el) { |
||
| 374 | $text = new Text(); |
||
| 375 | $text->setText($el); |
||
| 376 | $text->setPollId($pollId); |
||
| 377 | $this->textMapper->insert($text); |
||
| 378 | } |
||
| 379 | } |
||
| 380 | $url = $this->urlGenerator->linkToRoute('polls.page.index'); |
||
| 381 | return new RedirectResponse($url); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @NoAdminRequired |
||
| 386 | * @NoCSRFRequired |
||
| 387 | */ |
||
| 388 | public function createPoll() { |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @NoAdminRequired |
||
| 395 | * @NoCSRFRequired |
||
| 396 | * @param $pollType |
||
| 397 | * @param $pollTitle |
||
| 398 | * @param $pollDesc |
||
| 399 | * @param $userId |
||
| 400 | * @param $chosenDates |
||
| 401 | * @param $expireTs |
||
| 402 | * @param $accessType |
||
| 403 | * @param $accessValues |
||
| 404 | * @param $isAnonymous |
||
| 405 | * @param $hideNames |
||
| 406 | * @return RedirectResponse |
||
| 407 | */ |
||
| 408 | public function insertPoll( |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @NoAdminRequired |
||
| 494 | * @NoCSRFRequired |
||
| 495 | * @PublicPage |
||
| 496 | * @param $pollId |
||
| 497 | * @param $userId |
||
| 498 | * @param $types |
||
| 499 | * @param $dates |
||
| 500 | * @param $receiveNotifications |
||
| 501 | * @param $changed |
||
| 502 | * @return RedirectResponse |
||
| 503 | */ |
||
| 504 | public function insertVote($pollId, $userId, $types, $dates, $receiveNotifications, $changed) { |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @NoAdminRequired |
||
| 564 | * @NoCSRFRequired |
||
| 565 | * @PublicPage |
||
| 566 | * @param $pollId |
||
| 567 | * @param $userId |
||
| 568 | * @param $commentBox |
||
| 569 | * @return JSONResponse |
||
| 570 | */ |
||
| 571 | public function insertComment($pollId, $userId, $commentBox) { |
||
| 588 | )); |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @NoAdminRequired |
||
| 593 | * @NoCSRFRequired |
||
| 594 | * @param $searchTerm |
||
| 595 | * @param $groups |
||
| 596 | * @param $users |
||
| 597 | * @return array |
||
| 598 | */ |
||
| 599 | /* public function search($searchTerm, $groups, $users) { |
||
| 600 | return array_merge($this->searchForGroups($searchTerm, $groups), $this->searchForUsers($searchTerm, $users)); |
||
| 601 | } |
||
| 602 | */ |
||
| 603 | /** |
||
| 604 | * @NoAdminRequired |
||
| 605 | * @NoCSRFRequired |
||
| 606 | * @param $searchTerm |
||
| 607 | * @param $groups |
||
| 608 | * @return array |
||
| 609 | */ |
||
| 610 | /* public function searchForGroups($searchTerm, $groups) { |
||
| 611 | $selectedGroups = json_decode($groups); |
||
| 612 | $groups = $this->groupManager->search($searchTerm); |
||
| 613 | $gids = array(); |
||
| 614 | $sgids = array(); |
||
| 615 | foreach ($selectedGroups as $sg) { |
||
| 616 | $sgids[] = str_replace('group_', '', $sg); |
||
| 617 | } |
||
| 618 | foreach ($groups as $g) { |
||
| 619 | $gids[] = $g->getGID(); |
||
| 620 | } |
||
| 621 | $diffGids = array_diff($gids, $sgids); |
||
| 622 | $gids = array(); |
||
| 623 | foreach ($diffGids as $g) { |
||
| 624 | $gids[] = ['gid' => $g, 'isGroup' => true]; |
||
| 625 | } |
||
| 626 | return $gids; |
||
| 627 | } |
||
| 628 | */ |
||
| 629 | /** |
||
| 630 | * @NoAdminRequired |
||
| 631 | * @NoCSRFRequired |
||
| 632 | * @param $searchTerm |
||
| 633 | * @param $users |
||
| 634 | * @return array |
||
| 635 | */ |
||
| 636 | /* public function searchForUsers($searchTerm, $users) { |
||
| 637 | $selectedUsers = json_decode($users); |
||
| 638 | Util::writeLog("polls", print_r($selectedUsers, true), Util::ERROR); |
||
| 639 | $userNames = $this->userMgr->searchDisplayName($searchTerm); |
||
| 640 | $users = array(); |
||
| 641 | $sUsers = array(); |
||
| 642 | foreach ($selectedUsers as $su) { |
||
| 643 | $sUsers[] = str_replace('user_', '', $su); |
||
| 644 | } |
||
| 645 | foreach ($userNames as $u) { |
||
| 646 | $alreadyAdded = false; |
||
| 647 | foreach ($sUsers as &$su) { |
||
| 648 | if ($su === $u->getUID()) { |
||
| 649 | unset($su); |
||
| 650 | $alreadyAdded = true; |
||
| 651 | break; |
||
| 652 | } |
||
| 653 | } |
||
| 654 | if (!$alreadyAdded) { |
||
| 655 | $users[] = array('uid' => $u->getUID(), 'displayName' => $u->getDisplayName(), 'isGroup' => false); |
||
| 656 | } else { |
||
| 657 | continue; |
||
| 658 | } |
||
| 659 | } |
||
| 660 | return $users; |
||
| 661 | } |
||
| 662 | */ |
||
| 663 | /** |
||
| 664 | * @NoAdminRequired |
||
| 665 | * @NoCSRFRequired |
||
| 666 | * @param $username |
||
| 667 | * @return string |
||
| 668 | */ |
||
| 669 | /* public function getDisplayName($username) { |
||
| 670 | return $this->manager->get($username)->getDisplayName(); |
||
| 671 | } |
||
| 672 | */ |
||
| 673 | /** |
||
| 674 | * @return Event[] |
||
| 675 | */ |
||
| 676 | /* public function getPollsForUser() { |
||
| 677 | return $this->eventMapper->findAllForUser($this->userId); |
||
| 678 | } |
||
| 679 | */ |
||
| 680 | /** |
||
| 681 | * @param $user |
||
| 682 | * @return Event[] |
||
| 683 | */ |
||
| 684 | /* public function getPollsForUserWithInfo($user = null) { |
||
| 685 | if ($user === null) { |
||
| 686 | return $this->eventMapper->findAllForUserWithInfo($this->userId); |
||
| 687 | } else { |
||
| 688 | return $this->eventMapper->findAllForUserWithInfo($user); |
||
| 689 | } |
||
| 690 | } |
||
| 691 | */ |
||
| 692 | /** |
||
| 693 | * @return array |
||
| 694 | */ |
||
| 695 | /* public function getGroups() { |
||
| 696 | // $this->requireLogin(); |
||
| 697 | if (class_exists('\OC_Group', true)) { |
||
| 698 | // Nextcloud <= 11, ownCloud |
||
| 699 | return \OC_Group::getUserGroups($this->userId); |
||
| 700 | } |
||
| 701 | // Nextcloud >= 12 |
||
| 702 | $groups = \OC::$server->getGroupManager()->getUserGroups(\OC::$server->getUserSession()->getUser()); |
||
| 703 | return array_map(function ($group) { |
||
| 704 | return $group->getGID(); |
||
| 705 | }, $groups); |
||
| 706 | } |
||
| 707 | */ |
||
| 708 | /** |
||
| 709 | * @param $poll |
||
| 710 | * @return bool |
||
| 711 | */ |
||
| 712 | private function hasUserAccess($poll) { |
||
| 753 |
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