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 |
||
| 25 | class Admin implements AdminInterface |
||
| 26 | { |
||
| 27 | use AdminTrait; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Entities collection. |
||
| 31 | * |
||
| 32 | * @var ArrayCollection |
||
| 33 | */ |
||
| 34 | protected $entities; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var MessageHandlerInterface |
||
| 38 | */ |
||
| 39 | protected $messageHandler; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var EntityManagerInterface |
||
| 43 | */ |
||
| 44 | protected $entityManager; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var DataProviderInterface |
||
| 48 | */ |
||
| 49 | protected $dataProvider; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Admin configuration object |
||
| 53 | * |
||
| 54 | * @var AdminConfiguration |
||
| 55 | */ |
||
| 56 | protected $configuration; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Admin configured actions |
||
| 60 | * |
||
| 61 | * @var ActionInterface[] |
||
| 62 | */ |
||
| 63 | protected $actions = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Admin current action. It will be set after calling the handleRequest() |
||
| 67 | * |
||
| 68 | * @var ActionInterface |
||
| 69 | */ |
||
| 70 | protected $currentAction; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Admin name |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $name; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Admin constructor. |
||
| 81 | * |
||
| 82 | * @param string $name |
||
| 83 | * @param DataProviderInterface $dataProvider |
||
| 84 | * @param AdminConfiguration $configuration |
||
| 85 | * @param MessageHandlerInterface $messageHandler |
||
| 86 | */ |
||
| 87 | 16 | public function __construct( |
|
| 88 | $name, |
||
| 89 | DataProviderInterface $dataProvider, |
||
| 90 | AdminConfiguration $configuration, |
||
| 91 | MessageHandlerInterface $messageHandler |
||
| 92 | ) { |
||
| 93 | 16 | $this->name = $name; |
|
| 94 | 16 | $this->dataProvider = $dataProvider; |
|
| 95 | 16 | $this->configuration = $configuration; |
|
| 96 | 16 | $this->messageHandler = $messageHandler; |
|
| 97 | 16 | $this->entities = new ArrayCollection(); |
|
| 98 | 16 | } |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Load entities and set current action according to request |
||
| 102 | * |
||
| 103 | * @param Request $request |
||
| 104 | * @param null $user |
||
| 105 | * @return void |
||
| 106 | * @throws AdminException |
||
| 107 | */ |
||
| 108 | 5 | public function handleRequest(Request $request, $user = null) |
|
| 109 | { |
||
| 110 | // set current action |
||
| 111 | 5 | $this->currentAction = $this->getAction($request->get('_route_params')['_action']); |
|
| 112 | // check if user is logged have required permissions to get current action |
||
| 113 | 5 | $this->checkPermissions($user); |
|
| 114 | |||
| 115 | // criteria filter request |
||
| 116 | 5 | $filter = new RequestFilter($this->currentAction->getConfiguration()->getParameter('criteria')); |
|
| 117 | 5 | $criteriaFilter = $filter->filter($request); |
|
| 118 | |||
| 119 | // pager filter request |
||
| 120 | 5 | if ($this->currentAction->getConfiguration()->getParameter('pager') == 'pagerfanta') { |
|
| 121 | 5 | $filter = new PagerfantaFilter(); |
|
| 122 | 5 | $pagerFilter = $filter->filter($request); |
|
| 123 | 5 | } else { |
|
| 124 | // empty bag |
||
| 125 | $pagerFilter = new ParameterBag(); |
||
| 126 | } |
||
| 127 | |||
| 128 | // if load strategy is none, no entity should be loaded |
||
| 129 | 5 | if ($this->currentAction->getConfiguration()->getParameter('load_strategy') == Admin::LOAD_STRATEGY_NONE) { |
|
| 130 | 1 | return; |
|
| 131 | } |
||
| 132 | |||
| 133 | // load entities according to action and request |
||
| 134 | 5 | $this->load( |
|
| 135 | 5 | $criteriaFilter->all(), |
|
| 136 | 5 | $pagerFilter->get('order', []), |
|
| 137 | 5 | $this->configuration->getParameter('max_per_page'), |
|
| 138 | 5 | $pagerFilter->get('page', 1) |
|
| 139 | 5 | ); |
|
| 140 | 5 | } |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Check if user is allowed to be here |
||
| 144 | * |
||
| 145 | * @param UserInterface|string $user |
||
| 146 | * @throws Exception |
||
| 147 | */ |
||
| 148 | 5 | public function checkPermissions($user) |
|
| 149 | { |
||
| 150 | 5 | if (!($user instanceof UserInterface)) { |
|
| 151 | 5 | return; |
|
| 152 | } |
||
| 153 | 1 | if ($this->currentAction === null) { |
|
| 154 | 1 | throw new Exception('Current action should be set before checking the permissions'); |
|
| 155 | } |
||
| 156 | 1 | $roles = $user->getRoles(); |
|
| 157 | 1 | $actionName = $this |
|
| 158 | 1 | ->getCurrentAction() |
|
| 159 | 1 | ->getName(); |
|
| 160 | |||
| 161 | 1 | if (!$this->isActionGranted($actionName, $roles)) { |
|
| 162 | 1 | $message = sprintf('User with roles %s not allowed for action "%s"', |
|
| 163 | 1 | implode(', ', $roles), |
|
| 164 | $actionName |
||
| 165 | 1 | ); |
|
| 166 | 1 | throw new NotFoundHttpException($message); |
|
| 167 | } |
||
| 168 | 1 | } |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Create and return a new entity. |
||
| 172 | * |
||
| 173 | * @return object |
||
| 174 | */ |
||
| 175 | 3 | public function create() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 192 | * |
||
| 193 | * @return bool true if the entity was saved without errors |
||
| 194 | */ |
||
| 195 | 1 | View Code Duplication | public function save() |
| 219 | |||
| 220 | /** |
||
| 221 | * Remove an entity with data provider |
||
| 222 | * |
||
| 223 | * @return bool true if the entity was saved without errors |
||
| 224 | */ |
||
| 225 | 1 | View Code Duplication | public function remove() |
| 249 | |||
| 250 | /** |
||
| 251 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 252 | * |
||
| 253 | * @param $actionName |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | * |
||
| 257 | * @throws Exception |
||
| 258 | */ |
||
| 259 | 8 | public function generateRouteName($actionName) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Load entities manually according to criteria. |
||
| 280 | * |
||
| 281 | * @param array $criteria |
||
| 282 | * @param array $orderBy |
||
| 283 | * @param int $limit |
||
| 284 | * @param int $offset |
||
| 285 | * @throws Exception |
||
| 286 | */ |
||
| 287 | 5 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Return loaded entities |
||
| 322 | * |
||
| 323 | * @return Collection |
||
| 324 | */ |
||
| 325 | 2 | public function getEntities() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 332 | * |
||
| 333 | * @return mixed |
||
| 334 | * |
||
| 335 | * @throws Exception |
||
| 336 | */ |
||
| 337 | 1 | public function getUniqueEntity() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Return admin name |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | 13 | public function getName() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Return true if current action is granted for user. |
||
| 360 | * |
||
| 361 | * @param string $actionName Le plus grand de tous les héros |
||
| 362 | * @param array $roles |
||
| 363 | * |
||
| 364 | * @return bool |
||
| 365 | */ |
||
| 366 | 1 | public function isActionGranted($actionName, array $roles) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * @return ActionInterface[] |
||
| 392 | */ |
||
| 393 | 6 | public function getActions() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @return integer[] |
||
| 400 | */ |
||
| 401 | 2 | public function getActionNames() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @param $name |
||
| 408 | * @return ActionInterface |
||
| 409 | * @throws Exception |
||
| 410 | */ |
||
| 411 | 5 | public function getAction($name) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Return if an action with specified name exists form this admin. |
||
| 424 | * |
||
| 425 | * @param $name |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | public function hasAction($name) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param ActionInterface $action |
||
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | 12 | public function addAction(ActionInterface $action) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * @return ActionInterface |
||
| 444 | * @throws Exception |
||
| 445 | */ |
||
| 446 | 5 | public function getCurrentAction() |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Return admin configuration object |
||
| 460 | * |
||
| 461 | * @return AdminConfiguration |
||
| 462 | */ |
||
| 463 | 13 | public function getConfiguration() |
|
| 467 | } |
||
| 468 |