Completed
Push — master ( 96a033...7625bb )
by Henry
07:07
created

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

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