Failed Conditions
Branch newinternal (3df7fe)
by Simon
04:13
created

PageRequestAccount::main()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 51
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 51
ccs 0
cts 37
cp 0
rs 6.9743
cc 7
eloc 26
nc 6
nop 0
crap 56

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\Request;
10
11
use Exception;
12
use Waca\DataObjects\Request;
13
use Waca\Helpers\BanHelper;
14
use Waca\SessionAlert;
15
use Waca\Tasks\PublicInterfacePageBase;
16
use Waca\Validation\RequestValidationHelper;
17
use Waca\Validation\ValidationError;
18
use Waca\WebRequest;
19
20
class PageRequestAccount extends PublicInterfacePageBase
21
{
22
	/**
23
	 * Main function for this page, when no specific actions are called.
24
	 * @return void
25
	 */
26
	protected function main()
27
	{
28
		// dual mode page
29
		if (WebRequest::wasPosted()) {
30
			$request = $this->createNewRequest();
31
32
			$validationErrors = $this->validateRequest($request);
33
34
			if (count($validationErrors) > 0) {
35
				foreach ($validationErrors as $validationError) {
36
					SessionAlert::error($validationError->getErrorMessage());
37
				}
38
39
				// Preserve the data after an error
40
				WebRequest::setSessionContext('accountReq',
41
					array(
42
						'username' => WebRequest::postString('name'),
43
						'email'    => WebRequest::postEmail('email'),
44
						'comments' => WebRequest::postString('comments'),
45
					)
46
				);
47
48
				// Validation error, bomb out early.
49
				$this->redirect();
50
51
				return;
52
			}
53
54
			// actually save the request to the database
55
			if ($this->getSiteConfiguration()->getEmailConfirmationEnabled()) {
56
				$this->saveAsEmailConfirmation($request);
57
			}
58
			else {
59
				$this->saveWithoutEmailConfirmation($request);
60
			}
61
		}
62
		else {
63
			// set the form values from the session context
64
			$context = WebRequest::getSessionContext('accountReq');
65
			if ($context !== null && is_array($context)) {
66
				$this->assign('username', $context['username']);
67
				$this->assign('email', $context['email']);
68
				$this->assign('comments', $context['comments']);
69
			}
70
71
			// Clear it for a refresh
72
			WebRequest::setSessionContext('accountReq', null);
73
74
			$this->setTemplate('request/request-form.tpl');
75
		}
76
	}
77
78
	/**
79
	 * @return Request
80
	 */
81
	protected function createNewRequest()
82
	{
83
		$request = new Request();
84
		$request->setDatabase($this->getDatabase());
85
86
		$request->setName(WebRequest::postString('name'));
87
		$request->setEmail(WebRequest::postEmail('email'));
88
		$request->setComment(WebRequest::postString('comments'));
89
90
		$request->setIp(WebRequest::remoteAddress());
91
		$request->setForwardedIp(WebRequest::forwardedAddress());
92
93
		$request->setUserAgent(WebRequest::userAgent());
94
95
		return $request;
96
	}
97
98
	/**
99
	 * @param Request $request
100
	 *
101
	 * @return ValidationError[]
102
	 */
103
	protected function validateRequest($request)
104
	{
105
		$validationHelper = new RequestValidationHelper(
106
			new BanHelper($this->getDatabase()),
107
			$request,
108
			WebRequest::postEmail('emailconfirm'),
109
			$this->getDatabase(),
110
			$this->getAntiSpoofProvider(),
111
			$this->getXffTrustProvider(),
112
			$this->getHttpHelper(),
113
			$this->getSiteConfiguration()->getMediawikiWebServiceEndpoint(),
114
			$this->getSiteConfiguration()->getTitleBlacklistEnabled(),
115
			$this->getTorExitProvider());
116
117
		// These are arrays of ValidationError.
118
		$nameValidation = $validationHelper->validateName();
119
		$emailValidation = $validationHelper->validateEmail();
120
		$otherValidation = $validationHelper->validateOther();
121
122
		$validationErrors = array_merge($nameValidation, $emailValidation, $otherValidation);
123
124
		return $validationErrors;
125
	}
126
127
	/**
128
	 * @param Request $request
129
	 *
130
	 * @throws Exception
131
	 */
132
	protected function saveAsEmailConfirmation(Request $request)
133
	{
134
		$request->generateEmailConfirmationHash();
135
		$request->save();
136
137
		$trustedIp = $this->getXffTrustProvider()->getTrustedClientIp(
138
			$request->getIp(),
139
			$request->getForwardedIp());
140
141
		$this->assign("ip", $trustedIp);
142
		$this->assign("id", $request->getId());
143
		$this->assign("hash", $request->getEmailConfirm());
144
145
		// Sends the confirmation email to the user.
146
		$this->getEmailHelper()->sendMail(
147
			$request->getEmail(),
148
			"[ACC #{$request->getId()}] English Wikipedia Account Request",
149
			$this->fetchTemplate('request/confirmation-mail.tpl'));
150
151
		$this->redirect('emailConfirmationRequired');
152
	}
153
154
	/**
155
	 * @param Request $request
156
	 *
157
	 * @throws Exception
158
	 */
159
	protected function saveWithoutEmailConfirmation(Request $request)
160
	{
161
		$request->setEmailConfirm(0); // Since it can't be null @todo fixme
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
Coding Style introduced by
Comment refers to a FIXME task
Loading history...
162
		$request->save();
163
164
		$this->getNotificationHelper()->requestReceived($request);
165
166
		$this->redirect('requestSubmitted');
167
	}
168
}