| Conditions | 30 |
| Paths | 3404 |
| Total Lines | 155 |
| Code Lines | 106 |
| Lines | 33 |
| Ratio | 21.29 % |
| 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 |
||
| 129 | public function editPost(\Base $f3) |
||
| 130 | { |
||
| 131 | $this->csrf('@admin_usersdata_list'); |
||
| 132 | $this->redirectLoggedOutUser(); |
||
| 133 | |||
| 134 | if (false == $f3->get('isRoot')) { |
||
| 135 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 136 | return $f3->reroute('@admin'); |
||
| 137 | } |
||
| 138 | |||
| 139 | $view = $this->template_path . 'edit.phtml'; |
||
| 140 | |||
| 141 | // get current user details |
||
| 142 | $uuid = $f3->get('REQUEST.uuid'); |
||
| 143 | $usersModel = Models\Users::instance(); |
||
| 144 | $mapper = $usersModel->getDataMapper(); |
||
| 145 | $mapper->load(['uuid = ?', $uuid]); |
||
| 146 | |||
| 147 | if (null == $mapper->id) { |
||
| 148 | $this->notify(_('The entry no longer exists!'), 'error'); |
||
| 149 | return $f3->reroute('@admin_usersdata_list'); |
||
| 150 | } |
||
| 151 | |||
| 152 | $f3->set('breadcrumbs', [ |
||
| 153 | _('Admin') => 'admin', |
||
| 154 | _('Users') => $this->url('@admin_users_search', [ |
||
| 155 | 'search' => $mapper->users_uuid, |
||
| 156 | 'search_fields' => 'uuid', |
||
| 157 | 'type' => 'exact', |
||
| 158 | ]), |
||
| 159 | _('Data') => $this->url('@admin_usersdata_search', [ |
||
| 160 | 'search' => $mapper->users_uuid, |
||
| 161 | 'search_fields' => 'users_uuid', |
||
| 162 | 'order' => 'key', |
||
| 163 | 'type' => 'exact', |
||
| 164 | ]), |
||
| 165 | _('Edit') => '', |
||
| 166 | ]); |
||
| 167 | |||
| 168 | // only allow updating of these fields |
||
| 169 | $data = $f3->get('REQUEST'); |
||
| 170 | $fields = [ |
||
| 171 | 'value' |
||
| 172 | ]; |
||
| 173 | |||
| 174 | // check input data has values set for the above fields |
||
| 175 | foreach ($fields as $k => $field) { |
||
| 176 | if (!array_key_exists($field, $data)) { |
||
| 177 | $data[$field] = null; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | // then remove any input data fields that aren't in the above fields |
||
| 181 | foreach ($data as $field => $v) { |
||
| 182 | if (!array_key_exists($field, $data) || empty($data[$field])) { |
||
| 183 | unset($data[$field]); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | // type check for filtering and validation |
||
| 188 | $fRules = ''; |
||
| 189 | switch ($mapper->type) { |
||
| 190 | case 'text': |
||
| 191 | case 'textarea': |
||
| 192 | $fRules = 'trim|sanitize_string'; |
||
| 193 | break; |
||
| 194 | |||
| 195 | case 'html': |
||
| 196 | case 'markdown': |
||
| 197 | case 'ini': |
||
| 198 | case 'yaml': |
||
| 199 | // trust raw input! |
||
| 200 | $data['value'] = $f3->get('REQUEST_UNCLEAN.value'); |
||
| 201 | break; |
||
| 202 | |||
| 203 | case 'json': |
||
| 204 | $data['value'] = $f3->get('REQUEST_UNCLEAN.value'); |
||
| 205 | break; |
||
| 206 | |||
| 207 | case 'email': |
||
| 208 | $fRules = 'sanitize_email'; |
||
| 209 | $vRules = 'valid_email'; |
||
| 210 | break; |
||
| 211 | |||
| 212 | case 'url': |
||
| 213 | $fRules = 'trim|sanitize_string'; |
||
| 214 | $vRules = 'valid_url'; |
||
| 215 | break; |
||
| 216 | |||
| 217 | case 'numeric': |
||
| 218 | case 'whole_number': |
||
| 219 | case 'integer': |
||
| 220 | case 'boolean': |
||
| 221 | View Code Duplication | case 'float': |
|
| 222 | $fRules = 'trim|sanitize_string'; |
||
| 223 | if ('float' == $mapper->type) { |
||
| 224 | $fRules .= '|sanitize_floats'; |
||
| 225 | } else { |
||
| 226 | $fRules = 'sanitize_numbers'; |
||
| 227 | } |
||
| 228 | $vRules = $mapper->type; |
||
| 229 | break; |
||
| 230 | |||
| 231 | case 'date': |
||
| 232 | $fRules = 'trim|sanitize_string'; |
||
| 233 | $vRules = $mapper->type; |
||
| 234 | break; |
||
| 235 | } |
||
| 236 | |||
| 237 | if (!empty($fRules)) { |
||
| 238 | $this->filterRules(['value' => $fRules]); |
||
| 239 | } |
||
| 240 | View Code Duplication | if (!empty($vRules)) { |
|
| 241 | $this->validationRules(['value' => $vRules]); |
||
| 242 | $errors = $this->validate(false, ['value' => $data['value']]); |
||
| 243 | if (true !== $errors) { |
||
| 244 | $this->notify(['warning' => $this->validationErrors($errors)]); |
||
| 245 | $f3->set('form', $mapper->cast()); |
||
| 246 | echo \View::instance()->render($this->template_path . 'edit.phtml'); |
||
| 247 | return; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | // update required fields to check from ones which changed |
||
| 252 | // validate the entered data |
||
| 253 | $data['uuid'] = $uuid; |
||
| 254 | $mapper->copyfrom($data); |
||
| 255 | $mapper->validationRequired($fields); |
||
| 256 | $errors = $mapper->validate(false); |
||
| 257 | View Code Duplication | if (is_array($errors)) { |
|
| 258 | $this->notify(['warning' => $mapper->validationErrors($errors)]); |
||
| 259 | $f3->set('form', $f3->get('REQUEST')); |
||
| 260 | echo \View::instance()->render($view); |
||
| 261 | return; |
||
| 262 | } |
||
| 263 | |||
| 264 | // no change, do nothing |
||
| 265 | if ($mapper->changed()) { |
||
| 266 | $this->notify(_('There was nothing to change!'), 'info'); |
||
| 267 | return $f3->reroute('@admin_usersdata_list'); |
||
| 268 | } |
||
| 269 | |||
| 270 | // reset usermapper and copy in valid data |
||
| 271 | $mapper->load(['uuid = ?', $data['uuid']]); |
||
| 272 | $mapper->copyfrom($data); |
||
| 273 | View Code Duplication | if ($mapper->save()) { |
|
| 274 | $this->notify(_('The account data was updated!'), 'success'); |
||
| 275 | } else { |
||
| 276 | $this->notify(_('Unable to update account data!'), 'error'); |
||
| 277 | $f3->set('form', $f3->get('REQUEST')); |
||
| 278 | echo \View::instance()->render($view); |
||
| 279 | return; |
||
| 280 | } |
||
| 281 | |||
| 282 | $f3->reroute('@admin_usersdata_search' . '?search=' . $mapper->uuid); |
||
| 283 | } |
||
| 284 | |||
| 454 |