| Conditions | 18 |
| Paths | 34 |
| Total Lines | 91 |
| Code Lines | 59 |
| 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 |
||
| 119 | public function getNotificationsByUser($userId) |
||
| 120 | { |
||
| 121 | $userInfo = api_get_user_info($userId); |
||
| 122 | $events = $this->get_all(); |
||
| 123 | $extraFieldData = $this->getUserExtraData(api_get_user_id()); |
||
| 124 | $allowJustification = api_get_plugin_setting('justification', 'tool_enable') === 'true'; |
||
| 125 | |||
| 126 | $userJustificationList = []; |
||
| 127 | if ($allowJustification) { |
||
| 128 | $plugin = Justification::create(); |
||
| 129 | $userJustificationList = $plugin->getUserJustificationList($userId); |
||
| 130 | } |
||
| 131 | |||
| 132 | $notifications = []; |
||
| 133 | foreach ($events as $event) { |
||
| 134 | $days = (int) $event['day_diff']; |
||
| 135 | $checkIsRead = $event['persistent'] == 0 ? true : false; |
||
| 136 | $eventItemId = $event['event_id']; |
||
| 137 | |||
| 138 | switch ($event['event_type']) { |
||
| 139 | case self::ACCOUNT_EXPIRATION: |
||
| 140 | if (empty($userInfo['expiration_date'])) { |
||
| 141 | break; |
||
| 142 | } |
||
| 143 | |||
| 144 | $id = 'id_'.self::ACCOUNT_EXPIRATION.'_event_'.$event['id'].'_'.$userInfo['id']; |
||
| 145 | |||
| 146 | $read = false; |
||
| 147 | if ($checkIsRead) { |
||
| 148 | $read = $this->isRead($id, $extraFieldData); |
||
| 149 | } |
||
| 150 | |||
| 151 | $showNotification = $this->showNotification($userInfo['expiration_date'], $days); |
||
| 152 | if ($showNotification && $read === false) { |
||
| 153 | $notifications[] = [ |
||
| 154 | 'id' => $id, |
||
| 155 | 'title' => $event['title'], |
||
| 156 | 'content' => $event['content'], |
||
| 157 | 'event_text' => get_lang('ExpirationDate').': '.api_get_local_time($userInfo['expiration_date']), |
||
| 158 | 'link' => $event['link'], |
||
| 159 | 'persistent' => $event['persistent'] |
||
| 160 | ]; |
||
| 161 | } |
||
| 162 | break; |
||
| 163 | case self::JUSTIFICATION_EXPIRATION: |
||
| 164 | if (!empty($userJustificationList)) { |
||
| 165 | foreach ($userJustificationList as $userJustification) { |
||
| 166 | if (empty($userJustification['date_validity'])) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | |||
| 170 | if ($eventItemId != $userJustification['justification_document_id']) { |
||
| 171 | continue; |
||
| 172 | } |
||
| 173 | |||
| 174 | $showNotification = $this->showNotification($userJustification['date_validity'], $days); |
||
| 175 | |||
| 176 | $id = 'id_'.self::JUSTIFICATION_EXPIRATION.'_event_'.$event['id'].'_'.$userJustification['id']; |
||
| 177 | |||
| 178 | $fieldData = $plugin->getJustification($userJustification['justification_document_id']); |
||
|
|
|||
| 179 | |||
| 180 | $read = false; |
||
| 181 | if ($checkIsRead) { |
||
| 182 | $read = $this->isRead($id, $extraFieldData); |
||
| 183 | } |
||
| 184 | |||
| 185 | $eventText = $plugin->get_lang('Justification').': '.$fieldData['name'].' <br />'; |
||
| 186 | $eventText .= $plugin->get_lang('JustificationDate').': '.$userJustification['date_validity']; |
||
| 187 | |||
| 188 | $url = $event['link']; |
||
| 189 | if (empty($url)) { |
||
| 190 | $url = api_get_path(WEB_CODE_PATH).'auth/justification.php#'.$fieldData['code']; |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($showNotification && $read === false) { |
||
| 194 | $notifications[] = [ |
||
| 195 | 'id' => $id, |
||
| 196 | 'title' => $event['title'], |
||
| 197 | 'content' => $event['content'], |
||
| 198 | 'event_text' => $eventText, |
||
| 199 | 'link' => $url, |
||
| 200 | 'persistent' => $event['persistent'] |
||
| 201 | ]; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | break; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | return $notifications; |
||
| 210 | } |
||
| 282 |