Passed
Pull Request — master (#6127)
by Angel Fernando Quiroz
07:52
created

ResubscriptionEventSubscriber::onResubscribe()   B

Complexity

Conditions 9
Paths 26

Size

Total Lines 83
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 46
nc 26
nop 1
dl 0
loc 83
rs 7.6226
c 1
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
use Chamilo\CoreBundle\Event\AbstractEvent;
8
use Chamilo\CoreBundle\Event\Events;
9
use Chamilo\CoreBundle\Event\SessionResubscriptionEvent;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
class ResubscriptionEventSubscriber implements EventSubscriberInterface
13
{
14
    private Resubscription $plugin;
15
16
    public function __construct()
17
    {
18
        $this->plugin = Resubscription::create();
19
    }
20
21
    public static function getSubscribedEvents(): array
22
    {
23
        return [
24
            Events::SESSION_RESUBSCRIPTION => 'onResubscribe',
25
        ];
26
    }
27
28
    /**
29
     * @throws \Exception
30
     */
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
                }
115
            }
116
        }
117
    }
118
}