| Conditions | 9 |
| Paths | 10 |
| Total Lines | 83 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 2 | 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 |
||
| 89 | public function prepare(INotification $notification, string $languageCode): INotification { |
||
| 90 | if ($notification->getApp() !== 'files_sharing' || $notification->getObjectType() !== 'remote_share') { |
||
| 91 | // Not my app => throw |
||
| 92 | throw new \InvalidArgumentException(); |
||
| 93 | } |
||
| 94 | |||
| 95 | // Read the language from the notification |
||
| 96 | $l = $this->factory->get('files_sharing', $languageCode); |
||
| 97 | |||
| 98 | switch ($notification->getSubject()) { |
||
| 99 | // Deal with known subjects |
||
| 100 | case 'remote_share': |
||
| 101 | $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
||
| 102 | |||
| 103 | $params = $notification->getSubjectParameters(); |
||
| 104 | if ($params[0] !== $params[1] && $params[1] !== null) { |
||
| 105 | $remoteInitiator = $this->createRemoteUser($params[0]); |
||
| 106 | $remoteOwner = $this->createRemoteUser($params[1]); |
||
| 107 | $params[3] = $remoteInitiator['name'] . '@' . $remoteInitiator['server']; |
||
| 108 | $params[4] = $remoteOwner['name'] . '@' . $remoteOwner['server']; |
||
| 109 | |||
| 110 | $notification->setParsedSubject( |
||
| 111 | $l->t('You received "%3$s" as a remote share from %4$s (%1$s) (on behalf of %5$s (%2$s))', $params) |
||
| 112 | ); |
||
| 113 | |||
| 114 | $notification->setRichSubject( |
||
| 115 | $l->t('You received {share} as a remote share from {user} (on behalf of {behalf})'), |
||
| 116 | [ |
||
| 117 | 'share' => [ |
||
| 118 | 'type' => 'pending-federated-share', |
||
| 119 | 'id' => $notification->getObjectId(), |
||
| 120 | 'name' => $params[2], |
||
| 121 | ], |
||
| 122 | 'user' => $remoteInitiator, |
||
| 123 | 'behalf' => $remoteOwner, |
||
| 124 | ] |
||
| 125 | ); |
||
| 126 | } else { |
||
| 127 | $remoteOwner = $this->createRemoteUser($params[0]); |
||
| 128 | $params[3] = $remoteOwner['name'] . '@' . $remoteOwner['server']; |
||
| 129 | |||
| 130 | $notification->setParsedSubject( |
||
| 131 | $l->t('You received "%3$s" as a remote share from %4$s (%1$s)', $params) |
||
| 132 | ); |
||
| 133 | |||
| 134 | |||
| 135 | $notification->setRichSubject( |
||
| 136 | $l->t('You received {share} as a remote share from {user}'), |
||
| 137 | [ |
||
| 138 | 'share' => [ |
||
| 139 | 'type' => 'pending-federated-share', |
||
| 140 | 'id' => $notification->getObjectId(), |
||
| 141 | 'name' => $params[2], |
||
| 142 | ], |
||
| 143 | 'user' => $remoteOwner, |
||
| 144 | ] |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | // Deal with the actions for a known subject |
||
| 149 | foreach ($notification->getActions() as $action) { |
||
| 150 | switch ($action->getLabel()) { |
||
| 151 | case 'accept': |
||
| 152 | $action->setParsedLabel( |
||
| 153 | (string)$l->t('Accept') |
||
| 154 | ) |
||
| 155 | ->setPrimary(true); |
||
| 156 | break; |
||
| 157 | |||
| 158 | case 'decline': |
||
| 159 | $action->setParsedLabel( |
||
| 160 | (string)$l->t('Decline') |
||
| 161 | ); |
||
| 162 | break; |
||
| 163 | } |
||
| 164 | |||
| 165 | $notification->addParsedAction($action); |
||
| 166 | } |
||
| 167 | return $notification; |
||
| 168 | |||
| 169 | default: |
||
| 170 | // Unknown subject => Unknown notification => throw |
||
| 171 | throw new \InvalidArgumentException(); |
||
| 172 | } |
||
| 273 |