| Conditions | 16 |
| Paths | 182 |
| Total Lines | 130 |
| Code Lines | 88 |
| Lines | 57 |
| Ratio | 43.85 % |
| 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 |
||
| 115 | public function editPost(\Base $f3) |
||
| 116 | { |
||
| 117 | $this->csrf('@admin_users_list'); |
||
| 118 | $this->redirectLoggedOutUser(); |
||
| 119 | |||
| 120 | if (false == $f3->get('isRoot')) { |
||
| 121 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 122 | return $f3->reroute('@admin'); |
||
| 123 | } |
||
| 124 | |||
| 125 | $view = $this->template_path . 'edit.phtml'; |
||
| 126 | |||
| 127 | $f3->set('breadcrumbs', [ |
||
| 128 | _('Admin') => 'admin', |
||
| 129 | _('Users') => 'admin_users_list', |
||
| 130 | _('Edit') => '', |
||
| 131 | ]); |
||
| 132 | |||
| 133 | // get current user details |
||
| 134 | $usersModel = Models\Users::instance(); |
||
| 135 | $uuid = $f3->get('REQUEST.uuid'); |
||
| 136 | $usersMapper = $usersModel->getUserByUUID($uuid); |
||
| 137 | View Code Duplication | if (null == $usersMapper->id) { |
|
| 138 | $this->notify(_('The account no longer exists!'), 'error'); |
||
| 139 | $f3->set('form', $f3->get('REQUEST')); |
||
| 140 | echo \View::instance()->render('user/account.phtml'); |
||
| 141 | return; |
||
| 142 | } |
||
| 143 | $oldUserMapper = clone $usersMapper; |
||
| 144 | |||
| 145 | // only allow updating of these fields |
||
| 146 | $data = $f3->get('REQUEST'); |
||
| 147 | $fields = [ |
||
| 148 | 'email', |
||
| 149 | 'password', |
||
| 150 | 'firstname', |
||
| 151 | 'lastname', |
||
| 152 | 'password_question', |
||
| 153 | 'password_answer', |
||
| 154 | 'scopes', |
||
| 155 | 'status', |
||
| 156 | ]; |
||
| 157 | |||
| 158 | // check input data has values set for the above fields |
||
| 159 | foreach ($fields as $k => $field) { |
||
| 160 | if (!array_key_exists($field, $data)) { |
||
| 161 | $data[$field] = null; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | // then remove any input data fields that aren't in the above fields |
||
| 165 | foreach ($data as $field => $v) { |
||
| 166 | if (!in_array($field, $fields)) { |
||
| 167 | unset($data[$field]); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | // is this a password change? if so, check they match |
||
| 172 | $str = Helpers\Str::instance(); |
||
| 173 | $password = $f3->get('REQUEST.password'); |
||
| 174 | $confirm_password = $f3->get('REQUEST.confirm_password'); |
||
| 175 | View Code Duplication | if (!empty($password) || !empty($confirm_password)) { |
|
| 176 | if ($password !== $confirm_password) { |
||
| 177 | $this->notify(_('That password and confirm password must match!'), 'warning'); |
||
| 178 | $f3->set('form', $f3->get('REQUEST')); |
||
| 179 | echo \View::instance()->render($view); |
||
| 180 | return; |
||
| 181 | } elseif ($str->passwordVerify($usersMapper->password, $password)) { |
||
| 182 | $this->notify(_('The new password and old password are the same!'), 'warning'); |
||
| 183 | $f3->set('form', $f3->get('REQUEST')); |
||
| 184 | echo \View::instance()->render($view); |
||
| 185 | return; |
||
| 186 | } else { |
||
| 187 | // set new hashed password |
||
| 188 | $data['password'] = $str->password($password); |
||
| 189 | } |
||
| 190 | } else { |
||
| 191 | // same password |
||
| 192 | $data['password'] = $usersMapper->password; |
||
| 193 | } |
||
| 194 | |||
| 195 | // check if email address change that email isn't taken |
||
| 196 | $email = $f3->get('REQUEST.email'); |
||
| 197 | View Code Duplication | if ($usersMapper->email !== $email) { |
|
| 198 | $usersMapper->load(['email = ?', $email]); |
||
| 199 | if ($usersMapper->email == $email) { |
||
| 200 | $this->notify(sprintf(_('The email address %s is already in use!'), $email), 'warning'); |
||
| 201 | $f3->set('form', $f3->get('REQUEST')); |
||
| 202 | echo \View::instance()->render($view); |
||
| 203 | return; |
||
| 204 | } else { |
||
| 205 | // new email |
||
| 206 | $data['email'] = $email; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | // update required fields to check from ones which changed |
||
| 211 | // validate the entered data |
||
| 212 | $data['uuid'] = $uuid; |
||
| 213 | $usersMapper->copyfrom($data); |
||
| 214 | $usersMapper->validationRequired($fields); |
||
| 215 | $errors = $usersMapper->validate(false); |
||
| 216 | View Code Duplication | if (is_array($errors)) { |
|
| 217 | $this->notify(['warning' => $usersMapper->validationErrors($errors)]); |
||
| 218 | $f3->set('form', $f3->get('REQUEST')); |
||
| 219 | echo \View::instance()->render($view); |
||
| 220 | return; |
||
| 221 | } |
||
| 222 | |||
| 223 | // no change, do nothing |
||
| 224 | View Code Duplication | if ($usersMapper->cast() === $oldUserMapper->cast()) { |
|
| 225 | $this->notify(_('There was nothing to change!'), 'info'); |
||
| 226 | $f3->set('form', $f3->get('REQUEST')); |
||
| 227 | echo \View::instance()->render($view); |
||
| 228 | return; |
||
| 229 | } |
||
| 230 | |||
| 231 | // reset usermapper and copy in valid data |
||
| 232 | $usersMapper->load(['uuid = ?', $data['uuid']]); |
||
| 233 | $usersMapper->copyfrom($data); |
||
| 234 | View Code Duplication | if ($usersMapper->save()) { |
|
| 235 | $this->notify(_('The account was updated!'), 'success'); |
||
| 236 | } else { |
||
| 237 | $this->notify(_('Unable to update your account!'), 'error'); |
||
| 238 | $f3->set('form', $f3->get('REQUEST')); |
||
| 239 | echo \View::instance()->render($view); |
||
| 240 | return; |
||
| 241 | } |
||
| 242 | |||
| 243 | $f3->reroute('@admin_users_search' . '?search=' . $usersMapper->uuid); |
||
| 244 | } |
||
| 245 | |||
| 280 |