Completed
Push — master ( dc8f37...8770f3 )
by Henry
15:26 queued 05:23
created

includes/Admin/Controller/Module.php (2 issues)

Labels
Severity

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
use Redaxscript\Validator;
7
use function json_encode;
8
9
/**
10
 * children class to process the admin module request
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Controller
16
 * @author Henry Ruhs
17
 */
18
19
class Module extends ControllerAbstract
20
{
21
	/**
22
	 * process the class
23
	 *
24
	 * @since 4.0.0
25
	 *
26
	 * @param string $action action to process
27
	 *
28
	 * @return string
29
	 */
30 6
31
	public function process(string $action = null) : string
32 6
	{
33 6
		$postArray = $this->_normalizePost($this->_sanitizePost());
34
		$validateArray = $this->_validatePost($postArray);
35
36
		/* validate post */
37 6
38
		if ($validateArray)
39 3
		{
40
			return $this->_error(
41 3
			[
42 3
				'route' => $this->_getErrorRoute($postArray),
43
				'message' => $validateArray
44
			]);
45
		}
46
47
		/* handle update */
48 3
49
		if ($action === 'update')
50
		{
51
			$updateArray =
52 2
			[
53 2
				'name' => $postArray['name'],
54 2
				'description' => $postArray['description'],
55 2
				'status' => $postArray['status'],
56
				'access' => $postArray['access']
57 2
			];
58
			if ($this->_update($postArray['id'], $updateArray))
59 2
			{
60
				return $this->_success(
61 2
				[
62 2
					'route' => $this->_getSuccessRoute($postArray),
63
					'timeout' => 2
64
				]);
65
			}
66
		}
67
68
		/* handle error */
69 1
70
		return $this->_error(
71 1
		[
72
			'route' => $this->_getErrorRoute($postArray)
73
		]);
74
	}
75
76
	/**
77
	 * sanitize the post
78
	 *
79
	 * @since 4.0.0
80
	 *
81
	 * @return array
82
	 */
83 6
84
	protected function _sanitizePost() : array
85 6
	{
86 6
		$nameFilter = new Filter\Name();
87
		$numberFilter = new Filter\Number();
88
		$toggleFilter = new Filter\Toggle();
89
90
		/* sanitize post */
91
92 6
		return
93 6
		[
94 6
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
0 ignored issues
show
It seems like $this->_request->getPost('id') 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...
95 6
			'name' => $nameFilter->sanitize($this->_request->getPost('name')),
0 ignored issues
show
It seems like $this->_request->getPost('name') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Name::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 6
			'description' => $this->_request->getPost('description'),
97
			'status' => $toggleFilter->sanitize($this->_request->getPost('status')),
98
			'access' => json_encode($this->_request->getPost('access'))
99
		];
100
	}
101
102
	/**
103
	 * validate the post
104
	 *
105
	 * @since 4.0.0
106
	 *
107
	 * @param array $postArray array of the post
108
	 *
109
	 * @return array
110 6
	 */
111
112 6
	protected function _validatePost(array $postArray = []) : array
113
	{
114
		$nameValidator = new Validator\Name();
115
		$validateArray = [];
116 6
117
		/* validate post */
118 3
119
		if (!$postArray['name'])
120 6
		{
121
			$validateArray[] = $this->_language->get('name_empty');
122
		}
123
		else if (!$nameValidator->validate($postArray['name']))
124
		{
125
			$validateArray[] = $this->_language->get('name_incorrect');
126
		}
127
		return $validateArray;
128
	}
129
130
	/**
131
	 * update the module
132
	 *
133
	 * @since 4.0.0
134 2
	 *
135
	 * @param int $moduleId identifier of the module
136 2
	 * @param array $updateArray array of the update
137 2
	 *
138
	 * @return bool
139
	 */
140
141
	protected function _update(int $moduleId = null, array $updateArray = []) : bool
142
	{
143
		$moduleModel = new Admin\Model\Module();
144
		return $moduleModel->updateByIdAndArray($moduleId, $updateArray);
145
	}
146
147
	/**
148
	 * get success route
149
	 *
150 2
	 * @since 4.0.0
151
	 *
152 2
	 * @param array $postArray array of the post
153
	 *
154 1
	 * @return string
155
	 */
156 1
157
	protected function _getSuccessRoute(array $postArray = []) : string
158
	{
159
		if ($this->_registry->get('modulesEdit') && $postArray['id'])
160
		{
161
			return 'admin/view/modules#row-' . $postArray['id'];
162
		}
163
		return 'admin';
164
	}
165
166
	/**
167
	 * get error route
168
	 *
169 4
	 * @since 4.0.0
170
	 *
171 4
	 * @param array $postArray array of the post
172
	 *
173 3
	 * @return string
174
	 */
175 2
176
	protected function _getErrorRoute(array $postArray = []) : string
177 1
	{
178
		if ($this->_registry->get('modulesEdit'))
179 1
		{
180
			if ($postArray['id'])
181
			{
182
				return 'admin/edit/modules/' . $postArray['id'];
183
			}
184
			return 'admin/view/modules';
185
		}
186
		return 'admin';
187
	}
188
}
189