Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Admin 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 Admin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Admin implements AdminInterface |
||
| 28 | { |
||
| 29 | use AdminTrait; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Entities collection. |
||
| 33 | * |
||
| 34 | * @var ArrayCollection |
||
| 35 | */ |
||
| 36 | protected $entities; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var MessageHandlerInterface |
||
| 40 | */ |
||
| 41 | protected $messageHandler; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var EntityManagerInterface |
||
| 45 | */ |
||
| 46 | protected $entityManager; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var DataProviderInterface |
||
| 50 | */ |
||
| 51 | protected $dataProvider; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Admin configuration object |
||
| 55 | * |
||
| 56 | * @var AdminConfiguration |
||
| 57 | */ |
||
| 58 | protected $configuration; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Admin configured actions |
||
| 62 | * |
||
| 63 | * @var ActionInterface[] |
||
| 64 | */ |
||
| 65 | protected $actions = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Admin current action. It will be set after calling the handleRequest() |
||
| 69 | * |
||
| 70 | * @var ActionInterface |
||
| 71 | */ |
||
| 72 | protected $currentAction; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Admin name |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $name; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var EventDispatcherInterface |
||
| 83 | */ |
||
| 84 | protected $eventDispatcher; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Admin constructor. |
||
| 88 | * |
||
| 89 | * @param string $name |
||
| 90 | * @param DataProviderInterface $dataProvider |
||
| 91 | * @param AdminConfiguration $configuration |
||
| 92 | * @param MessageHandlerInterface $messageHandler |
||
| 93 | * @param EventDispatcherInterface $eventDispatcher |
||
| 94 | */ |
||
| 95 | 26 | public function __construct( |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Load entities and set current action according to request |
||
| 112 | * |
||
| 113 | * @param Request $request |
||
| 114 | * @param null $user |
||
| 115 | * @return void |
||
| 116 | * @throws AdminException |
||
| 117 | */ |
||
| 118 | 9 | public function handleRequest(Request $request, $user = null) |
|
| 119 | { |
||
| 120 | // set current action |
||
| 121 | 9 | $this->currentAction = $this->getAction($request->get('_route_params')['_action']); |
|
| 122 | // check if user is logged have required permissions to get current action |
||
| 123 | 9 | $this->checkPermissions($user); |
|
| 124 | |||
| 125 | // criteria filter request |
||
| 126 | 9 | $filter = new RequestFilter($this->currentAction->getConfiguration()->getParameter('criteria')); |
|
| 127 | 9 | $criteriaFilter = $filter->filter($request); |
|
| 128 | |||
| 129 | // pager filter request |
||
| 130 | 9 | if ($this->currentAction->getConfiguration()->getParameter('pager') == 'pagerfanta') { |
|
| 131 | 8 | $filter = new PagerfantaFilter(); |
|
| 132 | 8 | $pagerFilter = $filter->filter($request); |
|
| 133 | 8 | } else { |
|
| 134 | // empty bag |
||
| 135 | 1 | $pagerFilter = new ParameterBag(); |
|
| 136 | } |
||
| 137 | |||
| 138 | // if load strategy is none, no entity should be loaded |
||
| 139 | 9 | if ($this->currentAction->getConfiguration()->getParameter('load_strategy') == Admin::LOAD_STRATEGY_NONE) { |
|
| 140 | 1 | return; |
|
| 141 | } |
||
| 142 | |||
| 143 | // load entities according to action and request |
||
| 144 | 9 | $this->load( |
|
| 145 | 9 | $criteriaFilter->all(), |
|
| 146 | 9 | $pagerFilter->get('order', []), |
|
| 147 | 9 | $this->configuration->getParameter('max_per_page'), |
|
| 148 | 9 | $pagerFilter->get('page', 1) |
|
| 149 | 9 | ); |
|
| 150 | 8 | } |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Check if user is allowed to be here |
||
| 154 | * |
||
| 155 | * @param UserInterface|string $user |
||
| 156 | * @throws Exception |
||
| 157 | */ |
||
| 158 | 9 | public function checkPermissions($user) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Create and return a new entity. |
||
| 193 | * |
||
| 194 | * @return object |
||
| 195 | */ |
||
| 196 | 5 | public function create() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 218 | * |
||
| 219 | * @return bool true if the entity was saved without errors |
||
| 220 | */ |
||
| 221 | 2 | View Code Duplication | public function save() |
| 245 | |||
| 246 | /** |
||
| 247 | * Remove an entity with data provider |
||
| 248 | * |
||
| 249 | * @return bool true if the entity was saved without errors |
||
| 250 | */ |
||
| 251 | 2 | View Code Duplication | public function remove() |
| 275 | |||
| 276 | /** |
||
| 277 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 278 | * |
||
| 279 | * @param $actionName |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | * |
||
| 283 | * @throws Exception |
||
| 284 | */ |
||
| 285 | 15 | public function generateRouteName($actionName) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Load entities manually according to criteria. |
||
| 306 | * |
||
| 307 | * @param array $criteria |
||
| 308 | * @param array $orderBy |
||
| 309 | * @param int $limit |
||
| 310 | * @param int $offset |
||
| 311 | * @throws Exception |
||
| 312 | */ |
||
| 313 | 9 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
|
| 314 | { |
||
| 315 | 9 | $actionConfiguration = $this |
|
| 316 | 9 | ->getCurrentAction() |
|
| 317 | 9 | ->getConfiguration(); |
|
| 318 | 9 | $pager = $actionConfiguration->getParameter('pager'); |
|
| 319 | 9 | $requirePagination = $this |
|
| 320 | 9 | ->getCurrentAction() |
|
| 321 | 9 | ->isPaginationRequired(); |
|
| 322 | |||
| 323 | 9 | if ($pager == 'pagerfanta' && $requirePagination) { |
|
| 324 | // adapter to pagerfanta |
||
| 325 | 1 | $adapter = new PagerFantaAdminAdapter($this->dataProvider, $criteria, $orderBy); |
|
| 326 | // create pager |
||
| 327 | 1 | $this->pager = new Pagerfanta($adapter); |
|
| 328 | 1 | $this->pager->setMaxPerPage($limit); |
|
| 329 | 1 | $this->pager->setCurrentPage($offset); |
|
| 330 | |||
| 331 | 1 | $entities = $this |
|
| 332 | ->pager |
||
| 333 | 1 | ->getCurrentPageResults(); |
|
| 334 | 1 | } else { |
|
| 335 | // if the current action should retrieve only one entity, the offset should be zero |
||
| 336 | 8 | if ($actionConfiguration->getParameter('load_strategy') !== AdminInterface::LOAD_STRATEGY_MULTIPLE) { |
|
| 337 | 7 | $offset = 0; |
|
| 338 | 7 | } |
|
| 339 | 8 | $entities = $this |
|
| 340 | ->dataProvider |
||
| 341 | 8 | ->findBy($criteria, $orderBy, $limit, $offset); |
|
| 342 | } |
||
| 343 | 9 | if (!is_array($entities) && !($entities instanceof Collection)) { |
|
| 344 | 1 | throw new Exception('The data provider should return either a collection or an array. Got '.gettype($entities).' instead'); |
|
| 345 | } |
||
| 346 | |||
| 347 | 8 | if (is_array($entities)) { |
|
| 348 | 8 | $entities = new ArrayCollection($entities); |
|
| 349 | 8 | } |
|
| 350 | 8 | $this->entities = $entities; |
|
| 351 | 8 | } |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Return loaded entities |
||
| 355 | * |
||
| 356 | * @return Collection |
||
| 357 | */ |
||
| 358 | 2 | public function getEntities() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 365 | * |
||
| 366 | * @return mixed |
||
| 367 | * |
||
| 368 | * @throws Exception |
||
| 369 | */ |
||
| 370 | 1 | public function getUniqueEntity() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Return admin name |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | 19 | public function getName() |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Return true if current action is granted for user. |
||
| 393 | * |
||
| 394 | * @param string $actionName Le plus grand de tous les héros |
||
| 395 | * @param array $roles |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | 2 | public function isActionGranted($actionName, array $roles) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * @return ActionInterface[] |
||
| 425 | */ |
||
| 426 | 10 | public function getActions() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @return integer[] |
||
| 433 | */ |
||
| 434 | 2 | public function getActionNames() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @param $name |
||
| 441 | * @return ActionInterface |
||
| 442 | * @throws Exception |
||
| 443 | */ |
||
| 444 | 9 | public function getAction($name) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Return if an action with specified name exists form this admin. |
||
| 457 | * |
||
| 458 | * @param $name |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | 1 | public function hasAction($name) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @param ActionInterface $action |
||
| 468 | * @return void |
||
| 469 | */ |
||
| 470 | 15 | public function addAction(ActionInterface $action) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Return the current action or an exception if it is not set. |
||
| 477 | * |
||
| 478 | * @return ActionInterface |
||
| 479 | * @throws Exception |
||
| 480 | */ |
||
| 481 | 10 | public function getCurrentAction() |
|
| 482 | { |
||
| 483 | 10 | if ($this->currentAction === null) { |
|
| 484 | // current action should be defined |
||
| 485 | 1 | throw new Exception( |
|
| 486 | 'Current action is null. You should initialize it (with handleRequest method for example)' |
||
| 487 | 1 | ); |
|
| 488 | } |
||
| 489 | |||
| 490 | 9 | return $this->currentAction; |
|
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Return if the current action has been initialized and set. |
||
| 495 | * |
||
| 496 | * @return boolean |
||
| 497 | */ |
||
| 498 | 1 | public function isCurrentActionDefined() |
|
| 499 | { |
||
| 500 | 1 | return ($this->currentAction instanceof ActionInterface); |
|
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Return admin configuration object. |
||
| 505 | * |
||
| 506 | * @return AdminConfiguration |
||
| 507 | */ |
||
| 508 | 19 | public function getConfiguration() |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Return a translation key for a message according to the Admin's translation pattern. |
||
| 515 | * |
||
| 516 | * @param string $message |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | 5 | protected function generateMessageTranslationKey($message) |
|
| 527 | } |
||
| 528 |