| Conditions | 7 |
| Paths | 7 |
| Total Lines | 113 |
| Code Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 82 | public function prepare(INotification $notification, string $languageCode): INotification { |
||
| 83 | $l = $this->l10nFactory->get('polls', $languageCode); |
||
| 84 | if ($notification->getApp() !== 'polls') { |
||
| 85 | throw new \InvalidArgumentException(); |
||
| 86 | } |
||
| 87 | |||
| 88 | try { |
||
| 89 | $poll = $this->pollMapper->find(intval($notification->getObjectId())); |
||
| 90 | } catch (DoesNotExistException $e) { |
||
| 91 | $this->notificationService->removeNotification(intval($notification->getObjectId())); |
||
| 92 | throw new \InvalidArgumentException(); |
||
| 93 | } |
||
| 94 | |||
| 95 | $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('polls', 'polls-black.svg'))); |
||
| 96 | $parameters = $notification->getSubjectParameters(); |
||
| 97 | |||
| 98 | switch ($notification->getSubject()) { |
||
| 99 | case 'invitation': |
||
| 100 | |||
| 101 | $owner = $this->userManager->get($poll->getOwner()); |
||
| 102 | |||
| 103 | $notification->setParsedSubject( |
||
| 104 | $l->t('%s invited you to a poll', [$owner->getDisplayName()]) |
||
| 105 | ); |
||
| 106 | |||
| 107 | $notification->setRichSubject( |
||
| 108 | $l->t('{user} has invited you to the poll "%s".', [$poll->getTitle()]), |
||
| 109 | [ |
||
| 110 | 'user' => [ |
||
| 111 | 'type' => 'user', |
||
| 112 | 'id' => $poll->getOwner(), |
||
| 113 | 'name' => $owner->getDisplayName(), |
||
| 114 | ] |
||
| 115 | ] |
||
| 116 | ); |
||
| 117 | $notification->setLink($this->url->linkToRouteAbsolute( |
||
| 118 | 'polls.page.vote', |
||
| 119 | ['id' => $poll->getId()] |
||
| 120 | )); |
||
| 121 | break; |
||
| 122 | |||
| 123 | case 'takeOverPoll': |
||
| 124 | $newOwner = $this->userManager->get($parameters['actor']); |
||
| 125 | |||
| 126 | $notification->setParsedSubject( |
||
| 127 | $l->t('%s took over your poll', [$newOwner->getDisplayName()]) |
||
| 128 | ); |
||
| 129 | |||
| 130 | $notification->setRichSubject( |
||
| 131 | $l->t('{user} took over your poll "%s" and is the new owner.', [$poll->getTitle()]), |
||
| 132 | [ |
||
| 133 | 'user' => [ |
||
| 134 | 'type' => 'user', |
||
| 135 | 'id' => $newOwner->getUID(), |
||
| 136 | 'name' => $newOwner->getDisplayName(), |
||
| 137 | ] |
||
| 138 | ] |
||
| 139 | ); |
||
| 140 | $notification->setLink($this->url->linkToRouteAbsolute( |
||
| 141 | 'polls.page.vote', |
||
| 142 | ['id' => $poll->getId()] |
||
| 143 | )); |
||
| 144 | break; |
||
| 145 | |||
| 146 | case 'deletePollByOther': |
||
| 147 | $actor = $this->userManager->get($parameters['actor']); |
||
| 148 | |||
| 149 | $notification->setParsedSubject( |
||
| 150 | $l->t('%s permanently deleted your poll', [$actor->getDisplayName()]) |
||
| 151 | ); |
||
| 152 | |||
| 153 | $notification->setRichSubject( |
||
| 154 | $l->t('{user} permanently deleted your poll "%s".', $parameters['pollTitle']), |
||
| 155 | [ |
||
| 156 | 'user' => [ |
||
| 157 | 'type' => 'user', |
||
| 158 | 'id' => $actor->getUID(), |
||
| 159 | 'name' => $actor->getDisplayName(), |
||
| 160 | ] |
||
| 161 | ] |
||
| 162 | ); |
||
| 163 | $notification->setLink($this->url->linkToRouteAbsolute( |
||
| 164 | 'polls.page.vote', |
||
| 165 | ['id' => $poll->getId()] |
||
| 166 | )); |
||
| 167 | break; |
||
| 168 | |||
| 169 | case 'softDeletePollByOther': |
||
| 170 | $actor = $this->userManager->get($parameters['actor']); |
||
| 171 | |||
| 172 | $notification->setParsedSubject( |
||
| 173 | $l->t('%s changed the deleted status of your poll.', [$actor->getDisplayName()]) |
||
| 174 | ); |
||
| 175 | $notification->setRichSubject( |
||
| 176 | $l->t('{user} changed the deleted status of your poll "%s".', $parameters['pollTitle']), |
||
| 177 | [ |
||
| 178 | 'user' => [ |
||
| 179 | 'type' => 'user', |
||
| 180 | 'id' => $actor->getUID(), |
||
| 181 | 'name' => $actor->getDisplayName(), |
||
| 182 | ] |
||
| 183 | ] |
||
| 184 | ); |
||
| 185 | $notification->setLink($this->url->linkToRouteAbsolute( |
||
| 186 | 'polls.page.vote', |
||
| 187 | ['id' => $poll->getId()] |
||
| 188 | )); |
||
| 189 | break; |
||
| 190 | default: |
||
| 191 | // Unknown subject => Unknown notification => throw |
||
| 192 | throw new \InvalidArgumentException(); |
||
| 193 | } |
||
| 194 | return $notification; |
||
| 195 | } |
||
| 197 |