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