Passed
Push — errorviewer ( bb5419...defbed )
by Simon
40:12 queued 30:10
created

CountAction::fetchAdminData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 60
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 60
ccs 0
cts 50
cp 0
rs 9.232
cc 2
nc 2
nop 1
crap 6

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
 * 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\API\Actions;
10
11
use DOMElement;
12
use Waca\API\ApiException as ApiException;
13
use Waca\API\IXmlApiAction;
14
use Waca\DataObjects\EmailTemplate;
15
use Waca\DataObjects\User;
16
use Waca\Tasks\XmlApiPageBase;
17
use Waca\WebRequest;
18
19
/**
20
 * API Count action
21
 */
22
class CountAction extends XmlApiPageBase implements IXmlApiAction
23
{
24
    /**
25
     * The target user
26
     * @var User $user
27
     */
28
    private $user;
29
30
    public function executeApiAction(DOMElement $apiDocument)
31
    {
32
        $username = WebRequest::getString('user');
33
        if ($username === null) {
34
            throw new ApiException("Please specify a username");
35
        }
36
37
        $userElement = $this->document->createElement("user");
38
        $userElement->setAttribute("name", $username);
39
        $apiDocument->appendChild($userElement);
40
41
        $user = User::getByUsername($username, $this->getDatabase());
42
43
        if ($user === false) {
44
            $userElement->setAttribute("missing", "true");
45
46
            return $apiDocument;
47
        }
48
49
        $this->user = $user;
50
51
        $userElement->setAttribute("level", $this->user->getStatus());
52
        $userElement->setAttribute("created", $this->getAccountsCreated());
53
54
        $userElement->setAttribute("today", $this->getToday());
55
56
        // Let the IRC bot handle the result of this.
57
        $this->fetchAdminData($userElement);
58
59
        return $apiDocument;
60
    }
61
62
    private function getAccountsCreated()
63
    {
64
        $query = <<<QUERY
65
        SELECT COUNT(*) AS count
66
        FROM log
67
            LEFT JOIN emailtemplate ON concat('Closed ', emailtemplate.id) = log.action
68
            INNER JOIN user ON log.user = user.id
69
        WHERE
70
            (defaultaction = :created OR log.action = 'Closed custom-y')
71
            AND log.objecttype = 'Request'
72
            AND user.username = :username;
73
QUERY;
74
75
        $statement = $this->getDatabase()->prepare($query);
76
        $statement->execute(array(":username" => $this->user->getUsername(), ":created" => EmailTemplate::CREATED));
77
        $result = $statement->fetchColumn();
78
        $statement->closeCursor();
79
80
        return $result;
81
    }
82
83
    private function getToday()
84
    {
85
        $query = <<<QUERY
86
        SELECT
87
            COUNT(*) AS count
88
        FROM log
89
            LEFT JOIN emailtemplate ON concat('Closed ', emailtemplate.id) = log.action
90
            INNER JOIN user ON log.user = user.id
91
        WHERE
92
            log.timestamp LIKE :date
93
            AND (defaultaction = :created OR log.action = 'Closed custom-y')
94
            AND user.username = :username;
95
QUERY;
96
97
        $statement = $this->getDatabase()->prepare($query);
98
        $statement->bindValue(":username", $this->user->getUsername());
99
        $statement->bindValue(":date", date('Y-m-d') . "%");
100
        $statement->bindValue(":created", EmailTemplate::CREATED);
101
        $statement->execute();
102
        $today = $statement->fetchColumn();
103
        $statement->closeCursor();
104
105
        return $today;
106
    }
107
108
    private function fetchAdminData(DOMElement $userElement)
109
    {
110
        $query = "SELECT COUNT(*) AS count FROM log WHERE log.user = :userid AND log.action = :action;";
111
112
        $statement = $this->getDatabase()->prepare($query);
113
        $statement->bindValue(":userid", $this->user->getId());
114
        
115
        // Each entry is in the form [ database string, attribute name ]
116
        // and it happens to be that the attribute is just the lower case form of the database value
117
        $actions = [
118
            [ 'Suspended', 'suspended' ],
119
            [ 'Promoted', 'promoted' ],
120
            [ 'Approved', 'approved' ],
121
            [ 'Demoted', 'demoted' ],
122
            [ 'Declined', 'declined' ],
123
            [ 'Renamed', 'renamed' ],
124
            [ 'Edited', 'edited' ],
125
            [ 'Prefchange', 'prefchange' ],
126
        ];
127
        foreach ($actions as $action) {
128
            $dbValue = $action[0];
129
            $attributeName = $action[1];
130
            
131
            $statement->bindValue(":action", $dbValue);
132
            $statement->execute();
133
            $attributeValue = $statement->fetchColumn();
134
            $userElement->setAttribute($attributeName, $attributeValue);
135
            $statement->closeCursor();
136
        }
137
138
        // Combine all three actions affecting Welcome templates into one count.
139
        $combinedquery = $this->getDatabase()->prepare(<<<SQL
140
            SELECT
141
                COUNT(*) AS count
142
            FROM log
143
            WHERE log.user = :userid
144
                AND log.action IN ('CreatedTemplate', 'EditedTemplate', 'DeletedTemplate');
145
SQL
146
        );
147
148
        $combinedquery->bindValue(":userid", $this->user->getId());
149
        $combinedquery->execute();
150
        $dtc = $combinedquery->fetchColumn();
151
        $userElement->setAttribute("welctempchange", $dtc);
152
        $combinedquery->closeCursor();
153
154
        // Combine both actions affecting Email templates into one count.
155
        $combinedquery = $this->getDatabase()->prepare(<<<SQL
156
            SELECT COUNT(*) AS count
157
            FROM log
158
            WHERE log.user = :userid
159
                AND log.action IN ('CreatedEmail', 'EditedEmail');
160
SQL
161
        );
162
163
        $combinedquery->bindValue(":userid", $this->user->getId());
164
        $combinedquery->execute();
165
        $cec = $combinedquery->fetchColumn();
166
        $userElement->setAttribute("emailtempchange", $cec);
167
        $combinedquery->closeCursor();
168
    }
169
}
170