Passed
Push — dependabot/npm_and_yarn/vue-i1... ( 836b78...06776d )
by
unknown
11:15
created

ZombieManager::listZombies()   C

Complexity

Conditions 10
Paths 256

Size

Total Lines 84
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 39
c 0
b 0
f 0
nc 256
nop 6
dl 0
loc 84
rs 6.1333

How to fix   Long Method    Complexity   

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
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Framework\Container;
5
6
/**
7
 * ZombieQuery.
8
 *
9
 * @copyright (c) 2012 University of Geneva
10
 * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
11
 * @author Laurent Opprecht <[email protected]>
12
 */
13
class ZombieManager
14
{
15
    public static function last_year()
16
    {
17
        $today = time();
18
        $day = date('j', $today);
19
        $month = date('n', $today);
20
        $year = date('Y', $today) - 1;
21
22
        return mktime(0, 0, 0, $month, $day, $year);
23
    }
24
25
    /**
26
     * Returns users whose last login is prior from $ceiling.
27
     *
28
     * @param int|string $ceiling     last login date
29
     * @param bool       $active_only if true returns only active users. Otherwise returns all users.
30
     *
31
     * @return array
32
     */
33
    public static function listZombies(
34
        $ceiling,
35
        $active_only = true,
36
        $from = 0,
37
        $count = 10,
38
        $column = 'user.firstname',
39
        $direction = 'desc'
40
    ) {
41
        $column = str_replace('user.', '', $column);
42
        if (empty($column)) {
43
            $column = 'firstname';
44
        }
45
46
        $validColumns = ['id', 'official_code', 'firstname', 'lastname', 'username', 'email', 'status', 'created_at', 'active', 'login_date'];
47
        if (!in_array($column, $validColumns)) {
48
            $column = 'firstname';
49
        }
50
        $ceiling = is_numeric($ceiling) ? (int) $ceiling : strtotime($ceiling);
51
        $ceiling = date('Y-m-d H:i:s', $ceiling);
52
53
        $user_table = Database::get_main_table(TABLE_MAIN_USER);
54
        $login_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
55
56
        $accessUrlHelper = Container::getAccessUrlHelper();
57
        $accessUrl = $accessUrlHelper->getCurrent();
58
59
        $sql = 'SELECT
60
                    user.id,
61
                    user.official_code,
62
                    user.firstname,
63
                    user.lastname,
64
                    user.username,
65
                    user.email,
66
                    user.status,
67
                    user.created_at,
68
                    user.active,
69
                    access.login_date';
70
71
        if ($accessUrlHelper->isMultiple()) {
72
            $access_url_rel_user_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
73
            $current_url_id = $accessUrl->getId();
74
75
            $sql .= " FROM $user_table as user, $login_table as access, $access_url_rel_user_table as url
76
                      WHERE
77
                        access.login_date = (SELECT MAX(a.login_date)
78
                                             FROM $login_table as a
79
                                             WHERE a.login_user_id = user.id
80
                                             ) AND
81
                        access.login_date <= '$ceiling' AND
82
                        user.id = access.login_user_id AND
83
                        url.user_id = user.id AND url.access_url_id=$current_url_id";
84
        } else {
85
            $sql .= " FROM $user_table as user, $login_table as access
86
                      WHERE
87
                        access.login_date = (SELECT MAX(a.login_date)
88
                                             FROM $login_table as a
89
                                             WHERE a.login_user_id = user.id
90
                                             ) AND
91
                        access.login_date <= '$ceiling' AND
92
                        user.id = access.login_user_id";
93
        }
94
95
        if ($active_only) {
96
            $sql .= ' AND user.active = 1';
97
        }
98
99
        $sql .= !str_contains($sql, 'WHERE') ? ' WHERE user.active <> '.USER_SOFT_DELETED : ' AND user.active <> '.USER_SOFT_DELETED;
100
        $column = str_replace('user.', '', $column);
101
        $sql .= " ORDER BY `$column` $direction";
102
        if (!is_null($from) && !is_null($count)) {
103
            $count = (int) $count;
104
            $from = (int) $from;
105
            $sql .= " LIMIT $from, $count ";
106
        }
107
108
        $result = Database::query($sql);
109
110
        if (Database::num_rows($result) === 0) {
111
            return [];
112
        }
113
        $userInfo = Database::store_result($result, 'ASSOC');
114
        $userInfo['auth_sources'] = api_get_user_entity($userInfo['id'])->getAuthSourcesAuthentications($accessUrl);
115
116
        return $userInfo;
117
    }
118
119
    /**
120
     * @param $ceiling
121
     */
122
    public static function deactivate_zombies($ceiling)
123
    {
124
        $zombies = self::listZombies($ceiling);
125
        $ids = [];
126
        foreach ($zombies as $zombie) {
127
            $ids[] = $zombie['id'];
128
        }
129
        UserManager::deactivate_users($ids);
130
    }
131
}
132