| Conditions | 14 |
| Paths | 35 |
| Total Lines | 106 |
| Code Lines | 71 |
| 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 |
||
| 150 | protected function parseShareInvitation(IShare $share, INotification $notification, IL10N $l): INotification { |
||
| 151 | |||
| 152 | if ($share->getShareType() === IShare::TYPE_USER) { |
||
| 153 | if ($share->getStatus() !== IShare::STATUS_PENDING) { |
||
| 154 | throw new AlreadyProcessedException(); |
||
| 155 | } |
||
| 156 | } else if ($share->getShareType() === IShare::TYPE_GROUP) { |
||
| 157 | if ($share->getStatus() !== IShare::STATUS_PENDING) { |
||
| 158 | throw new AlreadyProcessedException(); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | switch ($notification->getSubject()) { |
||
| 163 | case self::INCOMING_USER_SHARE: |
||
| 164 | if ($share->getSharedWith() !== $notification->getUser()) { |
||
| 165 | throw new AlreadyProcessedException(); |
||
| 166 | } |
||
| 167 | |||
| 168 | $sharer = $this->userManager->get($share->getSharedBy()); |
||
| 169 | if (!$sharer instanceof IUser) { |
||
| 170 | throw new \InvalidArgumentException('Temporary failure'); |
||
| 171 | } |
||
| 172 | |||
| 173 | $subject = $l->t('You received {share} as a share by {user}'); |
||
| 174 | $subjectParameters = [ |
||
| 175 | 'share' => [ |
||
| 176 | 'type' => 'highlight', |
||
| 177 | 'id' => $notification->getObjectId(), |
||
| 178 | 'name' => $share->getNode()->getName(), |
||
| 179 | ], |
||
| 180 | 'user' => [ |
||
| 181 | 'type' => 'user', |
||
| 182 | 'id' => $sharer->getUID(), |
||
| 183 | 'name' => $sharer->getDisplayName(), |
||
| 184 | ], |
||
| 185 | ]; |
||
| 186 | break; |
||
| 187 | |||
| 188 | case self::INCOMING_GROUP_SHARE: |
||
| 189 | $user = $this->userManager->get($notification->getUser()); |
||
| 190 | if (!$user instanceof IUser) { |
||
| 191 | throw new AlreadyProcessedException(); |
||
| 192 | } |
||
| 193 | |||
| 194 | $group = $this->groupManager->get($share->getSharedWith()); |
||
| 195 | if (!$group->inGroup($user)) { |
||
| 196 | throw new AlreadyProcessedException(); |
||
| 197 | } |
||
| 198 | |||
| 199 | if ($share->getPermissions() === 0) { |
||
| 200 | // Already rejected |
||
| 201 | throw new AlreadyProcessedException(); |
||
| 202 | } |
||
| 203 | |||
| 204 | $sharer = $this->userManager->get($share->getSharedBy()); |
||
| 205 | if (!$sharer instanceof IUser) { |
||
| 206 | throw new \InvalidArgumentException('Temporary failure'); |
||
| 207 | } |
||
| 208 | |||
| 209 | $subject = $l->t('You received {share} to group {group} as a share by {user}'); |
||
| 210 | $subjectParameters = [ |
||
| 211 | 'share' => [ |
||
| 212 | 'type' => 'highlight', |
||
| 213 | 'id' => $notification->getObjectId(), |
||
| 214 | 'name' => $share->getNode()->getName(), |
||
| 215 | ], |
||
| 216 | 'group' => [ |
||
| 217 | 'type' => 'user-group', |
||
| 218 | 'id' => $group->getGID(), |
||
| 219 | 'name' => $group->getDisplayName(), |
||
| 220 | ], |
||
| 221 | 'user' => [ |
||
| 222 | 'type' => 'user', |
||
| 223 | 'id' => $sharer->getUID(), |
||
| 224 | 'name' => $sharer->getDisplayName(), |
||
| 225 | ], |
||
| 226 | ]; |
||
| 227 | break; |
||
| 228 | |||
| 229 | default: |
||
| 230 | throw new \InvalidArgumentException('Invalid subject'); |
||
| 231 | } |
||
| 232 | |||
| 233 | $placeholders = $replacements = []; |
||
| 234 | foreach ($subjectParameters as $placeholder => $parameter) { |
||
| 235 | $placeholders[] = '{' . $placeholder . '}'; |
||
| 236 | $replacements[] = $parameter['name']; |
||
| 237 | } |
||
| 238 | |||
| 239 | $notification->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
||
| 240 | ->setRichSubject($subject, $subjectParameters) |
||
| 241 | ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
||
| 242 | |||
| 243 | $acceptAction = $notification->createAction(); |
||
| 244 | $acceptAction->setParsedLabel($l->t('Accept')) |
||
| 245 | ->setLink($this->url->linkToOCSRouteAbsolute('files_sharing.ShareAPI.acceptShare', ['id' => $share->getId()]), 'POST') |
||
| 246 | ->setPrimary(true); |
||
| 247 | $notification->addParsedAction($acceptAction); |
||
| 248 | |||
| 249 | $rejectAction = $notification->createAction(); |
||
| 250 | $rejectAction->setParsedLabel($l->t('Reject')) |
||
| 251 | ->setLink($this->url->linkToOCSRouteAbsolute('files_sharing.ShareAPI.deleteShare', ['id' => $share->getId()]), 'DELETE') |
||
| 252 | ->setPrimary(false); |
||
| 253 | $notification->addParsedAction($rejectAction); |
||
| 254 | |||
| 255 | return $notification; |
||
| 256 | } |
||
| 258 |