| Conditions | 11 |
| Paths | 31 |
| Total Lines | 113 |
| Code Lines | 69 |
| 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 Role $role */ |
||
| 154 | $role = $this->roleRepository |
||
| 155 | ->with('permissions') |
||
| 156 | ->find($id); |
||
| 157 | if (!$role) { |
||
|
|
|||
| 158 | $this->session->setFlash('error', 'Can not find the role'); |
||
| 159 | $this->logger->warning('Can not find role with id {id}', ['id' => $id]); |
||
| 160 | |||
| 161 | return new RedirectResponse( |
||
| 162 | $this->routeHelper->generateUrl('role_list') |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | $permissions = $this->permissionRepository |
||
| 167 | ->orderBy('code') |
||
| 168 | ->all(); |
||
| 169 | |||
| 170 | $currentPermissionsId = []; |
||
| 171 | foreach ($role->permissions as $entity) { |
||
| 172 | $currentPermissionsId[] = $entity->id; |
||
| 173 | } |
||
| 174 | |||
| 175 | $entityToFormParam = (new RoleParam())->fromEntity($role); |
||
| 176 | $entityToFormParam->setPermissions($currentPermissionsId); |
||
| 177 | |||
| 178 | if ($request->getMethod() === 'GET') { |
||
| 179 | return new TemplateResponse( |
||
| 180 | $this->template, |
||
| 181 | 'role/edit', |
||
| 182 | [ |
||
| 183 | 'param' => $entityToFormParam, |
||
| 184 | 'permissions' => $permissions |
||
| 185 | ] |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | |||
| 189 | $param = new RequestData($request); |
||
| 190 | $formParam = new RoleParam($param->posts()); |
||
| 191 | $validator = new RoleValidator($formParam); |
||
| 192 | |||
| 193 | if (!$validator->validate()) { |
||
| 194 | return new TemplateResponse( |
||
| 195 | $this->template, |
||
| 196 | 'role/edit', |
||
| 197 | [ |
||
| 198 | 'errors' => $validator->getErrors(), |
||
| 199 | 'param' => $formParam, |
||
| 200 | 'permissions' => $permissions |
||
| 201 | ] |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | |||
| 205 | $name = $param->post('name'); |
||
| 206 | $roleExist = $this->roleRepository->findBy(['name' => $name]); |
||
| 207 | |||
| 208 | if ($roleExist && $roleExist->id != $id) { |
||
| 209 | $this->session->setFlash('error', 'This role already exists'); |
||
| 210 | $this->logger->error('Role with name {name} already exists', ['name' => $name]); |
||
| 211 | return new TemplateResponse( |
||
| 212 | $this->template, |
||
| 213 | 'role/edit', |
||
| 214 | [ |
||
| 215 | 'param' => $formParam, |
||
| 216 | 'permissions' => $permissions |
||
| 217 | ] |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | $password = $param->post('password'); |
||
| 222 | |||
| 223 | $role->name = $formParam->getName(); |
||
| 224 | $role->description = $formParam->getDescription(); |
||
| 225 | |||
| 226 | //Handle permissions |
||
| 227 | $permissionsId = $param->post('permissions', []); |
||
| 228 | $permissionsIdToDelete = array_diff($currentPermissionsId, $permissionsId); |
||
| 229 | if (!empty($permissionsIdToDelete)) { |
||
| 230 | $deletedPermissions = $this->permissionRepository->findAll(...$permissionsIdToDelete); |
||
| 231 | $role->removePermissions($deletedPermissions); |
||
| 232 | } |
||
| 233 | $newPermissionsId = array_diff($permissionsId, $currentPermissionsId); |
||
| 234 | if (!empty($newPermissionsId)) { |
||
| 235 | $newPermissions = $this->permissionRepository->findAll(...$newPermissionsId); |
||
| 236 | $role->setPermissions($newPermissions); |
||
| 237 | } |
||
| 238 | ///////////////// |
||
| 239 | |||
| 240 | $result = $this->roleRepository->save($role); |
||
| 241 | |||
| 242 | if (!$result) { |
||
| 243 | $this->session->setFlash('error', 'Error when saved the role'); |
||
| 244 | $this->logger->error('Error when saved the role'); |
||
| 245 | return new TemplateResponse( |
||
| 246 | $this->template, |
||
| 247 | 'role/edit', |
||
| 248 | [ |
||
| 249 | 'param' => $formParam, |
||
| 250 | 'permissions' => $permissions |
||
| 251 | ] |
||
| 252 | ); |
||
| 253 | } |
||
| 254 | |||
| 255 | $this->session->setFlash('success', 'Role saved successfully'); |
||
| 256 | |||
| 257 | $returnUrl = $this->routeHelper->generateUrl('role_list'); |
||
| 258 | if ($param->get('from_detail')) { |
||
| 259 | $returnUrl = $this->routeHelper->generateUrl('role_detail', ['id' => $id]); |
||
| 260 | } |
||
| 261 | return new RedirectResponse($returnUrl); |
||
| 262 | } |
||
| 264 |