| Conditions | 21 |
| Paths | 29 |
| Total Lines | 113 |
| Code Lines | 74 |
| 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 |
||
| 174 | public function getNotificationsByUser(int $userId): array |
||
| 175 | { |
||
| 176 | $userInfo = api_get_user_info($userId); |
||
| 177 | $events = $this->get_all(); |
||
| 178 | $extraFieldData = $this->getUserExtraData($userId); |
||
| 179 | |||
| 180 | $notifications = []; |
||
| 181 | foreach ($events as $event) { |
||
| 182 | $days = (int) $event['day_diff']; |
||
| 183 | $checkIsRead = $event['persistent'] == 0; |
||
| 184 | $eventItemId = $event['event_id']; |
||
| 185 | |||
| 186 | switch ($event['event_type']) { |
||
| 187 | case self::ACCOUNT_EXPIRATION: |
||
| 188 | if (empty($userInfo['expiration_date'])) { |
||
| 189 | break; |
||
| 190 | } |
||
| 191 | |||
| 192 | $id = 'id_'.self::ACCOUNT_EXPIRATION.'_event_'.$event['id'].'_'.$userInfo['id']; |
||
| 193 | |||
| 194 | $read = false; |
||
| 195 | if ($checkIsRead) { |
||
| 196 | $read = $this->isRead($id, $extraFieldData); |
||
| 197 | } |
||
| 198 | |||
| 199 | $showNotification = $this->showNotification($userInfo['expiration_date'], $days); |
||
| 200 | if ($showNotification && $read === false) { |
||
| 201 | $notifications[] = [ |
||
| 202 | 'id' => $id, |
||
| 203 | 'title' => $event['title'], |
||
| 204 | 'content' => $event['content'], |
||
| 205 | 'event_text' => get_lang('ExpirationDate').': '.api_get_local_time($userInfo['expiration_date']), |
||
| 206 | 'link' => $event['link'], |
||
| 207 | 'persistent' => $event['persistent'], |
||
| 208 | ]; |
||
| 209 | } |
||
| 210 | break; |
||
| 211 | case self::JUSTIFICATION_EXPIRATION: |
||
| 212 | if (api_get_plugin_setting('justification', 'tool_enable') !== 'true') { |
||
| 213 | break; |
||
| 214 | } |
||
| 215 | |||
| 216 | $plugin = Justification::create(); |
||
| 217 | $userJustificationList = $plugin->getUserJustificationList($userId); |
||
| 218 | |||
| 219 | foreach ($userJustificationList as $userJustification) { |
||
| 220 | if (empty($userJustification['date_validity'])) { |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | |||
| 224 | if ($eventItemId != $userJustification['justification_document_id']) { |
||
| 225 | continue; |
||
| 226 | } |
||
| 227 | |||
| 228 | $showNotification = $this->showNotification($userJustification['date_validity'], $days); |
||
| 229 | |||
| 230 | $id = 'id_'.self::JUSTIFICATION_EXPIRATION.'_event_'.$event['id'].'_'.$userJustification['id']; |
||
| 231 | |||
| 232 | $fieldData = $plugin->getJustification($userJustification['justification_document_id']); |
||
| 233 | |||
| 234 | $read = false; |
||
| 235 | if ($checkIsRead) { |
||
| 236 | $read = $this->isRead($id, $extraFieldData); |
||
| 237 | } |
||
| 238 | |||
| 239 | $eventText = $plugin->get_lang('Justification').': '.$fieldData['name'].' <br />'; |
||
| 240 | $eventText .= $plugin->get_lang('JustificationDate').': '.$userJustification['date_validity']; |
||
| 241 | |||
| 242 | $url = $event['link']; |
||
| 243 | if (empty($url)) { |
||
| 244 | $url = api_get_path(WEB_CODE_PATH).'auth/justification.php#'.$fieldData['code']; |
||
| 245 | } |
||
| 246 | |||
| 247 | if ($showNotification && $read === false) { |
||
| 248 | $notifications[] = [ |
||
| 249 | 'id' => $id, |
||
| 250 | 'title' => $event['title'], |
||
| 251 | 'content' => $event['content'], |
||
| 252 | 'event_text' => $eventText, |
||
| 253 | 'link' => $url, |
||
| 254 | 'persistent' => $event['persistent'], |
||
| 255 | ]; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | break; |
||
| 259 | case self::SPECIFIC_USER: |
||
| 260 | $assignedUsers = self::getAssignedUsers($event['id']); |
||
| 261 | $assignedUserIdList = array_keys($assignedUsers); |
||
| 262 | |||
| 263 | if (!in_array($userId, $assignedUserIdList)) { |
||
| 264 | break; |
||
| 265 | } |
||
| 266 | //no break |
||
| 267 | case self::GLOBAL_NOTIFICATION: |
||
| 268 | $id = "id_{$event['event_type']}_event_{$event['id']}_$userId"; |
||
| 269 | |||
| 270 | $wasRead = $checkIsRead && $this->isRead($id, $extraFieldData); |
||
| 271 | |||
| 272 | if (!$wasRead) { |
||
| 273 | $notifications[] = [ |
||
| 274 | 'id' => $id, |
||
| 275 | 'title' => $event['title'], |
||
| 276 | 'content' => $event['content'], |
||
| 277 | 'event_text' => null, |
||
| 278 | 'link' => $event['link'], |
||
| 279 | 'persistent' => $event['persistent'], |
||
| 280 | ]; |
||
| 281 | } |
||
| 282 | break; |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | return $notifications; |
||
| 287 | } |
||
| 453 |