Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

Controller::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
namespace Redaxscript\Modules\Contact;
3
4
use Redaxscript\Controller\ControllerAbstract;
5
use Redaxscript\Filter;
6
use Redaxscript\Html;
7
use Redaxscript\Mailer;
8
use Redaxscript\Model;
9
use Redaxscript\Validator;
10
use function nl2br;
11
12
/**
13
 * children class to process the contact request
14
 *
15
 * @since 4.0.0
16
 *
17
 * @package Redaxscript
18
 * @category Modules
19
 * @author Henry Ruhs
20
 */
21
22
class Controller extends ControllerAbstract
23
{
24
	/**
25
	 * process
26
	 *
27
	 * @since 4.0.0
28
	 *
29
	 * @return string
30
	 */
31
32
	public function process() : string
33
	{
34
		$postArray = $this->_normalizePost($this->_sanitizePost());
35
		$validateArray = $this->_validatePost($postArray);
0 ignored issues
show
Bug introduced by
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 34 can also be of type null; however, Redaxscript\Modules\Cont...roller::_validatePost() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
36
37
		/* handle validate */
38
39
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by
The expression $validateArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
		{
41
			return $this->_error(
42
			[
43
				'message' => $validateArray
44
			]);
45
		}
46
47
		/* handle mail */
48
49
		$mailArray =
50
		[
51
			'author' => $postArray['author'],
52
			'email' => $postArray['email'],
53
			'url' => $postArray['url'],
54
			'text' => $postArray['text']
55
		];
56
		if ($this->_mail($mailArray))
57
		{
58
			return $this->_success(
59
			[
60
				'route' => $this->_registry->get('liteRoute'),
61
				'timeout' => 2,
62
				'message' => $this->_language->get('message_sent', '_contact')
63
			]);
64
		}
65
66
		/* handle error */
67
68
		return $this->_error(
69
		[
70
			'message' => $this->_language->get('email_failed')
71
		]);
72
	}
73
74
	/**
75
	 * sanitize the post
76
	 *
77
	 * @since 4.0.0
78
	 *
79
	 * @return array
80
	 */
81
82
	protected function _sanitizePost() : array
83
	{
84
		$numberFilter = new Filter\Number();
85
		$specialFilter = new Filter\Special();
86
		$emailFilter = new Filter\Email();
87
		$urlFilter = new Filter\Url();
88
		$htmlFilter = new Filter\Html();
89
90
		/* sanitize post */
91
92
		return
93
		[
94
			'author' => $specialFilter->sanitize($this->_request->getPost('author')),
0 ignored issues
show
Bug introduced by
It seems like $this->_request->getPost('author') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
95
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
0 ignored issues
show
Bug introduced by
It seems like $this->_request->getPost('email') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Email::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
96
			'url' => $urlFilter->sanitize($this->_request->getPost('url')),
0 ignored issues
show
Bug introduced by
It seems like $this->_request->getPost('url') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Url::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
97
			'text' => nl2br($htmlFilter->sanitize($this->_request->getPost('text'))),
0 ignored issues
show
Bug introduced by
It seems like $this->_request->getPost('text') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Html::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
98
			'task' => $numberFilter->sanitize($this->_request->getPost('task')),
0 ignored issues
show
Bug introduced by
It seems like $this->_request->getPost('task') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
99
			'solution' => $this->_request->getPost('solution')
100
		];
101
	}
102
103
	/**
104
	 * validate
105
	 *
106
	 * @since 4.0.0
107
	 *
108
	 * @param array $postArray array of the post
109
	 *
110
	 * @return array
111
	 */
112
113
	protected function _validatePost(array $postArray = []) : array
114
	{
115
		$emailValidator = new Validator\Email();
116
		$urlValidator = new Validator\Url();
117
		$captchaValidator = new Validator\Captcha();
118
		$settingModel = new Model\Setting();
119
		$validateArray = [];
120
121
		/* validate post */
122
123
		if (!$postArray['author'])
124
		{
125
			$validateArray[] = $this->_language->get('author_empty');
126
		}
127
		if (!$postArray['email'])
128
		{
129
			$validateArray[] = $this->_language->get('email_empty');
130
		}
131
		else if (!$emailValidator->validate($postArray['email']))
132
		{
133
			$validateArray['email'] = $this->_language->get('email_incorrect');
134
		}
135
		if ($postArray['url'] && !$urlValidator->validate($postArray['url']))
136
		{
137
			$validateArray[] = $this->_language->get('url_incorrect');
138
		}
139
		if (!$postArray['text'])
140
		{
141
			$validateArray[] = $this->_language->get('message_empty');
142
		}
143
		if ($settingModel->get('captcha') > 0 && !$captchaValidator->validate($postArray['task'], $postArray['solution']))
144
		{
145
			$validateArray[] = $this->_language->get('captcha_incorrect');
146
		}
147
		return $validateArray;
148
	}
149
150
	/**
151
	 * mail
152
	 *
153
	 * @since 4.0.0
154
	 *
155
	 * @param array $mailArray
156
	 *
157
	 * @return bool
158
	 */
159
160
	protected function _mail(array $mailArray = []) : bool
161
	{
162
		$settingModel = new Model\Setting();
163
164
		/* html element */
165
166
		$element = new Html\Element();
167
		$linkEmail = $element
168
			->copy()
169
			->init('a',
170
			[
171
				'href' => 'mailto:' . $mailArray['email']
172
			])
173
			->text($mailArray['email']);
174
		$linkUrl = $element
175
			->copy()
176
			->init('a',
177
			[
178
				'href' => $mailArray['url']
179
			])
180
			->text($mailArray['url'] ? : $this->_language->get('none'));
181
182
		/* prepare mail */
183
184
		$toArray =
185
		[
186
			$settingModel->get('author') => $settingModel->get('email')
187
		];
188
		$fromArray =
189
		[
190
			$mailArray['author'] => $mailArray['email']
191
		];
192
		$subject = $this->_language->get('contact');
193
		$bodyArray =
194
		[
195
			$this->_language->get('author') . $this->_language->get('colon') . ' ' . $mailArray['author'],
196
			'<br />',
197
			$this->_language->get('email') . $this->_language->get('colon') . ' ' . $linkEmail,
198
			'<br />',
199
			$this->_language->get('url') . $this->_language->get('colon') . ' ' . $linkUrl,
200
			'<br />',
201
			$this->_language->get('message') . $this->_language->get('colon') . ' ' . $mailArray['text']
202
		];
203
204
		/* send mail */
205
206
		$mailer = new Mailer();
207
		$mailer->init($toArray, $fromArray, $subject, $bodyArray);
0 ignored issues
show
Bug introduced by
It seems like $subject defined by $this->_language->get('contact') on line 192 can also be of type array; however, Redaxscript\Mailer::init() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
208
		return $mailer->send();
209
	}
210
}
211