Complex classes like AdminController 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 AdminController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class AdminController |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var EngineInterface |
||
| 28 | */ |
||
| 29 | private $templating; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var ObjectManager |
||
| 33 | */ |
||
| 34 | private $entityManager; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var FormFactoryInterface |
||
| 38 | */ |
||
| 39 | private $formFactory; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var RouterInterface |
||
| 43 | */ |
||
| 44 | private $router; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var SessionInterface |
||
| 48 | */ |
||
| 49 | private $session; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var AdminContainer |
||
| 53 | */ |
||
| 54 | private $container; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var DatagridInterface |
||
| 58 | */ |
||
| 59 | private $datagrid; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $templates; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var ExporterInterface |
||
| 68 | */ |
||
| 69 | private $exporter; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var EventDispatcherInterface |
||
| 73 | */ |
||
| 74 | private $dispatcher; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param EngineInterface $templating |
||
| 78 | * @param ObjectManager $entityManager |
||
| 79 | * @param FormFactoryInterface $formFactory |
||
| 80 | * @param RouterInterface $router |
||
| 81 | * @param SessionInterface $session |
||
| 82 | * @param AdminContainer $container |
||
| 83 | * @param DatagridInterface $datagrid |
||
| 84 | * @param array $templates |
||
| 85 | * @param ExporterInterface $exporter |
||
| 86 | * @param EventDispatcherInterface $dispatcher |
||
| 87 | */ |
||
| 88 | public function __construct( |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Render the menu |
||
| 114 | * |
||
| 115 | * @param $request |
||
| 116 | * |
||
| 117 | * @return Response |
||
| 118 | */ |
||
| 119 | public function menuAction(Request $request) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Display the homepage/dashboard |
||
| 136 | * |
||
| 137 | * @return Response |
||
| 138 | */ |
||
| 139 | public function dashboardAction() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $name |
||
| 146 | * |
||
| 147 | * @return null|AdminInterface |
||
| 148 | */ |
||
| 149 | private function getAdminClass($name) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Display the listing page. |
||
| 160 | * Handles searches, sorting, actions and pagination on the list of entities. |
||
| 161 | * |
||
| 162 | * @param Request $request |
||
| 163 | * @param string $name |
||
| 164 | * |
||
| 165 | * @return Response |
||
| 166 | */ |
||
| 167 | public function listAction(Request $request, $name) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Export the listing |
||
| 190 | * |
||
| 191 | * @param Request $request |
||
| 192 | * @param string $name |
||
| 193 | * @param string $format |
||
| 194 | * |
||
| 195 | * @return Response |
||
| 196 | */ |
||
| 197 | public function exportAction(Request $request, $name, $format) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Builds a file name for the export from the admin class name and the datagrid filters |
||
| 214 | * |
||
| 215 | * @param string $className The name of the admin class |
||
| 216 | * @param array $filterNames The list of all datagrid filters available for this admin class |
||
| 217 | * @param array $queryFilter The filters used |
||
| 218 | * @param string $format The file format to export (e.g. xls) |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | private function getExportFilename($className, $filterNames, $queryFilter, $format) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Display the form to create a new entity |
||
| 254 | * |
||
| 255 | * @param Request $request |
||
| 256 | * @param string $name |
||
| 257 | * |
||
| 258 | * @return Response|RedirectResponse |
||
| 259 | */ |
||
| 260 | public function newAction(Request $request, $name) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Display the form to edit an entity |
||
| 296 | * |
||
| 297 | * @param Request $request |
||
| 298 | * @param string $name |
||
| 299 | * @param int $id |
||
| 300 | * |
||
| 301 | * @return Response |
||
| 302 | * |
||
| 303 | * @throws NotFoundHttpException |
||
| 304 | */ |
||
| 305 | public function editAction(Request $request, $name, $id) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param Request $request |
||
| 336 | * @param AdminInterface $adminClass |
||
| 337 | * @param object $entity |
||
| 338 | * |
||
| 339 | * @return FormInterface |
||
| 340 | */ |
||
| 341 | private function getFormForAdmin(Request $request, AdminInterface $adminClass, $entity = null) |
||
| 350 | |||
| 351 | private function processForm($form, AdminInterface $adminClass, $entity) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param Request $request |
||
| 361 | * @param FormInterface $form |
||
| 362 | * |
||
| 363 | * @return FormInterface |
||
| 364 | */ |
||
| 365 | private function addRefererField(Request $request, FormInterface $form) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param Request $request |
||
| 377 | * @param FormInterface $form |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | private function getReferer(Request $request, FormInterface $form) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Executes an action on selected table rows |
||
| 396 | * |
||
| 397 | * @param Request $request |
||
| 398 | * @param string $name |
||
| 399 | * |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | private function executeAction(Request $request, $name) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param AdminInterface $adminClass |
||
| 432 | * @param object $entity |
||
| 433 | * @param string $action |
||
| 434 | * |
||
| 435 | * @return mixed |
||
| 436 | */ |
||
| 437 | private function executeActionOnAdminOrEntity(AdminInterface $adminClass, $entity, $action) |
||
| 451 | } |
||
| 452 |