Completed
Push — newinternal ( 65a0f5...5b021c )
by Simon
08:29
created

UserSearchHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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