Completed
Branch newinternal (526082)
by Simon
04:16
created

ValidationError::getErrorMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
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\Validation;
10
11
use Exception;
12
13
class ValidationError
14
{
15
	const NAME_EMPTY = "name_empty";
16
	const NAME_EXISTS = "name_exists";
17
	const NAME_EXISTS_SUL = "name_exists";
18
	const NAME_NUMONLY = "name_numonly";
19
	const NAME_INVALIDCHAR = "name_invalidchar";
20
	const NAME_SANITISED = "name_sanitised";
21
	const EMAIL_EMPTY = "email_empty";
22
	const EMAIL_WIKIMEDIA = "email_wikimedia";
23
	const EMAIL_INVALID = "email_invalid";
24
	const EMAIL_MISMATCH = "email_mismatch";
25
	const OPEN_REQUEST_NAME = "open_request_name";
26
	const BANNED = "banned";
27
	const BANNED_TOR = "banned_tor";
28
	/**
29
	 * @var array Error text for the above
30
	 */
31
	private static $errorText = array(
32
		self::NAME_EMPTY        => 'You\'ve not chosen a username!',
33
		self::NAME_EXISTS       => 'I\'m sorry, but the username you selected is already taken. Please try another. '
34
			. 'Please note that Wikipedia automatically capitalizes the first letter of any user name, therefore '
35
			. '[[User:example]] would become [[User:Example]].',
36
		self::NAME_EXISTS_SUL   => 'I\'m sorry, but the username you selected is already taken. Please try another. '
37
			. 'Please note that Wikipedia automatically capitalizes the first letter of any user name, therefore '
38
			. '[[User:example]] would become [[User:Example]].',
39
		self::NAME_NUMONLY      => 'The username you chose is invalid: it consists entirely of numbers. Please retry '
40
			. 'with a valid username.',
41
		self::NAME_INVALIDCHAR  => 'There appears to be an invalid character in your username. Please note that the '
42
			. 'following characters are not allowed: <code># @ / &lt; &gt; [ ] | { }</code>',
43
		self::NAME_SANITISED    => 'Your requested username has been automatically adjusted due to technical '
44
			. 'restrictions. Underscores have been replaced with spaces, and the first character has been capitalised.',
45
		self::EMAIL_EMPTY       => 'You need to supply an email address.',
46
		self::EMAIL_WIKIMEDIA   => 'Please provide your email address here.',
47
		self::EMAIL_INVALID     => 'Invalid E-mail address supplied. Please check you entered it correctly.',
48
		self::EMAIL_MISMATCH    => 'The email addresses you entered do not match. Please try again.',
49
		self::OPEN_REQUEST_NAME => 'There is already an open request with this name in this system.',
50
		self::BANNED            => 'I\'m sorry, but you are currently banned from requesting accounts using this tool. '
51
			. 'However, you can still send an email to [email protected] to request an account.',
52
		self::BANNED_TOR        => 'Tor exit nodes are currently banned from using this tool due to excessive abuse. '
53
			. 'Please note that Tor is also currently banned from editing Wikipedia.',
54
	);
55
	/**
56
	 * Summary of $errorCode
57
	 * @var string
58
	 */
59
	private $errorCode;
60
	/**
61
	 * Summary of $isError
62
	 * @var bool
63
	 */
64
	private $isError;
65
66
	/**
67
	 * Summary of __construct
68
	 *
69
	 * @param string $errorCode
70
	 * @param bool   $isError
71
	 */
72
	public function __construct($errorCode, $isError = true)
73
	{
74
		$this->errorCode = $errorCode;
75
		$this->isError = $isError;
76
	}
77
78
	/**
79
	 * Summary of getErrorCode
80
	 * @return string
81
	 */
82
	public function getErrorCode()
83
	{
84
		return $this->errorCode;
85
	}
86
87
	/**
88
	 * @return string
89
	 * @throws Exception
90
	 */
91
	public function getErrorMessage()
92
	{
93
		$text = self::$errorText[$this->errorCode];
94
95
		if ($text == null) {
96
			throw new Exception('Unknown validation error');
97
		}
98
99
		return $text;
100
	}
101
102
	/**
103
	 * Summary of isError
104
	 * @return bool
105
	 */
106
	public function isError()
107
	{
108
		return $this->isError;
109
	}
110
}
111