Completed
Push — master ( ba8c52...47b68a )
by Henry
07:01
created

Comment::_getErrorRoute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 20
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
		$specialFilter = new Filter\Special();
119
		$urlFilter = new Filter\Url();
120
		$htmlFilter = new Filter\Html();
121
122
		/* sanitize post */
123
124
		return
125
		[
126
			'id' => $specialFilter->sanitize($this->_request->getPost('id')),
127
			'url' => $urlFilter->sanitize($this->_request->getPost('url')),
128
			'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')),
129
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
130
			'article' => $specialFilter->sanitize($this->_request->getPost('article')),
131
			'status' => $specialFilter->sanitize($this->_request->getPost('status')),
132
			'rank' => $specialFilter->sanitize($this->_request->getPost('rank')),
133
			'access' => json_encode($this->_request->getPost('access')),
134
			'date' => strtotime($this->_request->getPost('date'))
135
		];
136
	}
137
138
	/**
139
	 * validate the post
140
	 *
141
	 * @since 4.0.0
142
	 *
143
	 * @param array $postArray array of the post
144
	 *
145
	 * @return array
146
	 */
147
148
	protected function _validatePost(array $postArray = []) : array
149
	{
150
		$validateArray = [];
151
152
		/* validate post */
153
154
		if (!$postArray['text'])
155
		{
156
			$validateArray[] = $this->_language->get('text_empty');
157
		}
158
		return $validateArray;
159
	}
160
161
	/**
162
	 * create the comment
163
	 *
164
	 * @since 4.0.0
165
	 *
166
	 * @param array $createArray array of the create
167
	 *
168
	 * @return bool
169
	 */
170
171
	protected function _create(array $createArray = []) : bool
172
	{
173
		$commentModel = new Admin\Model\Comment();
174
		return $commentModel->createByArray($createArray);
175
	}
176
177
	/**
178
	 * update the comment
179
	 *
180
	 * @since 4.0.0
181
	 *
182
	 * @param int $commentId identifier of the comment
183
	 * @param array $updateArray array of the update
184
	 *
185
	 * @return bool
186
	 */
187
188
	public function _update(int $commentId = null, array $updateArray = []) : bool
189
	{
190
		$commentModel = new Admin\Model\Comment();
191
		return $commentModel->updateByIdAndArray($commentId, $updateArray);
192
	}
193
194
	/**
195
	 * get success route
196
	 *
197
	 * @since 4.0.0
198
	 *
199
	 * @param array $postArray array of the post
200
	 *
201
	 * @return string
202
	 */
203
204
	protected function _getSuccessRoute(array $postArray = []) : string
205
	{
206
		if ($this->_registry->get('commentsEdit'))
207
		{
208
			if ($postArray['id'])
209
			{
210
				return 'admin/view/comments#row-' . $postArray['id'];
211
			}
212
			return 'admin/view/comments';
213
		}
214
		return 'admin';
215
	}
216
217
	/**
218
	 * get error route
219
	 *
220
	 * @since 4.0.0
221
	 *
222
	 * @param array $postArray array of the post
223
	 *
224
	 * @return string
225
	 */
226
227
	protected function _getErrorRoute(array $postArray = []) : string
228
	{
229
		if ($this->_registry->get('commentsEdit') && $postArray['id'])
230
		{
231
			return 'admin/edit/comments/' . $postArray['id'];
232
		}
233
		if ($this->_registry->get('commentsNew'))
234
		{
235
			return 'admin/new/comments';
236
		}
237
		return 'admin';
238
	}
239
}
240