Complex classes like ResourceController 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 ResourceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class ResourceController extends Controller |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var MetadataInterface |
||
| 38 | */ |
||
| 39 | protected $metadata; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var RequestConfigurationFactoryInterface |
||
| 43 | */ |
||
| 44 | protected $requestConfigurationFactory; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var ViewHandlerInterface |
||
| 48 | */ |
||
| 49 | protected $viewHandler; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var RepositoryInterface |
||
| 53 | */ |
||
| 54 | protected $repository; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var FactoryInterface |
||
| 58 | */ |
||
| 59 | protected $factory; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var NewResourceFactoryInterface |
||
| 63 | */ |
||
| 64 | protected $newResourceFactory; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var ObjectManager |
||
| 68 | */ |
||
| 69 | protected $manager; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var SingleResourceProviderInterface |
||
| 73 | */ |
||
| 74 | protected $singleResourceProvider; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var ResourcesCollectionProviderInterface |
||
| 78 | */ |
||
| 79 | protected $resourcesCollectionProvider; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var ResourceFormFactoryInterface |
||
| 83 | */ |
||
| 84 | protected $resourceFormFactory; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var RedirectHandlerInterface |
||
| 88 | */ |
||
| 89 | protected $redirectHandler; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var FlashHelperInterface |
||
| 93 | */ |
||
| 94 | protected $flashHelper; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var AuthorizationCheckerInterface |
||
| 98 | */ |
||
| 99 | protected $authorizationChecker; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var EventDispatcherInterface |
||
| 103 | */ |
||
| 104 | protected $eventDispatcher; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var StateMachineInterface |
||
| 108 | */ |
||
| 109 | protected $stateMachine; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var ResourceUpdateHandlerInterface |
||
| 113 | */ |
||
| 114 | protected $resourceUpdateHandler; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var ResourceDeleteHandlerInterface |
||
| 118 | */ |
||
| 119 | protected $resourceDeleteHandler; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param MetadataInterface $metadata |
||
| 123 | * @param RequestConfigurationFactoryInterface $requestConfigurationFactory |
||
| 124 | * @param ViewHandlerInterface $viewHandler |
||
| 125 | * @param RepositoryInterface $repository |
||
| 126 | * @param FactoryInterface $factory |
||
| 127 | * @param NewResourceFactoryInterface $newResourceFactory |
||
| 128 | * @param ObjectManager $manager |
||
| 129 | * @param SingleResourceProviderInterface $singleResourceProvider |
||
| 130 | * @param ResourcesCollectionProviderInterface $resourcesFinder |
||
| 131 | * @param ResourceFormFactoryInterface $resourceFormFactory |
||
| 132 | * @param RedirectHandlerInterface $redirectHandler |
||
| 133 | * @param FlashHelperInterface $flashHelper |
||
| 134 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
| 135 | * @param EventDispatcherInterface $eventDispatcher |
||
| 136 | * @param StateMachineInterface $stateMachine |
||
| 137 | * @param ResourceUpdateHandlerInterface $resourceUpdateHandler |
||
| 138 | * @param ResourceDeleteHandlerInterface $resourceDeleteHandler |
||
| 139 | */ |
||
| 140 | public function __construct( |
||
| 141 | MetadataInterface $metadata, |
||
| 142 | RequestConfigurationFactoryInterface $requestConfigurationFactory, |
||
| 143 | ViewHandlerInterface $viewHandler, |
||
| 144 | RepositoryInterface $repository, |
||
| 145 | FactoryInterface $factory, |
||
| 146 | NewResourceFactoryInterface $newResourceFactory, |
||
| 147 | ObjectManager $manager, |
||
| 148 | SingleResourceProviderInterface $singleResourceProvider, |
||
| 149 | ResourcesCollectionProviderInterface $resourcesFinder, |
||
| 150 | ResourceFormFactoryInterface $resourceFormFactory, |
||
| 151 | RedirectHandlerInterface $redirectHandler, |
||
| 152 | FlashHelperInterface $flashHelper, |
||
| 153 | AuthorizationCheckerInterface $authorizationChecker, |
||
| 154 | EventDispatcherInterface $eventDispatcher, |
||
| 155 | StateMachineInterface $stateMachine, |
||
| 156 | ResourceUpdateHandlerInterface $resourceUpdateHandler, |
||
| 157 | ResourceDeleteHandlerInterface $resourceDeleteHandler |
||
| 158 | ) { |
||
| 159 | $this->metadata = $metadata; |
||
| 160 | $this->requestConfigurationFactory = $requestConfigurationFactory; |
||
| 161 | $this->viewHandler = $viewHandler; |
||
| 162 | $this->repository = $repository; |
||
| 163 | $this->factory = $factory; |
||
| 164 | $this->newResourceFactory = $newResourceFactory; |
||
| 165 | $this->manager = $manager; |
||
| 166 | $this->singleResourceProvider = $singleResourceProvider; |
||
| 167 | $this->resourcesCollectionProvider = $resourcesFinder; |
||
| 168 | $this->resourceFormFactory = $resourceFormFactory; |
||
| 169 | $this->redirectHandler = $redirectHandler; |
||
| 170 | $this->flashHelper = $flashHelper; |
||
| 171 | $this->authorizationChecker = $authorizationChecker; |
||
| 172 | $this->eventDispatcher = $eventDispatcher; |
||
| 173 | $this->stateMachine = $stateMachine; |
||
| 174 | $this->resourceUpdateHandler = $resourceUpdateHandler; |
||
| 175 | $this->resourceDeleteHandler = $resourceDeleteHandler; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param Request $request |
||
| 180 | * |
||
| 181 | * @return Response |
||
| 182 | */ |
||
| 183 | public function showAction(Request $request): Response |
||
| 184 | { |
||
| 185 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
| 186 | |||
| 187 | $this->isGrantedOr403($configuration, ResourceActions::SHOW); |
||
| 188 | $resource = $this->findOr404($configuration); |
||
| 189 | |||
| 190 | $this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource); |
||
| 191 | |||
| 192 | $view = View::create($resource); |
||
| 193 | |||
| 194 | if ($configuration->isHtmlRequest()) { |
||
| 195 | $view |
||
| 196 | ->setTemplate($configuration->getTemplate(ResourceActions::SHOW . '.html')) |
||
| 197 | ->setTemplateVar($this->metadata->getName()) |
||
| 198 | ->setData([ |
||
| 199 | 'configuration' => $configuration, |
||
| 200 | 'metadata' => $this->metadata, |
||
| 201 | 'resource' => $resource, |
||
| 202 | $this->metadata->getName() => $resource, |
||
| 203 | ]) |
||
| 204 | ; |
||
| 205 | } |
||
| 206 | |||
| 207 | return $this->viewHandler->handle($configuration, $view); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param Request $request |
||
| 212 | * |
||
| 213 | * @return Response |
||
| 214 | */ |
||
| 215 | public function indexAction(Request $request): Response |
||
| 216 | { |
||
| 217 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
| 218 | |||
| 219 | $this->isGrantedOr403($configuration, ResourceActions::INDEX); |
||
| 220 | $resources = $this->resourcesCollectionProvider->get($configuration, $this->repository); |
||
| 221 | |||
| 222 | $this->eventDispatcher->dispatchMultiple(ResourceActions::INDEX, $configuration, $resources); |
||
| 223 | |||
| 224 | $view = View::create($resources); |
||
| 225 | |||
| 226 | if ($configuration->isHtmlRequest()) { |
||
| 227 | $view |
||
| 228 | ->setTemplate($configuration->getTemplate(ResourceActions::INDEX . '.html')) |
||
| 229 | ->setTemplateVar($this->metadata->getPluralName()) |
||
| 230 | ->setData([ |
||
| 231 | 'configuration' => $configuration, |
||
| 232 | 'metadata' => $this->metadata, |
||
| 233 | 'resources' => $resources, |
||
| 234 | $this->metadata->getPluralName() => $resources, |
||
| 235 | ]) |
||
| 236 | ; |
||
| 237 | } |
||
| 238 | |||
| 239 | return $this->viewHandler->handle($configuration, $view); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param Request $request |
||
| 244 | * |
||
| 245 | * @return Response |
||
| 246 | */ |
||
| 247 | public function createAction(Request $request): Response |
||
| 248 | { |
||
| 249 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
| 250 | |||
| 251 | $this->isGrantedOr403($configuration, ResourceActions::CREATE); |
||
| 252 | $newResource = $this->newResourceFactory->create($configuration, $this->factory); |
||
| 253 | |||
| 254 | $form = $this->resourceFormFactory->create($configuration, $newResource); |
||
| 255 | |||
| 256 | if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) { |
||
| 257 | $newResource = $form->getData(); |
||
| 258 | |||
| 259 | $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
| 260 | |||
| 261 | if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
||
| 262 | throw new HttpException($event->getErrorCode(), $event->getMessage()); |
||
| 263 | } |
||
| 264 | if ($event->isStopped()) { |
||
| 265 | $this->flashHelper->addFlashFromEvent($configuration, $event); |
||
| 266 | |||
| 267 | if ($event->hasResponse()) { |
||
| 268 | return $event->getResponse(); |
||
| 269 | } |
||
| 270 | |||
| 271 | return $this->redirectHandler->redirectToIndex($configuration, $newResource); |
||
| 272 | } |
||
| 273 | |||
| 274 | if ($configuration->hasStateMachine()) { |
||
| 275 | $this->stateMachine->apply($configuration, $newResource); |
||
| 276 | } |
||
| 277 | |||
| 278 | $this->repository->add($newResource); |
||
| 279 | $postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
| 280 | |||
| 281 | if (!$configuration->isHtmlRequest()) { |
||
| 282 | return $this->viewHandler->handle($configuration, View::create($newResource, Response::HTTP_CREATED)); |
||
| 283 | } |
||
| 284 | |||
| 285 | $this->flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource); |
||
| 286 | |||
| 287 | if ($postEvent->hasResponse()) { |
||
| 288 | return $postEvent->getResponse(); |
||
| 289 | } |
||
| 290 | |||
| 291 | return $this->redirectHandler->redirectToResource($configuration, $newResource); |
||
| 292 | } |
||
| 293 | |||
| 294 | if (!$configuration->isHtmlRequest()) { |
||
| 295 | return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST)); |
||
| 296 | } |
||
| 297 | |||
| 298 | $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
| 299 | |||
| 300 | $view = View::create() |
||
| 301 | ->setData([ |
||
| 302 | 'configuration' => $configuration, |
||
| 303 | 'metadata' => $this->metadata, |
||
| 304 | 'resource' => $newResource, |
||
| 305 | $this->metadata->getName() => $newResource, |
||
| 306 | 'form' => $form->createView(), |
||
| 307 | ]) |
||
| 308 | ->setTemplate($configuration->getTemplate(ResourceActions::CREATE . '.html')) |
||
| 309 | ; |
||
| 310 | |||
| 311 | return $this->viewHandler->handle($configuration, $view); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param Request $request |
||
| 316 | * |
||
| 317 | * @return Response |
||
| 318 | */ |
||
| 319 | public function updateAction(Request $request): Response |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param Request $request |
||
| 401 | * |
||
| 402 | * @return Response |
||
| 403 | */ |
||
| 404 | public function deleteAction(Request $request): Response |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param Request $request |
||
| 462 | * |
||
| 463 | * @return Response |
||
| 464 | */ |
||
| 465 | public function applyStateMachineTransitionAction(Request $request): Response |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @param RequestConfiguration $configuration |
||
| 525 | * @param string $permission |
||
| 526 | * |
||
| 527 | * @throws AccessDeniedException |
||
| 528 | */ |
||
| 529 | protected function isGrantedOr403(RequestConfiguration $configuration, string $permission): void |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @param RequestConfiguration $configuration |
||
| 544 | * |
||
| 545 | * @return ResourceInterface |
||
| 546 | * |
||
| 547 | * @throws NotFoundHttpException |
||
| 548 | */ |
||
| 549 | protected function findOr404(RequestConfiguration $configuration): ResourceInterface |
||
| 557 | } |
||
| 558 |