| Conditions | 9 |
| Paths | 20 |
| Total Lines | 99 |
| Code Lines | 60 |
| 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 |
||
| 214 | public function update(ServerRequestInterface $request): ResponseInterface |
||
| 215 | { |
||
| 216 | $context = []; |
||
| 217 | $param = new RequestData($request); |
||
| 218 | |||
| 219 | $id = (int) $request->getAttribute('id'); |
||
| 220 | |||
| 221 | /** @var Role|null $role */ |
||
| 222 | $role = $this->roleRepository->find($id); |
||
| 223 | |||
| 224 | if ($role === null) { |
||
| 225 | $this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
||
| 226 | |||
| 227 | return new RedirectResponse( |
||
| 228 | $this->routeHelper->generateUrl('role_list') |
||
| 229 | ); |
||
| 230 | } |
||
| 231 | $context['role'] = $role; |
||
| 232 | $context['param'] = (new RoleParam())->fromEntity($role); |
||
| 233 | |||
| 234 | $currentPermissionsId = Arr::getColumn($role->permissions, 'id'); |
||
| 235 | $context['param']->setPermissions($currentPermissionsId); |
||
| 236 | |||
| 237 | $permissions = $this->permissionRepository->orderBy('code') |
||
| 238 | ->all(); |
||
| 239 | |||
| 240 | $context['permissions'] = $permissions; |
||
| 241 | |||
| 242 | if ($request->getMethod() === 'GET') { |
||
| 243 | return new TemplateResponse( |
||
| 244 | $this->template, |
||
| 245 | 'role/update', |
||
| 246 | $context |
||
| 247 | ); |
||
| 248 | } |
||
| 249 | $formParam = new RoleParam($param->posts()); |
||
| 250 | $context['param'] = $formParam; |
||
| 251 | |||
| 252 | $validator = new RoleValidator($formParam, $this->lang); |
||
| 253 | if ($validator->validate() === false) { |
||
| 254 | $context['errors'] = $validator->getErrors(); |
||
| 255 | |||
| 256 | return new TemplateResponse( |
||
| 257 | $this->template, |
||
| 258 | 'role/update', |
||
| 259 | $context |
||
| 260 | ); |
||
| 261 | } |
||
| 262 | |||
| 263 | $entityExist = $this->roleRepository->findBy([ |
||
| 264 | 'name' => $formParam->getName(), |
||
| 265 | ]); |
||
| 266 | |||
| 267 | if ($entityExist !== null && $entityExist->id !== $id) { |
||
| 268 | $this->flash->setError($this->lang->tr('This record already exist')); |
||
| 269 | |||
| 270 | return new TemplateResponse( |
||
| 271 | $this->template, |
||
| 272 | 'role/update', |
||
| 273 | $context |
||
| 274 | ); |
||
| 275 | } |
||
| 276 | |||
| 277 | $role->name = $formParam->getName(); |
||
| 278 | $role->description = $formParam->getDescription(); |
||
| 279 | |||
| 280 | $permissionsId = $formParam->getPermissions(); |
||
| 281 | |||
| 282 | //Delete Handle |
||
| 283 | $permissionsIdToDelete = array_diff($currentPermissionsId, $permissionsId); |
||
| 284 | if (count($permissionsIdToDelete) > 0) { |
||
| 285 | $deletedPermissions = $this->permissionRepository->findAll(...$permissionsIdToDelete); |
||
| 286 | $role->removePermissions($deletedPermissions); |
||
| 287 | } |
||
| 288 | |||
| 289 | //New Handle |
||
| 290 | $permissionsIdToAdd = array_diff($permissionsId, $currentPermissionsId); |
||
| 291 | if (count($permissionsIdToAdd) > 0) { |
||
| 292 | $addedPermissions = $this->permissionRepository->findAll(...$permissionsIdToAdd); |
||
| 293 | $role->setPermissions($addedPermissions); |
||
| 294 | } |
||
| 295 | |||
| 296 | try { |
||
| 297 | $this->roleRepository->save($role); |
||
| 298 | |||
| 299 | $this->flash->setSuccess($this->lang->tr('Data successfully updated')); |
||
| 300 | |||
| 301 | return new RedirectResponse( |
||
| 302 | $this->routeHelper->generateUrl('role_detail', ['id' => $id]) |
||
| 303 | ); |
||
| 304 | } catch (Exception $ex) { |
||
| 305 | $this->logger->error('Error when saved the data {error}', ['error' => $ex->getMessage()]); |
||
| 306 | |||
| 307 | $this->flash->setError($this->lang->tr('Data processing error')); |
||
| 308 | |||
| 309 | return new TemplateResponse( |
||
| 310 | $this->template, |
||
| 311 | 'role/update', |
||
| 312 | $context |
||
| 313 | ); |
||
| 356 |