Completed
Push — master ( 572ec0...68cf53 )
by Henry
08:39
created

Comment::_validatePost()   C

Complexity

Conditions 11
Paths 144

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 21
cts 21
cp 1
rs 6.95
c 0
b 0
f 0
cc 11
nc 144
nop 1
crap 11

How to fix   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
namespace Redaxscript\Controller;
3
4
use Redaxscript\Filter;
5
use Redaxscript\Html;
6
use Redaxscript\Mailer;
7
use Redaxscript\Model;
8
use Redaxscript\Validator;
9
10
/**
11
 * children class to process the comment request
12
 *
13
 * @since 3.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Controller
17
 * @author Henry Ruhs
18
 * @author Balázs Szilágyi
19
 */
20
21
class Comment extends ControllerAbstract
22
{
23
	/**
24
	 * process the class
25
	 *
26
	 * @since 3.3.0
27
	 *
28
	 * @return string
29
	 */
30
31 7
	public function process() : string
32
	{
33 7
		$articleModel = new Model\Article();
34 7
		$settingModel = new Model\Setting();
35 7
		$postArray = $this->_normalizePost($this->_sanitizePost());
36 7
		$validateArray = $this->_validatePost($postArray);
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 35 can also be of type null; however, Redaxscript\Controller\Comment::_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...
37 7
		$route = $postArray['article'] ? $articleModel->getRouteById($postArray['article']) : null;
38
39
		/* handle validate */
40
41 7
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
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...
42
		{
43 3
			return $this->_error(
44
			[
45 3
				'route' => $route,
46 3
				'message' => $validateArray
47
			]);
48
		}
49
50
		/* handle create */
51
52
		$createArray =
53
		[
54 4
			'author' => $postArray['author'],
55 4
			'email' => $postArray['email'],
56 4
			'url' => $postArray['url'],
57 4
			'text' => $postArray['text'],
58 4
			'language' => $articleModel->getById($postArray['article'])->language,
59 4
			'article' => $postArray['article'],
60 4
			'status' => $settingModel->get('verification') ? 0 : 1
61
		];
62 4
		if (!$this->_create($createArray))
63
		{
64 1
			return $this->_error(
65
			[
66 1
				'route' => $route
67
			]);
68
		}
69
70
		/* handle mail */
71
72
		$mailArray =
73
		[
74 3
			'author' => $postArray['author'],
75 3
			'email' => $postArray['email'],
76 3
			'url' => $postArray['url'],
77 3
			'text' => $postArray['text'],
78 3
			'article' => $articleModel->getById($postArray['article'])->title,
79 3
			'route' => $route
80
		];
81 3
		if (!$this->_mail($mailArray))
82
		{
83 1
			return $this->_warning(
84
			[
85 1
				'route' => $route,
86 1
				'timeout' => $settingModel->get('notification') ? 2 : 0,
87 1
				'message' => $this->_language->get('email_failed')
88
			]);
89
		}
90
91
		/* handle success */
92
93 2
		return $this->_success(
94
		[
95 2
			'route' => $route,
96 2
			'timeout' => $settingModel->get('notification') ? 2 : 0,
97 2
			'message' => $settingModel->get('moderation') ? $this->_language->get('comment_moderation') : $this->_language->get('comment_sent')
98
		]);
99
	}
100
101
	/**
102
	 * sanitize the post
103
	 *
104
	 * @since 4.0.0
105
	 *
106
	 * @return array
107
	 */
108
109 7
	protected function _sanitizePost() : array
110
	{
111 7
		$htmlFilter = new Filter\Html();
112 7
		$emailFilter = new Filter\Email();
113 7
		$numberFilter = new Filter\Number();
114 7
		$textFilter = new Filter\Text();
115 7
		$urlFilter = new Filter\Url();
116
117
		/* sanitize post */
118
119
		return
120
		[
121 7
			'author' => $textFilter->sanitize($this->_request->getPost('author')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('author') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, 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...
122 7
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
0 ignored issues
show
Bug introduced by Henry Ruhs
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...
123 7
			'url' => $urlFilter->sanitize($this->_request->getPost('url')),
0 ignored issues
show
Bug introduced by Henry Ruhs
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...
124 7
			'text' => $htmlFilter->sanitize($this->_request->getPost('text')),
0 ignored issues
show
Bug introduced by Henry Ruhs
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...
125 7
			'article' => $numberFilter->sanitize($this->_request->getPost('article')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('article') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::sanitize() does only seem to accept integer|string|null, 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...
126 7
			'task' => $numberFilter->sanitize($this->_request->getPost('task')),
0 ignored issues
show
Bug introduced by redaxmedia
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 integer|string|null, 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...
127 7
			'solution' => $textFilter->sanitize($this->_request->getPost('solution'))
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('solution') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, 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...
128
		];
129
	}
130
131
	/**
132
	 * validate the post
133
	 *
134
	 * @since 3.3.0
135
	 *
136
	 * @param array $postArray array of the post
137
	 *
138
	 * @return array
139
	 */
140
141 7
	protected function _validatePost(array $postArray = []) : array
142
	{
143 7
		$emailValidator = new Validator\Email();
144 7
		$captchaValidator = new Validator\Captcha();
145 7
		$nameValidator = new Validator\Name();
146 7
		$urlValidator = new Validator\Url();
147 7
		$settingModel = new Model\Setting();
148
		$validateArray = [];
149
150
		/* validate post */
151 7
152
		if (!$postArray['author'])
153 2
		{
154
			$validateArray[] = $this->_language->get('author_empty');
155 7
		}
156
		else if (!$nameValidator->validate($postArray['author']))
157 1
		{
158
			$validateArray[] = $this->_language->get('author_incorrect');
159 6
		}
160
		if (!$postArray['email'])
161 1
		{
162
			$validateArray[] = $this->_language->get('email_empty');
163 7
		}
164
		else if (!$emailValidator->validate($postArray['email']))
165 1
		{
166
			$validateArray[] = $this->_language->get('email_incorrect');
167 7
		}
168
		if ($postArray['url'] && !$urlValidator->validate($postArray['url']))
169 2
		{
170
			$validateArray[] = $this->_language->get('url_incorrect');
171 7
		}
172
		if (!$postArray['text'])
173 2
		{
174
			$validateArray[] = $this->_language->get('comment_empty');
175 7
		}
176
		if (!$postArray['article'])
177 2
		{
178
			$validateArray[] = $this->_language->get('article_empty');
179 7
		}
180
		if ($settingModel->get('captcha') > 0 && !$captchaValidator->validate($postArray['task'], $postArray['solution']))
181
		{
182
			$validateArray[] = $this->_language->get('captcha_incorrect');
183
		}
184
		return $validateArray;
185
	}
186
187
	/**
188
	 * create the comment
189
	 *
190
	 * @since 3.0.0
191
	 *
192 3
	 * @param array $createArray array of the create
193
	 *
194 3
	 * @return bool
195 3
	 */
196
197
	protected function _create(array $createArray = []) : bool
198
	{
199
		$commentModel = new Model\Comment();
200
		return $commentModel->createByArray($createArray);
201
	}
202
203
	/**
204
	 * send the mail
205
	 *
206
	 * @since 3.3.0
207
	 *
208 2
	 * @param array $mailArray array of the mail
209
	 *
210 2
	 * @return bool
211 2
	 */
212
213
	protected function _mail(array $mailArray = []) : bool
214
	{
215 2
		$settingModel = new Model\Setting();
216
		$urlArticle = $this->_registry->get('root') . '/' . $this->_registry->get('parameterRoute') . $mailArray['route'];
217 2
218 2
		/* html element */
219
220 2
		$element = new Html\Element();
221
		$linkEmail = $element
222 2
			->copy()
223
			->init('a',
224 2
			[
225 2
				'href' => 'mailto:' . $mailArray['email']
226
			])
227 2
			->text($mailArray['email']);
228
		$linkUrl = $element
229 2
			->copy()
230
			->init('a',
231 2
			[
232 2
				'href' => $mailArray['url']
233
			])
234 2
			->text($mailArray['url'] ? : $this->_language->get('none'));
235
		$linkArticle = $element
236 2
			->copy()
237
			->init('a',
238
			[
239
				'href' => $urlArticle
240
			])
241
			->text($urlArticle);
242 2
243
		/* prepare mail */
244
245
		$toArray =
246 2
		[
247
			$this->_language->get('author') => $settingModel->get('email')
248 2
		];
249
		$fromArray =
250
		[
251 2
			$mailArray['author'] => $mailArray['email']
252 2
		];
253 2
		$subject = $this->_language->get('comment_new');
254 2
		$bodyArray =
255 2
		[
256 2
			$this->_language->get('author') . $this->_language->get('colon') . ' ' . $mailArray['author'],
257 2
			'<br />',
258 2
			$this->_language->get('email') . $this->_language->get('colon') . ' ' . $linkEmail,
259 2
			'<br />',
260
			$this->_language->get('url') . $this->_language->get('colon') . ' ' . $linkUrl,
261
			'<br />',
262
			$this->_language->get('article') . $this->_language->get('colon') . ' ' . $linkArticle,
263
			'<br />',
264 2
			$this->_language->get('comment') . $this->_language->get('colon') . ' ' . $mailArray['text']
265 2
		];
266 2
267
		/* send mail */
268
269
		$mailer = new Mailer();
270
		$mailer->init($toArray, $fromArray, $subject, $bodyArray);
0 ignored issues
show
Bug introduced by JimMorrison723
It seems like $subject defined by $this->_language->get('comment_new') on line 253 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...
271
		return $mailer->send();
272
	}
273
}
274