| Conditions | 8 |
| Paths | 25 |
| Total Lines | 79 |
| Code Lines | 44 |
| 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 |
||
| 28 | public function onResubscribe(SessionResubscriptionEvent $event): void |
||
| 29 | { |
||
| 30 | if (AbstractEvent::TYPE_PRE === $event->getType()) { |
||
| 31 | $resubscriptionLimit = Resubscription::create()->get('resubscription_limit'); |
||
| 32 | |||
| 33 | // Initialize variables as a calendar year by default |
||
| 34 | $limitDateFormat = 'Y-01-01'; |
||
| 35 | $limitDate = gmdate($limitDateFormat); |
||
| 36 | $resubscriptionOffset = "1 year"; |
||
| 37 | |||
| 38 | // No need to use a 'switch' with only two options so an 'if' is enough. |
||
| 39 | // However, this could change if the number of options increases |
||
| 40 | if ($resubscriptionLimit === 'natural_year') { |
||
| 41 | $limitDateFormat = 'Y-m-d'; |
||
| 42 | $limitDate = gmdate($limitDateFormat); |
||
| 43 | $limitDate = gmdate($limitDateFormat, strtotime("$limitDate -$resubscriptionOffset")); |
||
| 44 | } |
||
| 45 | |||
| 46 | $join = " INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION)." s ON s.id = su.session_id"; |
||
| 47 | |||
| 48 | // User sessions and courses |
||
| 49 | $userSessions = Database::select( |
||
| 50 | 'su.session_id, s.access_end_date', |
||
| 51 | Database::get_main_table(TABLE_MAIN_SESSION_USER).' su '.$join, |
||
| 52 | [ |
||
| 53 | 'where' => [ |
||
| 54 | 'su.user_id = ? AND s.access_end_date >= ?' => [ |
||
| 55 | api_get_user_id(), |
||
| 56 | $limitDate, |
||
| 57 | ], |
||
| 58 | ], |
||
| 59 | 'order' => 'access_end_date DESC', |
||
| 60 | ] |
||
| 61 | ); |
||
| 62 | $userSessionCourses = []; |
||
| 63 | foreach ($userSessions as $userSession) { |
||
| 64 | $userSessionCourseResult = Database::select( |
||
| 65 | 'c_id', |
||
| 66 | Database::get_main_table(TABLE_MAIN_SESSION_COURSE), |
||
| 67 | [ |
||
| 68 | 'where' => [ |
||
| 69 | 'session_id = ?' => [ |
||
| 70 | $userSession['session_id'], |
||
| 71 | ], |
||
| 72 | ], |
||
| 73 | ] |
||
| 74 | ); |
||
| 75 | foreach ($userSessionCourseResult as $userSessionCourse) { |
||
| 76 | if (!isset($userSessionCourses[$userSessionCourse['c_id']])) { |
||
| 77 | $userSessionCourses[$userSessionCourse['c_id']] = $userSession['access_end_date']; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | // Current session and courses |
||
| 83 | $currentSessionCourseResult = Database::select( |
||
| 84 | 'c_id', |
||
| 85 | Database::get_main_table(TABLE_MAIN_SESSION_COURSE), |
||
| 86 | [ |
||
| 87 | 'where' => [ |
||
| 88 | 'session_id = ?' => [ |
||
| 89 | $event->getSessionId(), |
||
| 90 | ], |
||
| 91 | ], |
||
| 92 | ] |
||
| 93 | ); |
||
| 94 | |||
| 95 | // Check if current course code matches with one of the users |
||
| 96 | foreach ($currentSessionCourseResult as $currentSessionCourse) { |
||
| 97 | if (isset($userSessionCourses[$currentSessionCourse['c_id']])) { |
||
| 98 | $endDate = $userSessionCourses[$currentSessionCourse['c_id']]; |
||
| 99 | $resubscriptionDate = gmdate($limitDateFormat, strtotime($endDate." +$resubscriptionOffset")); |
||
| 100 | $icon = Display::getMdiIcon(ObjectIcon::USER, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Learner')); |
||
| 101 | $canResubscribeFrom = sprintf( |
||
| 102 | get_plugin_lang('CanResubscribeFromX', 'resubscription'), |
||
| 103 | $resubscriptionDate |
||
| 104 | ); |
||
| 105 | |||
| 106 | throw new Exception(Display::label($icon.' '.$canResubscribeFrom, "info")); |
||
| 107 | } |
||
| 111 | } |