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