| Conditions | 28 |
| Paths | 2648 |
| Total Lines | 135 |
| Code Lines | 88 |
| Lines | 27 |
| Ratio | 20 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 159 | public function editPost(\Base $f3) |
||
| 160 | { |
||
| 161 | $this->csrf('@admin_config_list'); |
||
| 162 | $this->redirectLoggedOutUser(); |
||
| 163 | |||
| 164 | if (false == $f3->get('isRoot')) { |
||
| 165 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 166 | return $f3->reroute('@admin'); |
||
| 167 | } |
||
| 168 | |||
| 169 | $view = $this->template_path . 'edit.phtml'; |
||
| 170 | |||
| 171 | $f3->set('breadcrumbs', [ |
||
| 172 | _('Admin') => 'admin', |
||
| 173 | _('Config') => 'admin_config_list', |
||
| 174 | _('Edit') => '', |
||
| 175 | ]); |
||
| 176 | |||
| 177 | // get current user details |
||
| 178 | $uuid = $f3->get('REQUEST.uuid'); |
||
| 179 | $mapper = new Mappers\ConfigData; |
||
| 180 | |||
| 181 | $mapper->load(['uuid = ?', $uuid]); |
||
| 182 | |||
| 183 | if (null == $mapper->id) { |
||
| 184 | $this->notify(_('The entry no longer exists!'), 'error'); |
||
| 185 | return $f3->reroute('@admin_config_list'); |
||
| 186 | } |
||
| 187 | |||
| 188 | // only allow updating of these fields |
||
| 189 | $data = $f3->get('REQUEST'); |
||
| 190 | $fields = [ |
||
| 191 | 'value', 'options', 'description' |
||
| 192 | ]; |
||
| 193 | |||
| 194 | // check input data has values set for the above fields |
||
| 195 | foreach ($fields as $k => $field) { |
||
| 196 | if (!array_key_exists($field, $data)) { |
||
| 197 | $data[$field] = null; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | // then remove any input data fields that aren't in the above fields |
||
| 201 | foreach ($data as $field => $v) { |
||
| 202 | if (!in_array($field, $fields)) { |
||
| 203 | unset($data[$field]); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | // type check for filtering and validation |
||
| 208 | $fRules = ''; |
||
| 209 | switch ($mapper->type) { |
||
| 210 | case 'text': |
||
| 211 | case 'textarea': |
||
| 212 | $fRules = 'trim|sanitize_string'; |
||
| 213 | break; |
||
| 214 | |||
| 215 | case 'html': |
||
| 216 | case 'markdown': |
||
| 217 | case 'ini': |
||
| 218 | case 'yaml': |
||
| 219 | // trust raw input! |
||
| 220 | $data['value'] = $f3->get('REQUEST_UNCLEAN.value'); |
||
| 221 | break; |
||
| 222 | |||
| 223 | case 'json': |
||
| 224 | $data['value'] = $f3->get('REQUEST_UNCLEAN.value'); |
||
| 225 | break; |
||
| 226 | |||
| 227 | case 'email': |
||
| 228 | $fRules = 'sanitize_email'; |
||
| 229 | $vRules = 'valid_email'; |
||
| 230 | break; |
||
| 231 | |||
| 232 | case 'url': |
||
| 233 | $vRules = 'valid_url'; |
||
| 234 | break; |
||
| 235 | |||
| 236 | case 'numeric': |
||
| 237 | case 'whole_number': |
||
| 238 | case 'integer': |
||
| 239 | case 'boolean': |
||
| 240 | View Code Duplication | case 'float': |
|
| 241 | $fRules = 'trim|sanitize_string'; |
||
| 242 | if ('float' == $mapper->type) { |
||
| 243 | $fRules .= '|sanitize_floats'; |
||
| 244 | } else { |
||
| 245 | $fRules = 'sanitize_numbers'; |
||
| 246 | } |
||
| 247 | $vRules = $mapper->type; |
||
| 248 | break; |
||
| 249 | |||
| 250 | case 'date': |
||
| 251 | $vRules = $mapper->type; |
||
| 252 | break; |
||
| 253 | } |
||
| 254 | |||
| 255 | if (!empty($fRules)) { |
||
| 256 | $this->filterRules(['value' => $fRules]); |
||
| 257 | } |
||
| 258 | View Code Duplication | if (!empty($vRules)) { |
|
| 259 | $this->validationRules(['value' => $vRules]); |
||
| 260 | $errors = $this->validate(false, ['value' => $data['value']]); |
||
| 261 | if (true !== $errors) { |
||
| 262 | $this->notify(['warning' => $this->validationErrors($errors)]); |
||
| 263 | $f3->set('form', $mapper->cast()); |
||
| 264 | echo \View::instance()->render($this->template_path . 'edit.phtml'); |
||
| 265 | return; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | // update required fields to check from ones which changed |
||
| 270 | // validate the entered data |
||
| 271 | $data['uuid'] = $uuid; |
||
| 272 | $mapper->copyfrom($data); |
||
| 273 | |||
| 274 | // no change, do nothing |
||
| 275 | if (!$mapper->changed()) { |
||
| 276 | $this->notify(_('There was nothing to change!'), 'info'); |
||
| 277 | return $f3->reroute('@admin_config_list'); |
||
| 278 | } |
||
| 279 | |||
| 280 | // reset usermapper and copy in valid data |
||
| 281 | $mapper->load(['uuid = ?', $data['uuid']]); |
||
| 282 | $mapper->copyfrom($data); |
||
| 283 | View Code Duplication | if ($mapper->save()) { |
|
| 284 | $this->notify(_('The config data was updated!'), 'success'); |
||
| 285 | } else { |
||
| 286 | $this->notify(_('Unable to update config data!'), 'error'); |
||
| 287 | $f3->set('form', $f3->get('REQUEST')); |
||
| 288 | echo \View::instance()->render($view); |
||
| 289 | return; |
||
| 290 | } |
||
| 291 | |||
| 292 | $f3->reroute('@admin_config_search' . '?search=' . $mapper->uuid); |
||
| 293 | } |
||
| 294 | |||
| 413 |