Completed
Push — master ( 03fc7e...44f12a )
by Henry
15:26
created

includes/Admin/Controller/Comment.php (1 issue)

Check for implicit conversion of array to boolean.

Best Practice Bug Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Admin\Controller;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Filter;
6
7
/**
8
 * children class to process the admin comment request
9
 *
10
 * @since 4.0.0
11
 *
12
 * @package Redaxscript
13
 * @category Controller
14
 * @author Henry Ruhs
15
 */
16
17
class Comment extends ControllerAbstract
18
{
19
	/**
20
	 * process the class
21
	 *
22
	 * @since 4.0.0
23
	 *
24
	 * @param string $action action to process
25
	 *
26
	 * @return string
27
	 */
28
29
	public function process(string $action = null) : string
30
	{
31
		$postArray = $this->_normalizePost($this->_sanitizePost());
32
		$validateArray = $this->_validatePost($postArray);
33
		$myUser = $this->_registry->get('myUser');
34
		$myEmail = $this->_registry->get('myEmail');
35
		$now = $this->_registry->get('now');
36
37
		/* validate post */
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
				'route' => $this->_getErrorRoute($postArray),
44
				'message' => $validateArray
45
			]);
46
		}
47
48
		/* handle create */
49
50
		if ($action === 'create')
51
		{
52
			$createArray =
53
			[
54
				'author' => $myUser,
55
				'email' => $myEmail,
56
				'url' => $postArray['url'],
57
				'text' => $postArray['text'],
58
				'language' => $postArray['language'],
59
				'article' => $postArray['article'],
60
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
61
				'rank' => $postArray['rank'],
62
				'access' => $postArray['access'],
63
				'date' => $postArray['date'] ? $postArray['date'] : $now
64
			];
65
			if ($this->_create($createArray))
66
			{
67
				return $this->_success(
68
				[
69
					'route' => $this->_getSuccessRoute($postArray),
70
					'timeout' => 2
71
				]);
72
			}
73
		}
74
75
		/* handle update */
76
77
		if ($action === 'update')
78
		{
79
			$updateArray =
80
			[
81
				'url' => $postArray['url'],
82
				'text' => $postArray['text'],
83
				'language' => $postArray['language'],
84
				'article' => $postArray['article'],
85
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
86
				'rank' => $postArray['rank'],
87
				'access' => $postArray['access'],
88
				'date' => $postArray['date'] ? $postArray['date'] : $now
89
			];
90
			if ($this->_update($postArray['id'], $updateArray))
91
			{
92
				return $this->_success(
93
				[
94
					'route' => $this->_getSuccessRoute($postArray),
95
					'timeout' => 2
96
				]);
97
			}
98
		}
99
100
		/* handle error */
101
102
		return $this->_error(
103
		[
104
			'route' => $this->_getErrorRoute($postArray)
105
		]);
106
	}
107
108
	/**
109
	 * sanitize the post
110
	 *
111
	 * @since 4.0.0
112
	 *
113
	 * @return array
114
	 */
115
116
	protected function _sanitizePost() : array
117
	{
118
		$numberFilter = new Filter\Number();
119
		$specialFilter = new Filter\Special();
120
		$urlFilter = new Filter\Url();
121
		$htmlFilter = new Filter\Html();
122
123
		/* sanitize post */
124
125
		return
126
		[
127
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
128
			'url' => $urlFilter->sanitize($this->_request->getPost('url')),
129
			'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')),
130
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
131
			'article' => $this->_request->getPost('article'),
132
			'status' => $numberFilter->sanitize($this->_request->getPost('status')),
133
			'rank' => $numberFilter->sanitize($this->_request->getPost('rank')),
134
			'access' => json_encode($this->_request->getPost('access')),
135
			'date' => strtotime($this->_request->getPost('date'))
136
		];
137
	}
138
139
	/**
140
	 * validate the post
141
	 *
142
	 * @since 4.0.0
143
	 *
144
	 * @param array $postArray array of the post
145
	 *
146
	 * @return array
147
	 */
148
149
	protected function _validatePost(array $postArray = []) : array
150
	{
151
		$validateArray = [];
152
153
		/* validate post */
154
155
		if (!$postArray['text'])
156
		{
157
			$validateArray[] = $this->_language->get('comment_empty');
158
		}
159
		if (!$postArray['article'])
160
		{
161
			$validateArray[] = $this->_language->get('article_empty');
162
		}
163
		return $validateArray;
164
	}
165
166
	/**
167
	 * create the comment
168
	 *
169
	 * @since 4.0.0
170
	 *
171
	 * @param array $createArray array of the create
172
	 *
173
	 * @return bool
174
	 */
175
176
	protected function _create(array $createArray = []) : bool
177
	{
178
		$commentModel = new Admin\Model\Comment();
179
		return $commentModel->createByArray($createArray);
180
	}
181
182
	/**
183
	 * update the comment
184
	 *
185
	 * @since 4.0.0
186
	 *
187
	 * @param int $commentId identifier of the comment
188
	 * @param array $updateArray array of the update
189
	 *
190
	 * @return bool
191
	 */
192
193
	public function _update(int $commentId = null, array $updateArray = []) : bool
194
	{
195
		$commentModel = new Admin\Model\Comment();
196
		return $commentModel->updateByIdAndArray($commentId, $updateArray);
197
	}
198
199
	/**
200
	 * get success route
201
	 *
202
	 * @since 4.0.0
203
	 *
204
	 * @param array $postArray array of the post
205
	 *
206
	 * @return string
207
	 */
208
209
	protected function _getSuccessRoute(array $postArray = []) : string
210
	{
211
		if ($this->_registry->get('commentsEdit'))
212
		{
213
			if ($postArray['id'])
214
			{
215
				return 'admin/view/comments#row-' . $postArray['id'];
216
			}
217
			return 'admin/view/comments';
218
		}
219
		return 'admin';
220
	}
221
222
	/**
223
	 * get error route
224
	 *
225
	 * @since 4.0.0
226
	 *
227
	 * @param array $postArray array of the post
228
	 *
229
	 * @return string
230
	 */
231
232
	protected function _getErrorRoute(array $postArray = []) : string
233
	{
234
		if ($this->_registry->get('commentsEdit') && $postArray['id'])
235
		{
236
			return 'admin/edit/comments/' . $postArray['id'];
237
		}
238
		if ($this->_registry->get('commentsNew'))
239
		{
240
			return 'admin/new/comments';
241
		}
242
		return 'admin';
243
	}
244
}
245