Complex classes like EntityController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EntityController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class EntityController extends Controller |
||
| 22 | { |
||
| 23 | 6 | public function listAction(Request $request) |
|
| 42 | |||
| 43 | 2 | public function postAction(Request $request) |
|
| 63 | |||
| 64 | 6 | public function getAction(Request $request, $id) |
|
| 74 | |||
| 75 | 6 | public function putAction(Request $request, $id) |
|
| 76 | { |
||
| 77 | 6 | $entity = $this->fetchEntity($id); |
|
| 78 | 6 | $this->assertPutGranted($entity); |
|
| 79 | 2 | $entity = $this->parseRequest($request, $entity, $this->getEntityClass()); |
|
| 80 | 2 | $entity = $this->postProcessPuttedEntity($entity); |
|
| 81 | |||
| 82 | /** @var ValidatorInterface $validator */ |
||
| 83 | 2 | $validator = $this->get('validator'); |
|
| 84 | 2 | $errors = $validator->validate($entity); |
|
| 85 | 2 | if ($errors->count() > 0) { |
|
| 86 | return new JsonResponse($this->parseConstraintViolations($errors), Response::HTTP_BAD_REQUEST); |
||
| 87 | } |
||
| 88 | |||
| 89 | 2 | $entity = $this->updateEntity($entity); |
|
| 90 | |||
| 91 | 2 | $normalizer = $this->get('ddr_rest.normalizer'); |
|
| 92 | 2 | $content = $normalizer->normalize($entity); |
|
| 93 | |||
| 94 | 2 | return new JsonResponse($content); |
|
| 95 | } |
||
| 96 | |||
| 97 | 2 | public function deleteAction(Request $request, $id) |
|
| 105 | |||
| 106 | public function listSubresourceAction(Request $request, $id) |
||
| 122 | |||
| 123 | public function postSubresourceAction(Request $request, $id) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return CrudServiceInterface |
||
| 141 | */ |
||
| 142 | 18 | protected function getService() |
|
| 163 | |||
| 164 | 2 | protected function parseRequest(Request $request, $entity = null, $entityClass = null) |
|
| 165 | { |
||
| 166 | 2 | return $this->get('ddr.rest.parser.request')->parseEntity($request, $entityClass, $entity); |
|
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param object $entity |
||
| 171 | * |
||
| 172 | * @return object |
||
| 173 | */ |
||
| 174 | protected function postProcessPostedEntity($entity) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param object $entity |
||
| 181 | * |
||
| 182 | * @return object |
||
| 183 | */ |
||
| 184 | 2 | protected function postProcessPuttedEntity($entity) |
|
| 185 | { |
||
| 186 | 2 | return $entity; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $subresource |
||
| 191 | * @param object $parent |
||
| 192 | * @param object $entity |
||
| 193 | * |
||
| 194 | * @return object |
||
| 195 | */ |
||
| 196 | protected function postProcessSubResourcePostedEntity($subresource, $entity, $parent) |
||
| 200 | |||
| 201 | 14 | protected function fetchEntity($id) |
|
| 210 | |||
| 211 | 4 | protected function listEntities(int $page = 1, int $perPage = 50): Paginator |
|
| 217 | |||
| 218 | protected function createEntity($entity) |
||
| 222 | |||
| 223 | 2 | protected function updateEntity($entity) |
|
| 224 | { |
||
| 225 | 2 | return $this->getService()->update($entity); |
|
| 226 | } |
||
| 227 | |||
| 228 | protected function listSubresource($entity, $subresource, $page = 1, $perPage = 50) |
||
| 234 | |||
| 235 | 22 | protected function getEntityClass() |
|
| 239 | |||
| 240 | protected function getShortName() |
||
| 244 | |||
| 245 | 18 | protected function getServiceId() |
|
| 249 | |||
| 250 | 22 | protected function getCurrentRequest() |
|
| 254 | |||
| 255 | 6 | protected function assertListGranted() |
|
| 256 | { |
||
| 257 | 6 | $classMetadata = $this->getClassMetadata(); |
|
| 258 | 6 | $right = $classMetadata->getListRight(); |
|
| 259 | 6 | if (null === $right) { |
|
| 260 | 2 | return; |
|
| 261 | } |
||
| 262 | |||
| 263 | 4 | $this->denyAccessUnlessGranted($right->attributes); |
|
| 264 | 2 | } |
|
| 265 | |||
| 266 | 2 | protected function assertPostGranted() |
|
| 276 | |||
| 277 | 6 | protected function assertGetGranted($entity) |
|
| 278 | { |
||
| 279 | 6 | $classMetadata = $this->getClassMetadata(); |
|
| 280 | 6 | $right = $classMetadata->getGetRight(); |
|
| 281 | 6 | if (null === $right) { |
|
| 282 | 2 | return; |
|
| 283 | } |
||
| 284 | |||
| 285 | 4 | $this->assertRightGranted($entity, $right); |
|
| 286 | 2 | } |
|
| 287 | |||
| 288 | 6 | protected function assertPutGranted($entity) |
|
| 289 | { |
||
| 290 | 6 | $classMetadata = $this->getClassMetadata(); |
|
| 291 | 6 | $right = $classMetadata->getPutRight(); |
|
| 292 | 6 | if (null === $right) { |
|
| 293 | 2 | throw $this->createAccessDeniedException(); |
|
| 294 | } |
||
| 295 | |||
| 296 | 4 | $this->assertRightGranted($entity, $right); |
|
| 297 | 2 | } |
|
| 298 | |||
| 299 | 2 | protected function assertDeleteGranted($entity) |
|
| 309 | |||
| 310 | protected function assertSubresourceListGranted($entity, $subresource) |
||
| 322 | |||
| 323 | protected function assertSubresourcePostGranted($entity, $subresource) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return ClassMetadata |
||
| 338 | */ |
||
| 339 | 22 | protected function getClassMetadata() |
|
| 347 | |||
| 348 | protected function getSubResourceEntityClass($subresource) |
||
| 355 | |||
| 356 | protected function resolveSubject($entity, $propertyPath) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param object $entity |
||
| 368 | * @param Right $right |
||
| 369 | */ |
||
| 370 | 8 | protected function assertRightGranted($entity, Right $right) |
|
| 371 | { |
||
| 372 | 8 | $propertyPath = $right->propertyPath; |
|
| 373 | 8 | if (null === $propertyPath) { |
|
| 374 | 8 | $this->denyAccessUnlessGranted($right->attributes); |
|
| 375 | } else { |
||
| 376 | $subject = $this->resolveSubject($entity, $propertyPath); |
||
| 377 | $this->denyAccessUnlessGranted($right->attributes, $subject); |
||
| 378 | } |
||
| 379 | 4 | } |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @return string[] |
||
| 383 | */ |
||
| 384 | protected function getSubresourceSerializationGroups($subresource) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $subresource |
||
| 391 | * @param object $entity |
||
| 392 | * |
||
| 393 | * @return |
||
| 394 | */ |
||
| 395 | protected function saveSubResource($subresource, $entity) |
||
| 396 | { |
||
| 397 | return $this->getService()->save($entity); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return string|null |
||
| 402 | */ |
||
| 403 | protected function getSubresource() |
||
| 407 | |||
| 408 | 8 | protected function parseIncludes(Request $request) |
|
| 417 | |||
| 418 | private function parseConstraintViolations(ConstraintViolationListInterface $errors) |
||
| 432 | |||
| 433 | 4 | private function addPaginationHeaders(Response $response, int $page, int $perPage, int $total) |
|
| 444 | } |
||
| 445 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.