| 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 |
||
| 136 | public function getNotificationsByUser($userId) |
||
| 137 | { |
||
| 138 | $userInfo = api_get_user_info($userId); |
||
| 139 | $events = $this->get_all(); |
||
| 140 | $extraFieldData = $this->getUserExtraData(api_get_user_id()); |
||
| 141 | $allowJustification = 'true' === api_get_plugin_setting('justification', 'tool_enable'); |
||
| 142 | |||
| 143 | $userJustificationList = []; |
||
| 144 | if ($allowJustification) { |
||
| 145 | $plugin = Justification::create(); |
||
| 146 | $userJustificationList = $plugin->getUserJustificationList($userId); |
||
| 147 | } |
||
| 148 | |||
| 149 | $notifications = []; |
||
| 150 | foreach ($events as $event) { |
||
| 151 | $days = (int) $event['day_diff']; |
||
| 152 | $checkIsRead = 0 == $event['persistent'] ? true : false; |
||
| 153 | $eventItemId = $event['event_id']; |
||
| 154 | |||
| 155 | switch ($event['event_type']) { |
||
| 156 | case self::ACCOUNT_EXPIRATION: |
||
| 157 | if (empty($userInfo['expiration_date'])) { |
||
| 158 | break; |
||
| 159 | } |
||
| 160 | |||
| 161 | $id = 'id_'.self::ACCOUNT_EXPIRATION.'_event_'.$event['id'].'_'.$userInfo['id']; |
||
| 162 | |||
| 163 | $read = false; |
||
| 164 | if ($checkIsRead) { |
||
| 165 | $read = $this->isRead($id, $extraFieldData); |
||
| 166 | } |
||
| 167 | |||
| 168 | $showNotification = $this->showNotification($userInfo['expiration_date'], $days); |
||
| 169 | if ($showNotification && false === $read) { |
||
| 170 | $notifications[] = [ |
||
| 171 | 'id' => $id, |
||
| 172 | 'title' => $event['title'], |
||
| 173 | 'content' => $event['content'], |
||
| 174 | 'event_text' => get_lang('ExpirationDate').': '.api_get_local_time($userInfo['expiration_date']), |
||
| 175 | 'link' => $event['link'], |
||
| 176 | 'persistent' => $event['persistent'], |
||
| 177 | ]; |
||
| 178 | } |
||
| 179 | break; |
||
| 180 | case self::JUSTIFICATION_EXPIRATION: |
||
| 181 | if (!empty($userJustificationList)) { |
||
| 182 | foreach ($userJustificationList as $userJustification) { |
||
| 183 | if (empty($userJustification['date_validity'])) { |
||
| 184 | continue; |
||
| 185 | } |
||
| 186 | |||
| 187 | if ($eventItemId != $userJustification['justification_document_id']) { |
||
| 188 | continue; |
||
| 189 | } |
||
| 190 | |||
| 191 | $showNotification = $this->showNotification($userJustification['date_validity'], $days); |
||
| 192 | |||
| 193 | $id = 'id_'.self::JUSTIFICATION_EXPIRATION.'_event_'.$event['id'].'_'.$userJustification['id']; |
||
| 194 | |||
| 195 | $fieldData = $plugin->getJustification($userJustification['justification_document_id']); |
||
|
|
|||
| 196 | |||
| 197 | $read = false; |
||
| 198 | if ($checkIsRead) { |
||
| 199 | $read = $this->isRead($id, $extraFieldData); |
||
| 200 | } |
||
| 201 | |||
| 202 | $eventText = $plugin->get_lang('Justification').': '.$fieldData['name'].' <br />'; |
||
| 203 | $eventText .= $plugin->get_lang('Justification expiration').': '.$userJustification['date_validity']; |
||
| 204 | |||
| 205 | $url = $event['link']; |
||
| 206 | if (empty($url)) { |
||
| 207 | $url = api_get_path(WEB_CODE_PATH).'auth/justification.php#'.$fieldData['code']; |
||
| 208 | } |
||
| 209 | |||
| 210 | if ($showNotification && false === $read) { |
||
| 211 | $notifications[] = [ |
||
| 212 | 'id' => $id, |
||
| 213 | 'title' => $event['title'], |
||
| 214 | 'content' => $event['content'], |
||
| 215 | 'event_text' => $eventText, |
||
| 216 | 'link' => $url, |
||
| 217 | 'persistent' => $event['persistent'], |
||
| 218 | ]; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | break; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | return $notifications; |
||
| 227 | } |
||
| 304 |