| Total Complexity | 90 |
| Total Lines | 693 |
| Duplicated Lines | 10.53 % |
| Coverage | 8.3% |
| Changes | 7 | ||
| 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 |
||
| 58 | class PageController extends Controller { |
||
| 59 | |||
| 60 | private $userId; |
||
| 61 | private $commentMapper; |
||
| 62 | private $dateMapper; |
||
| 63 | private $eventMapper; |
||
| 64 | private $notificationMapper; |
||
| 65 | private $participationMapper; |
||
| 66 | private $participationTextMapper; |
||
| 67 | private $textMapper; |
||
| 68 | private $urlGenerator; |
||
| 69 | private $userMgr; |
||
| 70 | private $avatarManager; |
||
| 71 | private $logger; |
||
| 72 | private $trans; |
||
| 73 | private $groupManager; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * PageController constructor. |
||
| 77 | * @param string $appName |
||
| 78 | * @param IRequest $request |
||
| 79 | * @param IUserManager $userMgr |
||
| 80 | * @param IGroupManager $groupManager |
||
| 81 | * @param IAvatarManager $avatarManager |
||
| 82 | * @param ILogger $logger |
||
| 83 | * @param IL10N $trans |
||
| 84 | * @param IURLGenerator $urlGenerator |
||
| 85 | * @param string $userId |
||
| 86 | * @param CommentMapper $commentMapper |
||
| 87 | * @param DateMapper $dateMapper |
||
| 88 | * @param EventMapper $eventMapper |
||
| 89 | * @param NotificationMapper $notificationMapper |
||
| 90 | * @param ParticipationMapper $ParticipationMapper |
||
| 91 | * @param ParticipationTextMapper $ParticipationTextMapper |
||
| 92 | * @param TextMapper $textMapper |
||
| 93 | */ |
||
| 94 | 1 | public function __construct( |
|
| 127 | 1 | } |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @NoAdminRequired |
||
| 131 | * @NoCSRFRequired |
||
| 132 | */ |
||
| 133 | 1 | public function index() { |
|
| 134 | 1 | $polls = $this->eventMapper->findAllForUserWithInfo($this->userId); |
|
| 135 | 1 | $comments = $this->commentMapper->findDistinctByUser($this->userId); |
|
| 136 | 1 | $partic = $this->participationMapper->findDistinctByUser($this->userId); |
|
| 137 | 1 | $particText = $this->participationTextMapper->findDistinctByUser($this->userId); |
|
| 138 | 1 | $response = new TemplateResponse('polls', 'main.tmpl', [ |
|
| 139 | 1 | 'polls' => $polls, |
|
| 140 | 1 | 'comments' => $comments, |
|
| 141 | 1 | 'participations' => $partic, |
|
| 142 | 1 | 'participations_text' => $particText, |
|
| 143 | 1 | 'userId' => $this->userId, |
|
| 144 | 1 | 'userMgr' => $this->userMgr, |
|
| 145 | 1 | 'urlGenerator' => $this->urlGenerator |
|
| 146 | 1 | ]); |
|
| 147 | 1 | $csp = new ContentSecurityPolicy(); |
|
| 148 | 1 | $response->setContentSecurityPolicy($csp); |
|
| 149 | 1 | return $response; |
|
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param int $pollId |
||
| 154 | * @param string $from |
||
| 155 | */ |
||
| 156 | private function sendNotifications($pollId, $from) { |
||
| 157 | $poll = $this->eventMapper->find($pollId); |
||
| 158 | $notifications = $this->notificationMapper->findAllByPoll($pollId); |
||
| 159 | foreach ($notifications as $notification) { |
||
| 160 | if ($from === $notification->getUserId()) { |
||
| 161 | continue; |
||
| 162 | } |
||
| 163 | $email = \OC::$server->getConfig()->getUserValue($notification->getUserId(), 'settings', 'email'); |
||
| 164 | if ($email === null || !filter_var($email, FILTER_VALIDATE_EMAIL)) { |
||
| 165 | continue; |
||
| 166 | } |
||
| 167 | $url = $this->urlGenerator->getAbsoluteURL( |
||
| 168 | $this->urlGenerator->linkToRoute('polls.page.goto_poll', |
||
| 169 | array('hash' => $poll->getHash())) |
||
| 170 | ); |
||
| 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 | $sender = $from; |
||
| 179 | if ($sendUser !== null) { |
||
| 180 | $sender = $sendUser->getDisplayName(); |
||
| 181 | } |
||
| 182 | $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>', |
||
| 183 | array( |
||
| 184 | $rec, |
||
| 185 | $sender, |
||
| 186 | $poll->getTitle(), |
||
| 187 | $url |
||
| 188 | )); |
||
| 189 | |||
| 190 | $msg .= '<br/><br/>'; |
||
| 191 | |||
| 192 | $toName = $this->userMgr->get($notification->getUserId())->getDisplayName(); |
||
| 193 | $subject = $this->trans->t('Polls App - New Activity'); |
||
| 194 | $fromAddress = Util::getDefaultEmailAddress('no-reply'); |
||
| 195 | $fromName = $this->trans->t('Polls App') . ' (' . $from . ')'; |
||
| 196 | |||
| 197 | try { |
||
| 198 | /** @var IMailer $mailer */ |
||
| 199 | $mailer = \OC::$server->getMailer(); |
||
| 200 | /** @var \OC\Mail\Message $message */ |
||
| 201 | $message = $mailer->createMessage(); |
||
| 202 | $message->setSubject($subject); |
||
| 203 | $message->setFrom(array($fromAddress => $fromName)); |
||
| 204 | $message->setTo(array($email => $toName)); |
||
| 205 | $message->setHtmlBody($msg); |
||
| 206 | $mailer->send($message); |
||
| 207 | } catch (\Exception $e) { |
||
| 208 | $message = 'Error sending mail to: ' . $toName . ' (' . $email . ')'; |
||
| 209 | Util::writeLog('polls', $message, Util::ERROR); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @NoAdminRequired |
||
| 216 | * @NoCSRFRequired |
||
| 217 | * @PublicPage |
||
| 218 | * @param string $hash |
||
| 219 | * @return TemplateResponse |
||
| 220 | */ |
||
| 221 | public function gotoPoll($hash) { |
||
| 222 | try { |
||
| 223 | $poll = $this->eventMapper->findByHash($hash); |
||
| 224 | } catch (DoesNotExistException $e) { |
||
| 225 | return new TemplateResponse('polls', 'no.acc.tmpl', []); |
||
| 226 | } |
||
| 227 | if ($poll->getType() === 0) { |
||
| 228 | $dates = $this->dateMapper->findByPoll($poll->getId()); |
||
| 229 | $votes = $this->participationMapper->findByPoll($poll->getId()); |
||
| 230 | $participants = $this->participationMapper->findParticipantsByPoll($poll->getId()); |
||
| 231 | } else { |
||
| 232 | $dates = $this->textMapper->findByPoll($poll->getId()); |
||
| 233 | $votes = $this->participationTextMapper->findByPoll($poll->getId()); |
||
| 234 | $participants = $this->participationTextMapper->findParticipantsByPoll($poll->getId()); |
||
| 235 | } |
||
| 236 | $comments = $this->commentMapper->findByPoll($poll->getId()); |
||
| 237 | try { |
||
| 238 | $notification = $this->notificationMapper->findByUserAndPoll($poll->getId(), $this->userId); |
||
| 239 | } catch (DoesNotExistException $e) { |
||
| 240 | $notification = null; |
||
| 241 | } |
||
| 242 | if ($this->hasUserAccess($poll)) { |
||
| 243 | return new TemplateResponse('polls', 'goto.tmpl', [ |
||
| 244 | 'poll' => $poll, |
||
| 245 | 'dates' => $dates, |
||
| 246 | 'comments' => $comments, |
||
| 247 | 'votes' => $votes, |
||
| 248 | 'participants' => $participants, |
||
| 249 | 'notification' => $notification, |
||
| 250 | 'userId' => $this->userId, |
||
| 251 | 'userMgr' => $this->userMgr, |
||
| 252 | 'urlGenerator' => $this->urlGenerator, |
||
| 253 | 'avatarManager' => $this->avatarManager |
||
| 254 | ]); |
||
| 255 | } else { |
||
| 256 | User::checkLoggedIn(); |
||
| 257 | return new TemplateResponse('polls', 'no.acc.tmpl', []); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @NoAdminRequired |
||
| 263 | * @NoCSRFRequired |
||
| 264 | * @param int $pollId |
||
| 265 | * @return TemplateResponse|RedirectResponse |
||
| 266 | */ |
||
| 267 | public function deletePoll($pollId) { |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @NoAdminRequired |
||
| 286 | * @NoCSRFRequired |
||
| 287 | * @param string $hash |
||
| 288 | * @return TemplateResponse |
||
| 289 | */ |
||
| 290 | public function editPoll($hash) { |
||
| 306 | ]); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @NoAdminRequired |
||
| 311 | * @NoCSRFRequired |
||
| 312 | * @param int $pollId |
||
| 313 | * @param string $pollType |
||
| 314 | * @param string $pollTitle |
||
| 315 | * @param string $pollDesc |
||
| 316 | * @param string $userId |
||
| 317 | * @param string $chosenDates |
||
| 318 | * @param int $expireTs |
||
| 319 | * @param string $accessType |
||
| 320 | * @param string $accessValues |
||
| 321 | * @param bool $isAnonymous |
||
| 322 | * @param bool $hideNames |
||
| 323 | * @return RedirectResponse |
||
| 324 | */ |
||
| 325 | public function updatePoll( |
||
| 326 | $pollId, |
||
| 327 | $pollType, |
||
| 328 | $pollTitle, |
||
| 329 | $pollDesc, |
||
| 330 | $userId, |
||
|
|
|||
| 331 | $chosenDates, |
||
| 332 | $expireTs, |
||
| 333 | $accessType, |
||
| 334 | $accessValues, |
||
| 335 | $isAnonymous, |
||
| 336 | $hideNames |
||
| 337 | ) { |
||
| 338 | |||
| 339 | |||
| 340 | $event = $this->eventMapper->find($pollId); |
||
| 341 | $event->setTitle($pollTitle); |
||
| 342 | $event->setDescription($pollDesc); |
||
| 343 | $event->setIsAnonymous($isAnonymous ? 1 : 0); |
||
| 344 | $event->setFullAnonymous($isAnonymous && $hideNames ? 1 : 0); |
||
| 345 | |||
| 346 | View Code Duplication | if ($accessType === 'select') { |
|
| 347 | if (isset($accessValues)) { |
||
| 348 | $accessValues = json_decode($accessValues); |
||
| 349 | if ($accessValues !== null) { |
||
| 350 | $groups = array(); |
||
| 351 | $users = array(); |
||
| 352 | if ($accessValues->groups !== null) { |
||
| 353 | $groups = $accessValues->groups; |
||
| 354 | } |
||
| 355 | if ($accessValues->users !== null) { |
||
| 356 | $users = $accessValues->users; |
||
| 357 | } |
||
| 358 | $accessType = ''; |
||
| 359 | foreach ($groups as $gid) { |
||
| 360 | $accessType .= $gid . ';'; |
||
| 361 | } |
||
| 362 | foreach ($users as $uid) { |
||
| 363 | $accessType .= $uid . ';'; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 | $event->setAccess($accessType); |
||
| 369 | /** @var string[] $chosenDates */ |
||
| 370 | $chosenDates = json_decode($chosenDates); |
||
| 371 | |||
| 372 | $expire = null; |
||
| 373 | if ($expireTs !== 0 && $expireTs !== '') { |
||
| 374 | $expire = date('Y-m-d H:i:s', $expireTs); |
||
| 375 | } |
||
| 376 | $event->setExpire($expire); |
||
| 377 | |||
| 378 | $this->dateMapper->deleteByPoll($pollId); |
||
| 379 | $this->textMapper->deleteByPoll($pollId); |
||
| 380 | if ($pollType === 'event') { |
||
| 381 | $event->setType(0); |
||
| 382 | $this->eventMapper->update($event); |
||
| 383 | sort($chosenDates); |
||
| 384 | View Code Duplication | foreach ($chosenDates as $el) { |
|
| 385 | $date = new Date(); |
||
| 386 | $date->setPollId($pollId); |
||
| 387 | $date->setDt(date('Y-m-d H:i:s', $el)); |
||
| 388 | $this->dateMapper->insert($date); |
||
| 389 | } |
||
| 390 | } else { |
||
| 391 | $event->setType(1); |
||
| 392 | $this->eventMapper->update($event); |
||
| 393 | foreach ($chosenDates as $el) { |
||
| 394 | $text = new Text(); |
||
| 395 | $text->setPollId($pollId); |
||
| 396 | $text->setText($el); |
||
| 397 | $this->textMapper->insert($text); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | $url = $this->urlGenerator->linkToRoute('polls.page.index'); |
||
| 401 | return new RedirectResponse($url); |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @NoAdminRequired |
||
| 406 | * @NoCSRFRequired |
||
| 407 | */ |
||
| 408 | public function createPoll() { |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @NoAdminRequired |
||
| 415 | * @NoCSRFRequired |
||
| 416 | * @param string $pollType |
||
| 417 | * @param string $pollTitle |
||
| 418 | * @param string $pollDesc |
||
| 419 | * @param string $userId |
||
| 420 | * @param string $chosenDates |
||
| 421 | * @param int $expireTs |
||
| 422 | * @param string $accessType |
||
| 423 | * @param string $accessValues |
||
| 424 | * @param bool $isAnonymous |
||
| 425 | * @param bool $hideNames |
||
| 426 | * @return RedirectResponse |
||
| 427 | */ |
||
| 428 | public function insertPoll( |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @NoAdminRequired |
||
| 516 | * @NoCSRFRequired |
||
| 517 | * @PublicPage |
||
| 518 | * @param int $pollId |
||
| 519 | * @param string $userId |
||
| 520 | * @param string $types |
||
| 521 | * @param string $dates |
||
| 522 | * @param bool $receiveNotifications |
||
| 523 | * @param bool $changed |
||
| 524 | * @return RedirectResponse |
||
| 525 | */ |
||
| 526 | public function insertVote($pollId, $userId, $types, $dates, $receiveNotifications, $changed) { |
||
| 527 | if ($this->userId !== null) { |
||
| 528 | if ($receiveNotifications) { |
||
| 529 | try { |
||
| 530 | //check if user already set notification for this poll |
||
| 531 | $this->notificationMapper->findByUserAndPoll($pollId, $userId); |
||
| 532 | } catch (DoesNotExistException $e) { |
||
| 533 | //insert if not exist |
||
| 534 | $not = new Notification(); |
||
| 535 | $not->setUserId($userId); |
||
| 536 | $not->setPollId($pollId); |
||
| 537 | $this->notificationMapper->insert($not); |
||
| 538 | } |
||
| 539 | } else { |
||
| 540 | try { |
||
| 541 | //delete if entry is in db |
||
| 542 | $not = $this->notificationMapper->findByUserAndPoll($pollId, $userId); |
||
| 543 | $this->notificationMapper->delete($not); |
||
| 544 | } catch (DoesNotExistException $e) { |
||
| 545 | //doesn't exist in db, nothing to do |
||
| 546 | } |
||
| 547 | } |
||
| 548 | } |
||
| 549 | $poll = $this->eventMapper->find($pollId); |
||
| 550 | if ($changed) { |
||
| 551 | $dates = json_decode($dates); |
||
| 552 | $types = json_decode($types); |
||
| 553 | $count_dates = count($dates); |
||
| 554 | if ($poll->getType() === 0) { |
||
| 555 | $this->participationMapper->deleteByPollAndUser($pollId, $userId); |
||
| 556 | } else { |
||
| 557 | $this->participationTextMapper->deleteByPollAndUser($pollId, $userId); |
||
| 558 | } |
||
| 559 | for ($i = 0; $i < $count_dates; $i++) { |
||
| 560 | if ($poll->getType() === 0) { |
||
| 561 | $part = new Participation(); |
||
| 562 | $part->setPollId($pollId); |
||
| 563 | $part->setUserId($userId); |
||
| 564 | $part->setDt(date('Y-m-d H:i:s', $dates[$i])); |
||
| 565 | $part->setType($types[$i]); |
||
| 566 | $this->participationMapper->insert($part); |
||
| 567 | } else { |
||
| 568 | $part = new ParticipationText(); |
||
| 569 | $part->setPollId($pollId); |
||
| 570 | $part->setUserId($userId); |
||
| 571 | $part->setText($dates[$i]); |
||
| 572 | $part->setType($types[$i]); |
||
| 573 | $this->participationTextMapper->insert($part); |
||
| 574 | } |
||
| 575 | |||
| 576 | } |
||
| 577 | $this->sendNotifications($pollId, $userId); |
||
| 578 | } |
||
| 579 | $hash = $poll->getHash(); |
||
| 580 | $url = $this->urlGenerator->linkToRoute('polls.page.goto_poll', ['hash' => $hash]); |
||
| 581 | return new RedirectResponse($url); |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @NoAdminRequired |
||
| 586 | * @NoCSRFRequired |
||
| 587 | * @PublicPage |
||
| 588 | * @param int $pollId |
||
| 589 | * @param string $userId |
||
| 590 | * @param string $commentBox |
||
| 591 | * @return JSONResponse |
||
| 592 | */ |
||
| 593 | public function insertComment($pollId, $userId, $commentBox) { |
||
| 594 | $comment = new Comment(); |
||
| 595 | $comment->setPollId($pollId); |
||
| 596 | $comment->setUserId($userId); |
||
| 597 | $comment->setComment($commentBox); |
||
| 598 | $comment->setDt(date('Y-m-d H:i:s')); |
||
| 599 | $this->commentMapper->insert($comment); |
||
| 600 | $this->sendNotifications($pollId, $userId); |
||
| 601 | $displayName = $userId; |
||
| 602 | $user = $this->userMgr->get($userId); |
||
| 603 | if ($user !== null) { |
||
| 604 | $displayName = $user->getDisplayName(); |
||
| 605 | } |
||
| 606 | return new JSONResponse(array( |
||
| 607 | 'comment' => $commentBox, |
||
| 608 | 'date' => date('Y-m-d H:i:s'), |
||
| 609 | 'userId' => $userId, |
||
| 610 | 'displayName' => $displayName |
||
| 611 | )); |
||
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @NoAdminRequired |
||
| 616 | * @NoCSRFRequired |
||
| 617 | * @param string $searchTerm |
||
| 618 | * @param string $groups |
||
| 619 | * @param string $users |
||
| 620 | * @return array |
||
| 621 | */ |
||
| 622 | public function search($searchTerm, $groups, $users) { |
||
| 623 | return array_merge($this->searchForGroups($searchTerm, $groups), $this->searchForUsers($searchTerm, $users)); |
||
| 624 | } |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @NoAdminRequired |
||
| 628 | * @NoCSRFRequired |
||
| 629 | * @param string $searchTerm |
||
| 630 | * @param string $groups |
||
| 631 | * @return array |
||
| 632 | */ |
||
| 633 | public function searchForGroups($searchTerm, $groups) { |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * @NoAdminRequired |
||
| 654 | * @NoCSRFRequired |
||
| 655 | * @param string $searchTerm |
||
| 656 | * @param string $users |
||
| 657 | * @return array |
||
| 658 | */ |
||
| 659 | public function searchForUsers($searchTerm, $users) { |
||
| 660 | $selectedUsers = json_decode($users); |
||
| 661 | Util::writeLog('polls', print_r($selectedUsers, true), Util::ERROR); |
||
| 662 | $userNames = $this->userMgr->searchDisplayName($searchTerm); |
||
| 663 | $users = array(); |
||
| 664 | $sUsers = array(); |
||
| 665 | foreach ($selectedUsers as $su) { |
||
| 666 | $sUsers[] = str_replace('user_', '', $su); |
||
| 667 | } |
||
| 668 | foreach ($userNames as $u) { |
||
| 669 | $alreadyAdded = false; |
||
| 670 | foreach ($sUsers as &$su) { |
||
| 671 | if ($su === $u->getUID()) { |
||
| 672 | unset($su); |
||
| 673 | $alreadyAdded = true; |
||
| 674 | break; |
||
| 675 | } |
||
| 676 | } |
||
| 677 | if (!$alreadyAdded) { |
||
| 678 | $users[] = array('uid' => $u->getUID(), 'displayName' => $u->getDisplayName(), 'isGroup' => false); |
||
| 679 | } else { |
||
| 680 | continue; |
||
| 681 | } |
||
| 682 | } |
||
| 683 | return $users; |
||
| 684 | } |
||
| 685 | |||
| 686 | /** |
||
| 687 | * @NoAdminRequired |
||
| 688 | * @NoCSRFRequired |
||
| 689 | * @param string $username |
||
| 690 | * @return string |
||
| 691 | */ |
||
| 692 | public function getDisplayName($username) { |
||
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @return \OCP\IGroup[] |
||
| 698 | */ |
||
| 699 | private function getGroups() { |
||
| 700 | if (class_exists('\OC_Group')) { |
||
| 701 | // Nextcloud <= 11, ownCloud |
||
| 702 | return \OC_Group::getUserGroups($this->userId); |
||
| 703 | } |
||
| 704 | // Nextcloud >= 12 |
||
| 705 | $groups = $this->groupManager->getUserGroups(\OC::$server->getUserSession()->getUser()); |
||
| 706 | return array_map(function ($group) { |
||
| 707 | return $group->getGID(); |
||
| 708 | }, $groups); |
||
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * @param Event $poll |
||
| 713 | * @return bool |
||
| 714 | */ |
||
| 715 | private function hasUserAccess($poll) { |
||
| 751 | } |
||
| 752 | } |
||
| 753 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.