| Conditions | 12 |
| Paths | 55 |
| Total Lines | 130 |
| Code Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 149 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 150 | { |
||
| 151 | $id = (int) $request->getAttribute('id'); |
||
| 152 | |||
| 153 | /** @var User $user */ |
||
| 154 | $user = $this->userRepository |
||
| 155 | ->with('roles') |
||
| 156 | ->find($id); |
||
| 157 | if (!$user) { |
||
|
|
|||
| 158 | $this->session->setFlash('error', 'Can not find the user'); |
||
| 159 | $this->logger->warning('Can not find user with id {id}', ['id' => $id]); |
||
| 160 | |||
| 161 | return new RedirectResponse( |
||
| 162 | $this->routeHelper->generateUrl('user_list') |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | $statusList = [ |
||
| 167 | '0' => 'Deactive', |
||
| 168 | '1' => 'Active', |
||
| 169 | ]; |
||
| 170 | |||
| 171 | $roles = $this->roleRepository->all(); |
||
| 172 | |||
| 173 | $currentRolesId = []; |
||
| 174 | foreach ($user->roles as $entity) { |
||
| 175 | $currentRolesId[] = $entity->id; |
||
| 176 | } |
||
| 177 | |||
| 178 | $entityToFormParam = (new UserParam())->fromEntity($user); |
||
| 179 | $entityToFormParam->setRoles($currentRolesId); |
||
| 180 | |||
| 181 | if ($request->getMethod() === 'GET') { |
||
| 182 | return new TemplateResponse( |
||
| 183 | $this->template, |
||
| 184 | 'user/edit', |
||
| 185 | [ |
||
| 186 | 'param' => $entityToFormParam, |
||
| 187 | 'status' => $statusList, |
||
| 188 | 'roles' => $roles |
||
| 189 | ] |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | |||
| 193 | $param = new RequestData($request); |
||
| 194 | $formParam = new UserParam($param->posts()); |
||
| 195 | $validator = new UserValidator($formParam); |
||
| 196 | |||
| 197 | if (!$validator->validate()) { |
||
| 198 | return new TemplateResponse( |
||
| 199 | $this->template, |
||
| 200 | 'user/edit', |
||
| 201 | [ |
||
| 202 | 'errors' => $validator->getErrors(), |
||
| 203 | 'param' => $formParam, |
||
| 204 | 'status' => $statusList, |
||
| 205 | 'roles' => $roles |
||
| 206 | ] |
||
| 207 | ); |
||
| 208 | } |
||
| 209 | |||
| 210 | $username = $param->post('username'); |
||
| 211 | $userExist = $this->userRepository->findBy(['username' => $username]); |
||
| 212 | |||
| 213 | if ($userExist && $userExist->id != $id) { |
||
| 214 | $this->session->setFlash('error', 'This user already exists'); |
||
| 215 | $this->logger->error('User with username {username} already exists', ['username' => $username]); |
||
| 216 | return new TemplateResponse( |
||
| 217 | $this->template, |
||
| 218 | 'user/edit', |
||
| 219 | [ |
||
| 220 | 'param' => $formParam, |
||
| 221 | 'status' => $statusList, |
||
| 222 | 'roles' => $roles |
||
| 223 | ] |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | $password = $param->post('password'); |
||
| 228 | |||
| 229 | $user->username = $formParam->getUsername(); |
||
| 230 | $user->firstname = Str::ucfirst($formParam->getFirstname()); |
||
| 231 | $user->lastname = Str::upper($formParam->getLastname()); |
||
| 232 | $user->role = $formParam->getRole(); |
||
| 233 | $user->email = $formParam->getEmail(); |
||
| 234 | $user->status = (bool) $formParam->getStatus(); |
||
| 235 | $user->updated_at = date('Y-m-d H::s'); |
||
| 236 | |||
| 237 | if (!empty($password)) { |
||
| 238 | $passwordHash = $this->hash->hash($password); |
||
| 239 | $user->password = $passwordHash; |
||
| 240 | } |
||
| 241 | |||
| 242 | //Handle roles |
||
| 243 | $rolesId = $param->post('roles', []); |
||
| 244 | $rolesIdToDelete = array_diff($currentRolesId, $rolesId); |
||
| 245 | if (!empty($rolesIdToDelete)) { |
||
| 246 | $deletedRoles = $this->roleRepository->findAll(...$rolesIdToDelete); |
||
| 247 | $user->removeRoles($deletedRoles); |
||
| 248 | } |
||
| 249 | $newRolesId = array_diff($rolesId, $currentRolesId); |
||
| 250 | if (!empty($newRolesId)) { |
||
| 251 | $newRoles = $this->roleRepository->findAll(...$newRolesId); |
||
| 252 | $user->setRoles($newRoles); |
||
| 253 | } |
||
| 254 | ///////////////// |
||
| 255 | |||
| 256 | $result = $this->userRepository->save($user); |
||
| 257 | |||
| 258 | if (!$result) { |
||
| 259 | $this->session->setFlash('error', 'Error when saved the user'); |
||
| 260 | $this->logger->error('Error when saved the user'); |
||
| 261 | return new TemplateResponse( |
||
| 262 | $this->template, |
||
| 263 | 'user/edit', |
||
| 264 | [ |
||
| 265 | 'param' => $formParam, |
||
| 266 | 'status' => $statusList, |
||
| 267 | 'roles' => $roles |
||
| 268 | ] |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | $this->session->setFlash('success', 'User saved successfully'); |
||
| 273 | |||
| 274 | $returnUrl = $this->routeHelper->generateUrl('user_list'); |
||
| 275 | if ($param->get('from_detail')) { |
||
| 276 | $returnUrl = $this->routeHelper->generateUrl('user_detail', ['id' => $id]); |
||
| 277 | } |
||
| 278 | return new RedirectResponse($returnUrl); |
||
| 279 | } |
||
| 281 |