| Conditions | 17 |
| Paths | 1280 |
| Total Lines | 80 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 95 | public function updateUser() |
||
| 96 | { |
||
| 97 | $this->onlyUser(); |
||
| 98 | $this->onlyPost(); |
||
| 99 | |||
| 100 | $user = (object)$this->request->getDataFull(); |
||
| 101 | $redirectUrl = "/admin"; |
||
| 102 | |||
| 103 | if ($user->userId !== $this->session->get("userId") || isset($user->userRoleSelector) || isset($user->locked_out)) { |
||
| 104 | //an admin is trying to update a user or form tampered with |
||
| 105 | $this->onlyAdmin(); |
||
| 106 | $redirectUrl = "/admin/home/view-user/" . $user->userId; |
||
| 107 | } else { |
||
| 108 | //set the role to the original state for update |
||
| 109 | $beforeUser = $this->userModel->getUserDetailsById($user->userId); |
||
| 110 | $user->userRoleSelector = $beforeUser->roles_idroles; |
||
| 111 | $user->userLockedOut = $beforeUser->locked_out; |
||
| 112 | } |
||
| 113 | |||
| 114 | $userId = $user->userId; |
||
| 115 | $password = $user->forgotPassword ?? ""; |
||
| 116 | $confirm = $user->forgotConfirm ?? ""; |
||
| 117 | $resetPassword = false; |
||
| 118 | $error = false; |
||
| 119 | $registerErrors = new \stdClass(); |
||
| 120 | |||
| 121 | if($userId == 1 && $user->userLockedOut == 1) |
||
| 122 | { |
||
| 123 | $error = true; |
||
| 124 | $this->alertBox->setAlert("Original admin may not be deactivated", "error"); |
||
| 125 | } |
||
| 126 | |||
| 127 | if($userId == 1 && $user->userRoleSelector != 2) |
||
| 128 | { |
||
| 129 | $error = true; |
||
| 130 | $this->alertBox->setAlert("Original admin must stay admin", "error"); |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($password !== "" || $confirm !== "") { |
||
| 134 | //we are resetting the password |
||
| 135 | $resetPassword = true; |
||
| 136 | if ($password !== $confirm) { |
||
| 137 | $error = true; |
||
| 138 | $registerErrors->forgotPassword = "password and confirmation do not match"; |
||
| 139 | $registerErrors->forgotConfirm = "password and confirmation do not match"; |
||
| 140 | } |
||
| 141 | |||
| 142 | $passwordError = $this->isPasswordComplex($password); |
||
| 143 | if (!$passwordError["success"]) { |
||
| 144 | $error = true; |
||
| 145 | $registerErrors->forgotPassword = $passwordError["message"]; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | if ($user->userName == "") { |
||
| 150 | $error = true; |
||
| 151 | $registerErrors->userName = "name must not be empty"; |
||
| 152 | } |
||
| 153 | if ($user->userSurname == "") { |
||
| 154 | $error = true; |
||
| 155 | $registerErrors->userSurname = "surname must not be empty"; |
||
| 156 | } |
||
| 157 | if ($user->userUsername == "") { |
||
| 158 | $error = true; |
||
| 159 | $registerErrors->userUsername = "username must not be empty"; |
||
| 160 | } |
||
| 161 | |||
| 162 | if ($error) { |
||
| 163 | $this->session->set("registrationErrors", $registerErrors); |
||
| 164 | $this->response->redirect($redirectUrl); |
||
| 165 | } |
||
| 166 | |||
| 167 | if ($resetPassword) { |
||
| 168 | $this->userModel->resetPassword($userId, $password); |
||
| 169 | } |
||
| 170 | |||
| 171 | $this->userModel->updateUser($user); |
||
| 172 | |||
| 173 | $this->alertBox->setAlert('User details updated'); |
||
| 174 | $this->response->redirect($redirectUrl); |
||
| 175 | } |
||
| 224 | } |