| Conditions | 6 |
| Paths | 6 |
| Total Lines | 108 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 70 | public function prepare(INotification $notification, string $languageCode): INotification { |
||
| 71 | $l = $this->l10nFactory->get('polls', $languageCode); |
||
| 72 | if ($notification->getApp() !== 'polls') { |
||
| 73 | throw new \InvalidArgumentException(); |
||
| 74 | } |
||
| 75 | $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('polls', 'polls-black.svg'))); |
||
| 76 | $parameters = $notification->getSubjectParameters(); |
||
| 77 | |||
| 78 | switch ($notification->getSubject()) { |
||
| 79 | case 'invitation': |
||
| 80 | $poll = $this->pollMapper->find(intval($notification->getObjectId())); |
||
| 81 | $owner = $this->userManager->get($poll->getOwner()); |
||
| 82 | |||
| 83 | $notification->setParsedSubject( |
||
| 84 | $l->t('%s invited you to a poll', [$owner->getDisplayName()]) |
||
| 85 | ); |
||
| 86 | |||
| 87 | $notification->setRichSubject( |
||
| 88 | $l->t('{user} has invited you to the poll "%s".', [$poll->getTitle()]), |
||
| 89 | [ |
||
| 90 | 'user' => [ |
||
| 91 | 'type' => 'user', |
||
| 92 | 'id' => $poll->getOwner(), |
||
| 93 | 'name' => $owner->getDisplayName(), |
||
| 94 | ] |
||
| 95 | ] |
||
| 96 | ); |
||
| 97 | $notification->setLink($this->url->linkToRouteAbsolute( |
||
| 98 | 'polls.page.vote', |
||
| 99 | ['id' => $poll->getId()] |
||
| 100 | )); |
||
| 101 | break; |
||
| 102 | |||
| 103 | case 'takeOverPoll': |
||
| 104 | $poll = $this->pollMapper->find(intval($notification->getObjectId())); |
||
| 105 | $newOwner = $this->userManager->get($parameters['actor']); |
||
| 106 | |||
| 107 | $notification->setParsedSubject( |
||
| 108 | $l->t('%s took over your poll', [$newOwner->getDisplayName()]) |
||
| 109 | ); |
||
| 110 | |||
| 111 | $notification->setRichSubject( |
||
| 112 | $l->t('{user} took over your poll "%s" and is the new owner.', [$poll->getTitle()]), |
||
| 113 | [ |
||
| 114 | 'user' => [ |
||
| 115 | 'type' => 'user', |
||
| 116 | 'id' => $newOwner->getUID(), |
||
| 117 | 'name' => $newOwner->getDisplayName(), |
||
| 118 | ] |
||
| 119 | ] |
||
| 120 | ); |
||
| 121 | $notification->setLink($this->url->linkToRouteAbsolute( |
||
| 122 | 'polls.page.vote', |
||
| 123 | ['id' => $poll->getId()] |
||
| 124 | )); |
||
| 125 | break; |
||
| 126 | |||
| 127 | case 'deletePollByOther': |
||
| 128 | $poll = $this->pollMapper->find(intval($notification->getObjectId())); |
||
| 129 | $actor = $this->userManager->get($parameters['actor']); |
||
| 130 | |||
| 131 | $notification->setParsedSubject( |
||
| 132 | $l->t('%s permanently deleted your poll', [$actor->getDisplayName()]) |
||
| 133 | ); |
||
| 134 | |||
| 135 | $notification->setRichSubject( |
||
| 136 | $l->t('{user} permanently deleted your poll "%s".', $parameters['pollTitle']), |
||
| 137 | [ |
||
| 138 | 'user' => [ |
||
| 139 | 'type' => 'user', |
||
| 140 | 'id' => $actor->getUID(), |
||
| 141 | 'name' => $actor->getDisplayName(), |
||
| 142 | ] |
||
| 143 | ] |
||
| 144 | ); |
||
| 145 | $notification->setLink($this->url->linkToRouteAbsolute( |
||
| 146 | 'polls.page.vote', |
||
| 147 | ['id' => $poll->getId()] |
||
| 148 | )); |
||
| 149 | break; |
||
| 150 | |||
| 151 | case 'softDeletePollByOther': |
||
| 152 | $poll = $this->pollMapper->find(intval($notification->getObjectId())); |
||
| 153 | $actor = $this->userManager->get($parameters['actor']); |
||
| 154 | |||
| 155 | $notification->setParsedSubject( |
||
| 156 | $l->t('%s changed the deleted status of your poll', [$actor->getDisplayName()]) |
||
| 157 | ); |
||
| 158 | $notification->setRichSubject( |
||
| 159 | $l->t('{user} changed the deleted status of your poll "%s".', $parameters['pollTitle']), |
||
| 160 | [ |
||
| 161 | 'user' => [ |
||
| 162 | 'type' => 'user', |
||
| 163 | 'id' => $actor->getUID(), |
||
| 164 | 'name' => $actor->getDisplayName(), |
||
| 165 | ] |
||
| 166 | ] |
||
| 167 | ); |
||
| 168 | $notification->setLink($this->url->linkToRouteAbsolute( |
||
| 169 | 'polls.page.vote', |
||
| 170 | ['id' => $poll->getId()] |
||
| 171 | )); |
||
| 172 | break; |
||
| 173 | default: |
||
| 174 | // Unknown subject => Unknown notification => throw |
||
| 175 | throw new \InvalidArgumentException(); |
||
| 176 | } |
||
| 177 | return $notification; |
||
| 178 | } |
||
| 180 |