Completed
Branch newinternal (830cd7)
by Simon
03:46
created

StatsMain::generateSmallStatsTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 66
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 66
ccs 0
cts 49
cp 0
rs 9.3191
cc 2
eloc 44
nc 2
nop 0
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\Pages\Statistics;
10
11
use Waca\Tasks\InternalPageBase;
12
13
class StatsMain extends InternalPageBase
14
{
15
	public function main()
16
	{
17
		$this->setHtmlTitle('Statistics');
18
19
		$this->assign('statsPageTitle', 'Account Creation Statistics');
20
21
		$statsPages = array(
22
			'fastCloses'       => 'Requests closed less than 30 seconds after reservation in the past 3 months',
23
			'inactiveUsers'    => 'Inactive tool users',
24
			'monthlyStats'     => 'Monthly Statistics',
25
			'reservedRequests' => 'All currently reserved requests',
26
			'templateStats'    => 'Template Stats',
27
			'topCreators'      => 'Top Account Creators',
28
			'users'            => 'Account Creation Tool users',
29
		);
30
31
		$this->generateSmallStatsTable();
32
33
		$this->assign('statsPages', $statsPages);
34
35
		$graphList = array('day', '2day', '4day', 'week', '2week', 'month', '3month');
36
		$this->assign('graphList', $graphList);
37
38
		$this->setTemplate('statistics/main.tpl');
39
	}
40
41
	/**
42
	 * Gets the relevant statistics from the database for the small statistics table
43
	 */
44
	private function generateSmallStatsTable()
45
	{
46
		$database = $this->getDatabase();
47
		$requestsQuery = <<<'SQL'
48
SELECT COUNT(*) FROM request WHERE status = :status AND emailconfirm = 'Confirmed';
49
SQL;
50
		$requestsStatement = $database->prepare($requestsQuery);
51
52
		$requestStates = $this->getSiteConfiguration()->getRequestStates();
53
54
		$requestStateData = array();
55
56
		foreach ($requestStates as $statusName => $data) {
57
			$requestsStatement->execute(array(':status' => $statusName));
58
			$requestCount = $requestsStatement->fetchColumn();
59
			$requestsStatement->closeCursor();
60
			$headerText = $data['header'];
61
			$requestStateData[$headerText] = $requestCount;
62
		}
63
64
		$this->assign('requestCountData', $requestStateData);
65
66
		// Unconfirmed requests
67
		$unconfirmedStatement = $database->query(<<<SQL
68
SELECT COUNT(*) FROM request WHERE emailconfirm != 'Confirmed' AND emailconfirm != '';
69
SQL
70
		);
71
		$unconfirmed = $unconfirmedStatement->fetchColumn();
72
		$unconfirmedStatement->closeCursor();
73
		$this->assign('statsUnconfirmed', $unconfirmed);
74
75
		$userStatusStatement = $database->prepare('SELECT COUNT(*) FROM user WHERE status = :status;');
76
77
		// Admin users
78
		$userStatusStatement->execute(array(':status' => 'Admin'));
79
		$adminUsers = $userStatusStatement->fetchColumn();
80
		$userStatusStatement->closeCursor();
81
		$this->assign('statsAdminUsers', $adminUsers);
82
83
		// Users
84
		$userStatusStatement->execute(array(':status' => 'User'));
85
		$users = $userStatusStatement->fetchColumn();
86
		$userStatusStatement->closeCursor();
87
		$this->assign('statsUsers', $users);
88
89
		// Suspended users
90
		$userStatusStatement->execute(array(':status' => 'Suspended'));
91
		$suspendedUsers = $userStatusStatement->fetchColumn();
92
		$userStatusStatement->closeCursor();
93
		$this->assign('statsSuspendedUsers', $suspendedUsers);
94
95
		// New users
96
		$userStatusStatement->execute(array(':status' => 'New'));
97
		$newUsers = $userStatusStatement->fetchColumn();
98
		$userStatusStatement->closeCursor();
99
		$this->assign('statsNewUsers', $newUsers);
100
101
		// Most comments on a request
102
		$mostCommentsStatement = $database->query(<<<SQL
103
SELECT request FROM comment GROUP BY request ORDER BY COUNT(*) DESC LIMIT 1;
104
SQL
105
		);
106
		$mostComments = $mostCommentsStatement->fetchColumn();
107
		$mostCommentsStatement->closeCursor();
108
		$this->assign('mostComments', $mostComments);
109
	}
110
111
	public function getSecurityConfiguration()
112
	{
113
		return $this->getSecurityManager()->configure()->asInternalPage();
114
	}
115
}
116