Passed
Push — master ( 2c1297...2ffb1a )
by Julito
10:51
created

ZombieManager::listZombies()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 68
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
c 0
b 0
f 0
nc 32
nop 6
dl 0
loc 68
rs 8.5546

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