Completed
Branch newinternal (157d0f)
by Simon
04:18
created

PageViewRequest::setupLogData()   C

Complexity

Conditions 11
Paths 182

Size

Total Lines 68
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 6
Bugs 1 Features 1
Metric Value
cc 11
eloc 46
c 6
b 1
f 1
nc 182
nop 2
dl 0
loc 68
ccs 0
cts 56
cp 0
crap 132
rs 5.4471

How to fix   Long Method    Complexity   

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;
10
11
use Exception;
12
use Waca\DataObjects\Comment;
13
use Waca\DataObjects\EmailTemplate;
14
use Waca\DataObjects\Log;
15
use Waca\DataObjects\Request;
16
use Waca\DataObjects\User;
17
use Waca\Exceptions\ApplicationLogicException;
18
use Waca\Fragments\RequestData;
19
use Waca\Helpers\LogHelper;
20
use Waca\PdoDatabase;
21
use Waca\Tasks\InternalPageBase;
22
use Waca\WebRequest;
23
24
class PageViewRequest extends InternalPageBase
25
{
26
	use RequestData;
27
	const PRIVATE_DATA_BARRIER = 'privateData';
28
	const SET_BAN_BARRIER = 'setBan';
29
	const STATUS_SYMBOL_OPEN = '&#x2610';
30
	const STATUS_SYMBOL_ACCEPTED = '&#x2611';
31
	const STATUS_SYMBOL_REJECTED = '&#x2612';
32
33
	/**
34
	 * Main function for this page, when no specific actions are called.
35
	 * @throws ApplicationLogicException
36
	 */
37
	protected function main()
38
	{
39
		// set up csrf protection
40
		$this->assignCSRFToken();
41
42
		// get some useful objects
43
		$database = $this->getDatabase();
44
		$request = $this->getRequest($database, WebRequest::getInt('id'));
45
		$config = $this->getSiteConfiguration();
46
		$currentUser = User::getCurrent($database);
47
48
		// Test we should be able to look at this request
49
		if ($config->getEmailConfirmationEnabled()) {
50
			if ($request->getEmailConfirm() !== 'Confirmed') {
51
				// Not allowed to look at this yet.
52
				throw new ApplicationLogicException('The email address has not yet been confirmed for this request.');
53
			}
54
		}
55
56
		$this->setupBasicData($request, $config);
57
58
		$this->setupUsernameData($request);
59
60
		$this->setupTitle($request);
61
62
		$this->setupReservationDetails($request->getReserved(), $database, $currentUser);
63
		$this->setupGeneralData($database);
64
65
		$this->assign('requestDataCleared', false);
66
		if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) {
67
			$this->assign('requestDataCleared', true);
68
		}
69
70
		$allowedPrivateData = $this->isAllowedPrivateData($request, $currentUser);
71
72
		$this->setupLogData($request, $database);
73
74
		if ($allowedPrivateData) {
75
			$this->setTemplate('view-request/main-with-data.tpl');
76
			$this->setupPrivateData($request, $currentUser, $this->getSiteConfiguration(), $database);
77
78
			if ($currentUser->isCheckuser()) {
79
				$this->setTemplate('view-request/main-with-checkuser-data.tpl');
80
				$this->setupCheckUserData($request);
81
			}
82
		}
83
		else {
84
			$this->setTemplate('view-request/main.tpl');
85
		}
86
	}
87
88
	/**
89
	 * @param Request $request
90
	 */
91
	protected function setupTitle(Request $request)
92
	{
93
		$statusSymbol = self::STATUS_SYMBOL_OPEN;
94
		if ($request->getStatus() === 'Closed') {
95
			if ($request->getWasCreated()) {
96
				$statusSymbol = self::STATUS_SYMBOL_ACCEPTED;
97
			}
98
			else {
99
				$statusSymbol = self::STATUS_SYMBOL_REJECTED;
100
			}
101
		}
102
103
		$this->setHtmlTitle($statusSymbol . ' #' . $request->getId());
104
	}
105
106
	/**
107
	 * Sets up data unrelated to the request, such as the email template information
108
	 *
109
	 * @param PdoDatabase $database
110
	 */
111
	protected function setupGeneralData(PdoDatabase $database)
112
	{
113
		$config = $this->getSiteConfiguration();
114
115
		$this->assign('createAccountReason', 'Requested account at [[WP:ACC]], request #');
116
117
		$this->assign('defaultRequestState', $config->getDefaultRequestStateKey());
118
119
		$this->assign('requestStates', $config->getRequestStates());
120
121
		/** @var EmailTemplate $createdTemplate */
122
		$createdTemplate = EmailTemplate::getById($config->getDefaultCreatedTemplateId(), $database);
123
124
		$this->assign('createdHasJsQuestion', $createdTemplate->getJsquestion() != '');
125
		$this->assign('createdJsQuestion', $createdTemplate->getJsquestion());
126
		$this->assign('createdId', $createdTemplate->getId());
127
		$this->assign('createdName', $createdTemplate->getName());
128
129
		$createReasons = EmailTemplate::getActiveTemplates(EmailTemplate::CREATED, $database);
130
		$this->assign("createReasons", $createReasons);
131
		$declineReasons = EmailTemplate::getActiveTemplates(EmailTemplate::NOT_CREATED, $database);
132
		$this->assign("declineReasons", $declineReasons);
133
134
		$allCreateReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::CREATED, $database);
135
		$this->assign("allCreateReasons", $allCreateReasons);
136
		$allDeclineReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::NOT_CREATED, $database);
137
		$this->assign("allDeclineReasons", $allDeclineReasons);
138
		$allOtherReasons = EmailTemplate::getAllActiveTemplates(false, $database);
139
		$this->assign("allOtherReasons", $allOtherReasons);
140
141
		$this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) {
142
			return User::getAllUsernames($database, true);
143
		});
144
	}
145
146
	private function setupLogData(Request $request, PdoDatabase $database)
147
	{
148
		$currentUser = User::getCurrent($database);
149
150
		$logs = LogHelper::getRequestLogsWithComments($request->getId(), $database);
151
		$requestLogs = array();
152
153
		if (trim($request->getComment()) !== "") {
154
			$requestLogs[] = array(
155
				'type'     => 'comment',
156
				'security' => 'user',
157
				'userid'   => null,
158
				'user'     => $request->getName(),
159
				'entry'    => null,
160
				'time'     => $request->getDate(),
161
				'canedit'  => false,
162
				'id'       => $request->getId(),
163
				'comment'  => $request->getComment(),
164
			);
165
		}
166
167
		/** @var User[] $nameCache */
168
		$nameCache = array();
169
170
		$editableComments = $this->allowEditingComments($currentUser);
171
172
		/** @var Log|Comment $entry */
173
		foreach ($logs as $entry) {
174
			// both log and comment have a 'user' field
175
			if (!array_key_exists($entry->getUser(), $nameCache)) {
176
				$entryUser = User::getById($entry->getUser(), $database);
177
				$nameCache[$entry->getUser()] = $entryUser;
178
			}
179
180
			if ($entry instanceof Comment) {
181
				$requestLogs[] = array(
182
					'type'     => 'comment',
183
					'security' => $entry->getVisibility(),
184
					'user'     => $nameCache[$entry->getUser()]->getUsername(),
185
					'userid'   => $entry->getUser() == -1 ? null : $entry->getUser(),
186
					'entry'    => null,
187
					'time'     => $entry->getTime(),
188
					'canedit'  => ($editableComments || $entry->getUser() == $currentUser->getId()),
189
					'id'       => $entry->getId(),
190
					'comment'  => $entry->getComment(),
191
				);
192
			}
193
194
			if ($entry instanceof Log) {
195
				$invalidUserId = $entry->getUser() === -1 || $entry->getUser() === 0;
196
				$entryUser = $invalidUserId ? User::getCommunity() : $nameCache[$entry->getUser()];
197
198
				$requestLogs[] = array(
199
					'type'     => 'log',
200
					'security' => 'user',
201
					'userid'   => $entry->getUser() == -1 ? null : $entry->getUser(),
202
					'user'     => $entryUser->getUsername(),
203
					'entry'    => LogHelper::getLogDescription($entry),
204
					'time'     => $entry->getTimestamp(),
205
					'canedit'  => false,
206
					'id'       => $entry->getId(),
207
					'comment'  => $entry->getComment(),
208
				);
209
			}
210
		}
211
212
		$this->assign("requestLogs", $requestLogs);
213
	}
214
215
	/**
216
	 * @param Request $request
217
	 */
218
	protected function setupUsernameData(Request $request)
219
	{
220
		$blacklistData = $this->getBlacklistHelper()->isBlacklisted($request->getName());
221
222
		$this->assign('requestIsBlacklisted', $blacklistData !== false);
223
		$this->assign('requestBlacklist', $blacklistData);
224
225
		try {
226
			$spoofs = $this->getAntiSpoofProvider()->getSpoofs($request->getName());
227
		}
228
		catch (Exception $ex) {
229
			$spoofs = $ex->getMessage();
230
		}
231
232
		$this->assign("spoofs", $spoofs);
233
	}
234
235
	/**
236
	 * @param User $currentUser
237
	 *
238
	 * @return bool
239
	 */
240
	private function allowEditingComments(User $currentUser)
241
	{
242
		$editableComments = false;
243
		if ($currentUser->isAdmin() || $currentUser->isCheckuser()) {
244
			$editableComments = true;
245
246
			return $editableComments;
247
		}
248
249
		return $editableComments;
250
	}
251
}