Completed
Pull Request — master (#526)
by Michael
11:09 queued 01:05
created

StatusAction::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 59
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

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
namespace Waca\API\Actions;
4
5
use Waca\API\ApiActionBase as ApiActionBase;
6
use Waca\API\IApiAction as IApiAction;
7
8
use \PdoDatabase as PdoDatabase;
9
10
/**
11
 * API Count action
12
 */
13
class StatusAction extends ApiActionBase implements IApiAction
14
{
15
	/**
16
	 * The database
17
	 * @var PdoDatabase $database
18
	 */
19
	private $database;
20
21
	public function execute(\DOMElement $apiDocument)
22
	{
23
		$this->database = gGetDb();
24
25
		$statusElement = $this->document->createElement("status");
26
		$apiDocument->appendChild($statusElement);
27
28
		$query = $this->database->prepare(<<<SQL
29
            SELECT /* Api/StatusAction */ COUNT(*) AS count
30
            FROM request
31
            WHERE
32
                status = :pstatus
33
                AND emailconfirm = 'Confirmed';
34
SQL
35
		);
36
37
		global $availableRequestStates;
38
		foreach ($availableRequestStates as $key => $value) {
39
			$query->bindValue(":pstatus", $key);
40
			$query->execute();
41
			$sus = $query->fetchColumn();
42
			$statusElement->setAttribute($value['api'], $sus);
43
			$query->closeCursor();
44
		}
45
46
		$query = $this->database->prepare(<<<SQL
47
            SELECT /* Api/StatusAction */ COUNT(*) AS count
48
            FROM ban
49
            WHERE
50
                (duration > UNIX_TIMESTAMP() OR duration = -1)
51
                AND active = 1;
52
SQL
53
		);
54
55
		$query->execute();
56
		$sus = $query->fetchColumn();
57
		$statusElement->setAttribute("bans", $sus);
58
		$query->closeCursor();
59
60
		$query = $this->database->prepare("SELECT /* Api/StatusAction */ COUNT(*) AS count FROM user WHERE status = :ulevel;");
61
		$query->bindValue(":ulevel", "Admin");
62
		$query->execute();
63
		$sus = $query->fetchColumn();
64
		$statusElement->setAttribute("useradmin", $sus);
65
		$query->closeCursor();
66
67
		$query->bindValue(":ulevel", "User");
68
		$query->execute();
69
		$sus = $query->fetchColumn();
70
		$statusElement->setAttribute("user", $sus);
71
		$query->closeCursor();
72
73
		$query->bindValue(":ulevel", "New");
74
		$query->execute();
75
		$sus = $query->fetchColumn();
76
		$statusElement->setAttribute("usernew", $sus);
77
		$query->closeCursor();
78
79
		return $apiDocument;
80
	}
81
}
82