| Conditions | 6 |
| Paths | 7 |
| Total Lines | 83 |
| Code Lines | 48 |
| 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 |
||
| 138 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 139 | { |
||
| 140 | $permissions = $this->permissionRepository |
||
| 141 | ->orderBy('code') |
||
| 142 | ->all(); |
||
| 143 | |||
| 144 | if ($request->getMethod() === 'GET') { |
||
| 145 | return new TemplateResponse( |
||
| 146 | $this->template, |
||
| 147 | 'role/create', |
||
| 148 | [ |
||
| 149 | 'param' => new RoleParam([]), |
||
| 150 | 'permissions' => $permissions |
||
| 151 | ] |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | $param = new RequestData($request); |
||
| 156 | $formParam = new RoleParam($param->posts()); |
||
| 157 | $validator = new RoleValidator($formParam); |
||
| 158 | |||
| 159 | if (!$validator->validate()) { |
||
| 160 | return new TemplateResponse( |
||
| 161 | $this->template, |
||
| 162 | 'role/create', |
||
| 163 | [ |
||
| 164 | 'errors' => $validator->getErrors(), |
||
| 165 | 'param' => $formParam, |
||
| 166 | 'permissions' => $permissions |
||
| 167 | ] |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | |||
| 171 | $name = $param->post('name'); |
||
| 172 | $roleExist = $this->roleRepository->findBy(['name' => $name]); |
||
| 173 | |||
| 174 | if ($roleExist) { |
||
| 175 | $this->logger->error('Role with name {name} already exists', ['name' => $name]); |
||
| 176 | $this->session->setFlash('error', 'This role already exists'); |
||
| 177 | return new TemplateResponse( |
||
| 178 | $this->template, |
||
| 179 | 'role/create', |
||
| 180 | [ |
||
| 181 | 'param' => $formParam, |
||
| 182 | 'permissions' => $permissions |
||
| 183 | ] |
||
| 184 | ); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** @var Role $role */ |
||
| 188 | $role = $this->roleRepository->create([ |
||
| 189 | 'name' => $formParam->getName(), |
||
| 190 | 'description' => $formParam->getDescription() |
||
| 191 | ]); |
||
| 192 | |||
| 193 | //Handle permissions |
||
| 194 | $permissionsId = $param->post('permissions', []); |
||
| 195 | if (!empty($permissionsId)) { |
||
| 196 | $selectedPermissions = $this->permissionRepository->findAll(...$permissionsId); |
||
| 197 | $role->setPermissions($selectedPermissions); |
||
| 198 | } |
||
| 199 | /////////////////// |
||
| 200 | |||
| 201 | $result = $this->roleRepository->save($role); |
||
| 202 | |||
| 203 | if (!$result) { |
||
| 204 | $this->session->setFlash('error', 'Error when saved the role'); |
||
| 205 | $this->logger->error('Error when saved the role'); |
||
| 206 | return new TemplateResponse( |
||
| 207 | $this->template, |
||
| 208 | 'role/create', |
||
| 209 | [ |
||
| 210 | 'param' => $formParam, |
||
| 211 | 'permissions' => $permissions |
||
| 212 | ] |
||
| 213 | ); |
||
| 214 | } |
||
| 215 | |||
| 216 | |||
| 217 | $this->session->setFlash('success', 'Role saved successfully'); |
||
| 218 | |||
| 219 | return new RedirectResponse( |
||
| 220 | $this->routeHelper->generateUrl('role_list') |
||
| 221 | ); |
||
| 224 |