Passed
Push — comment-flagging ( 6669bb...9d978e )
by Simon
06:56 queued 02:53
created

StatusAction::executeApiAction()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 93
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 65
c 2
b 0
f 0
dl 0
loc 93
ccs 0
cts 74
cp 0
rs 8.7636
cc 4
nc 8
nop 1
crap 20

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\IXmlApiAction;
13
use Waca\DataObjects\RequestQueue;
14
use Waca\Helpers\SearchHelpers\RequestSearchHelper;
15
use Waca\RequestStatus;
16
use Waca\Tasks\XmlApiPageBase;
17
18
/**
19
 * API Count action
20
 */
21
class StatusAction extends XmlApiPageBase implements IXmlApiAction
22
{
23
    public function executeApiAction(DOMElement $apiDocument)
24
    {
25
        $statusElement = $this->document->createElement("status");
26
        $apiDocument->appendChild($statusElement);
27
28
        $query = $this->getDatabase()->prepare(<<<SQL
29
            SELECT /* Api/StatusAction */ COUNT(*) AS count
30
            FROM request
31
            WHERE
32
                status = :pstatus
33
                AND queue = :queue
34
                AND emailconfirm = 'Confirmed';
35
SQL
36
        );
37
38
        $allQueues = RequestQueue::getAllQueues($this->getDatabase());
39
40
        foreach ($allQueues as $value) {
41
            $query->bindValue(":pstatus", RequestStatus::OPEN);
42
            $query->bindValue(":queue", $value->getId());
43
            $query->execute();
44
            $sus = $query->fetchColumn();
45
            $statusElement->setAttribute($value->getApiName(), $sus);
46
            $query->closeCursor();
47
        }
48
49
        // hospital queue
50
        $search = RequestSearchHelper::get($this->getDatabase())->isHospitalised();
51
52
        if ($this->getSiteConfiguration()->getEmailConfirmationEnabled()) {
53
            $search->withConfirmedEmail();
54
        }
55
        $search->getRecordCount($hospitalCount);
56
        $statusElement->setAttribute('x-hospital', $hospitalCount);
57
58
        // job queue
59
        $search = RequestSearchHelper::get($this->getDatabase())
60
            ->byStatus(RequestStatus::JOBQUEUE);
61
62
        if ($this->getSiteConfiguration()->getEmailConfirmationEnabled()) {
63
            $search->withConfirmedEmail();
64
        }
65
66
        $search->getRecordCount($jobQueueRequestCount);
67
        $statusElement->setAttribute('x-jobqueue', $jobQueueRequestCount);
68
69
        // bans
70
        $query = $this->getDatabase()->prepare(<<<SQL
71
            SELECT /* Api/StatusAction */ COUNT(*) AS count
72
            FROM ban
73
            WHERE
74
                (duration > UNIX_TIMESTAMP() OR duration is null)
75
                AND active = 1;
76
SQL
77
        );
78
79
        $query->execute();
80
        $sus = $query->fetchColumn();
81
        $statusElement->setAttribute("bans", $sus);
82
        $query->closeCursor();
83
84
        $query = $this->getDatabase()->prepare(<<<SQL
85
SELECT /* Api/StatusAction */ COUNT(*) AS count
86
FROM user WHERE status = :ulevel;
87
SQL
88
        );
89
90
        $query->bindValue(":ulevel", "New");
91
        $query->execute();
92
        $sus = $query->fetchColumn();
93
        $statusElement->setAttribute("usernew", $sus);
94
        $query->closeCursor();
95
96
        $query = $this->getDatabase()->prepare(<<<SQL
97
select /* Api/StatusAction */ COUNT(*) from user u
98
inner join userrole ur on u.id = ur.user
99
where u.status = 'Active' and ur.role = :ulevel
100
SQL
101
        );
102
103
        $query->bindValue(":ulevel", "admin");
104
        $query->execute();
105
        $sus = $query->fetchColumn();
106
        $statusElement->setAttribute("useradmin", $sus);
107
        $query->closeCursor();
108
109
        $query->bindValue(":ulevel", "user");
110
        $query->execute();
111
        $sus = $query->fetchColumn();
112
        $statusElement->setAttribute("user", $sus);
113
        $query->closeCursor();
114
115
        return $apiDocument;
116
    }
117
}
118