| Conditions | 7 |
| Paths | 6 |
| Total Lines | 88 |
| Code Lines | 52 |
| 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 |
||
| 129 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 130 | { |
||
| 131 | $id = (int) $request->getAttribute('id'); |
||
| 132 | |||
| 133 | /** @var Permission $permission */ |
||
| 134 | $permission = $this->permissionRepository->find($id); |
||
| 135 | if (!$permission) { |
||
|
|
|||
| 136 | $this->session->setFlash('error', 'Can not find the permission'); |
||
| 137 | $this->logger->warning('Can not find permission with id {id}', ['id' => $id]); |
||
| 138 | |||
| 139 | return new RedirectResponse( |
||
| 140 | $this->routeHelper->generateUrl('permission_list') |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | $permissions = $this->permissionRepository |
||
| 145 | ->orderBy('code') |
||
| 146 | ->all(); |
||
| 147 | |||
| 148 | |||
| 149 | $entityToFormParam = (new PermissionParam())->fromEntity($permission); |
||
| 150 | |||
| 151 | if ($request->getMethod() === 'GET') { |
||
| 152 | return new TemplateResponse( |
||
| 153 | $this->template, |
||
| 154 | 'permission/edit', |
||
| 155 | [ |
||
| 156 | 'param' => $entityToFormParam, |
||
| 157 | 'permissions' => $permissions |
||
| 158 | ] |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | $param = new RequestData($request); |
||
| 163 | $formParam = new PermissionParam($param->posts()); |
||
| 164 | $validator = new PermissionValidator($formParam); |
||
| 165 | |||
| 166 | if (!$validator->validate()) { |
||
| 167 | return new TemplateResponse( |
||
| 168 | $this->template, |
||
| 169 | 'permission/edit', |
||
| 170 | [ |
||
| 171 | 'errors' => $validator->getErrors(), |
||
| 172 | 'param' => $formParam, |
||
| 173 | 'permissions' => $permissions |
||
| 174 | ] |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | |||
| 178 | $code = $param->post('code'); |
||
| 179 | $permissionExist = $this->permissionRepository->findBy(['code' => $code]); |
||
| 180 | |||
| 181 | if ($permissionExist && $permissionExist->id != $id) { |
||
| 182 | $this->session->setFlash('error', 'This permission already exists'); |
||
| 183 | $this->logger->error('Permission with code {code} already exists', ['code' => $code]); |
||
| 184 | return new TemplateResponse( |
||
| 185 | $this->template, |
||
| 186 | 'permission/edit', |
||
| 187 | [ |
||
| 188 | 'param' => $formParam, |
||
| 189 | 'permissions' => $permissions |
||
| 190 | ] |
||
| 191 | ); |
||
| 192 | } |
||
| 193 | |||
| 194 | $permission->code = $formParam->getCode(); |
||
| 195 | $permission->description = $formParam->getDescription(); |
||
| 196 | $permission->depend = $formParam->getDepend(); |
||
| 197 | |||
| 198 | $result = $this->permissionRepository->save($permission); |
||
| 199 | |||
| 200 | if (!$result) { |
||
| 201 | $this->session->setFlash('error', 'Error when saved the permission'); |
||
| 202 | $this->logger->error('Error when saved the permission'); |
||
| 203 | return new TemplateResponse( |
||
| 204 | $this->template, |
||
| 205 | 'permission/edit', |
||
| 206 | [ |
||
| 207 | 'param' => $formParam, |
||
| 208 | 'permissions' => $permissions |
||
| 209 | ] |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | |||
| 213 | $this->session->setFlash('success', 'Permission saved successfully'); |
||
| 214 | |||
| 215 | return new RedirectResponse( |
||
| 216 | $this->routeHelper->generateUrl('permission_list') |
||
| 217 | ); |
||
| 220 |