UserSearchHelper::withReservedRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Helpers\SearchHelpers;
11
12
use DateTime;
13
use PDO;
14
use Waca\DataObjects\User;
15
use Waca\PdoDatabase;
16
17
class UserSearchHelper extends SearchHelperBase
18
{
19
    /**
20
     * UserSearchHelper constructor.
21
     *
22
     * @param PdoDatabase $database
23
     */
24
    public function __construct(PdoDatabase $database)
25
    {
26
        parent::__construct($database, 'user', User::class);
27
    }
28
29
    /**
30
     * Initiates a search for requests
31
     *
32
     * @param PdoDatabase $database
33
     *
34
     * @return UserSearchHelper
35
     */
36
    public static function get(PdoDatabase $database)
37
    {
38
        $helper = new UserSearchHelper($database);
39
40
        return $helper;
41
    }
42
43
    /**
44
     * @param string $status
45
     *
46
     * @return $this
47
     */
48
    public function byStatus($status)
49
    {
50
        $this->whereClause .= ' AND status = ?';
51
        $this->parameterList[] = $status;
52
53
        return $this;
54
    }
55
56
    public function statusIn($statuses)
57
    {
58
        $this->inClause('status', $statuses);
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param string $role
65
     *
66
     * @return $this
67
     */
68
    public function byRole($role)
69
    {
70
        $this->joinClause .= ' INNER JOIN userrole r on origin.id = r.user';
71
        $this->whereClause .= ' AND r.role = ?';
72
        $this->parameterList[] = $role;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param DateTime $instant
79
     *
80
     * @return $this
81
     */
82
    public function lastActiveBefore(DateTime $instant)
83
    {
84
        $this->whereClause .= ' AND origin.lastactive < ? AND approvaldate.timestamp < ?';
85
        $this->joinClause .= <<<'SQLFRAG'
86
 LEFT JOIN (
87
    SELECT objectid, MAX(timestamp) timestamp 
88
    FROM log
89
    WHERE objecttype = 'User' AND action = 'Approved' 
90
    GROUP BY objectid
91
    ) approvaldate ON approvaldate.objectid = origin.id
92
SQLFRAG;
93
        $formattedDate = $instant->format("Y-m-d H:i:s");
94
        $this->parameterList[] = $formattedDate;
95
        $this->parameterList[] = $formattedDate;
96
97
        return $this;
98
    }
99
100
    public function getRoleMap(&$roleMap)
101
    {
102
        $query = <<<SQL
103
            SELECT /* UserSearchHelper/roleMap */ 
104
                  r.user user
105
                , group_concat(r.role SEPARATOR ', ') roles 
106
            FROM userrole r 
107
            WHERE user IN ({$this->buildQuery(array('id'))})
108
            GROUP BY r.user
109
SQL;
110
111
        $statement = $this->database->prepare($query);
112
        $statement->execute($this->parameterList);
113
114
        $roleMap = array();
115
        foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) {
116
            $roleMap[$row['user']] = $row['roles'];
117
        }
118
119
        return $this;
120
    }
121
122
    public function withReservedRequest()
123
    {
124
        $this->joinClause = ' INNER JOIN request req ON req.reserved = origin.id';
125
        $this->groupByClause = ' GROUP BY origin.id, origin.username';
126
127
        return $this->fetchMap('username');
128
    }
129
}
130