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