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 |
||
| 26 | class Admin implements AdminInterface |
||
| 27 | { |
||
| 28 | use AdminTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Entities collection. |
||
| 32 | * |
||
| 33 | * @var ArrayCollection |
||
| 34 | */ |
||
| 35 | protected $entities; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var MessageHandlerInterface |
||
| 39 | */ |
||
| 40 | protected $messageHandler; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var EntityManagerInterface |
||
| 44 | */ |
||
| 45 | protected $entityManager; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var DataProviderInterface |
||
| 49 | */ |
||
| 50 | protected $dataProvider; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Admin configuration object |
||
| 54 | * |
||
| 55 | * @var AdminConfiguration |
||
| 56 | */ |
||
| 57 | protected $configuration; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Admin configured actions |
||
| 61 | * |
||
| 62 | * @var ActionInterface[] |
||
| 63 | */ |
||
| 64 | protected $actions = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Admin current action. It will be set after calling the handleRequest() |
||
| 68 | * |
||
| 69 | * @var ActionInterface |
||
| 70 | */ |
||
| 71 | protected $currentAction; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Admin name |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $name; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Admin constructor. |
||
| 82 | * |
||
| 83 | * @param string $name |
||
| 84 | * @param DataProviderInterface $dataProvider |
||
| 85 | * @param AdminConfiguration $configuration |
||
| 86 | * @param MessageHandlerInterface $messageHandler |
||
| 87 | */ |
||
| 88 | 17 | public function __construct( |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Load entities and set current action according to request |
||
| 103 | * |
||
| 104 | * @param Request $request |
||
| 105 | * @param null $user |
||
| 106 | * @return void |
||
| 107 | * @throws AdminException |
||
| 108 | */ |
||
| 109 | 5 | public function handleRequest(Request $request, $user = null) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Check if user is allowed to be here |
||
| 145 | * |
||
| 146 | * @param UserInterface|string $user |
||
| 147 | * @throws Exception |
||
| 148 | */ |
||
| 149 | 5 | public function checkPermissions($user) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Create and return a new entity. |
||
| 184 | * |
||
| 185 | * @return object |
||
| 186 | */ |
||
| 187 | 3 | public function create() |
|
| 188 | { |
||
| 189 | // create an entity from the data provider |
||
| 190 | 3 | $entity = $this |
|
| 191 | ->dataProvider |
||
| 192 | 3 | ->create(); |
|
| 193 | |||
| 194 | // add it to the collection |
||
| 195 | 3 | $this |
|
| 196 | ->entities |
||
| 197 | 3 | ->add($entity); |
|
| 198 | |||
| 199 | 3 | // inform the user that the entity is created |
|
| 200 | $this |
||
| 201 | ->messageHandler |
||
| 202 | ->handleSuccess($this->generateMessageTranslationKey('created')); |
||
| 203 | |||
| 204 | return $entity; |
||
| 205 | } |
||
| 206 | |||
| 207 | 1 | /** |
|
| 208 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 209 | * |
||
| 210 | 1 | * @return bool true if the entity was saved without errors |
|
| 211 | 1 | */ |
|
| 212 | View Code Duplication | public function save() |
|
| 213 | 1 | { |
|
| 214 | 1 | try { |
|
| 215 | foreach ($this->entities as $entity) { |
||
| 216 | 1 | $this |
|
| 217 | ->dataProvider |
||
| 218 | 1 | ->save($entity); |
|
| 219 | 1 | } |
|
| 220 | 1 | // inform the user that the entity is saved |
|
| 221 | 1 | $this |
|
| 222 | ->messageHandler |
||
| 223 | 1 | ->handleSuccess($this->generateMessageTranslationKey('saved')); |
|
| 224 | 1 | $success = true; |
|
| 225 | 1 | } catch (Exception $e) { |
|
| 226 | 1 | $this |
|
| 227 | 1 | ->messageHandler |
|
| 228 | ->handleError( |
||
| 229 | 1 | $this->generateMessageTranslationKey('lag.admin.saved_errors'), |
|
| 230 | "An error has occurred while saving an entity : {$e->getMessage()}, stackTrace: {$e->getTraceAsString()}" |
||
| 231 | ); |
||
| 232 | $success = false; |
||
| 233 | } |
||
| 234 | return $success; |
||
| 235 | } |
||
| 236 | |||
| 237 | 1 | /** |
|
| 238 | * Remove an entity with data provider |
||
| 239 | * |
||
| 240 | 1 | * @return bool true if the entity was saved without errors |
|
| 241 | 1 | */ |
|
| 242 | View Code Duplication | public function remove() |
|
| 243 | 1 | { |
|
| 244 | 1 | try { |
|
| 245 | foreach ($this->entities as $entity) { |
||
| 246 | 1 | $this |
|
| 247 | ->dataProvider |
||
| 248 | 1 | ->remove($entity); |
|
| 249 | 1 | } |
|
| 250 | 1 | // inform the user that the entity is removed |
|
| 251 | 1 | $this |
|
| 252 | ->messageHandler |
||
| 253 | 1 | ->handleSuccess($this->generateMessageTranslationKey('deleted')); |
|
| 254 | 1 | $success = true; |
|
| 255 | 1 | } catch (Exception $e) { |
|
| 256 | 1 | $this |
|
| 257 | 1 | ->messageHandler |
|
| 258 | ->handleError( |
||
| 259 | 1 | $this->generateMessageTranslationKey('lag.admin.deleted_errors'), |
|
| 260 | "An error has occurred while deleting an entity : {$e->getMessage()}, stackTrace: {$e->getTraceAsString()} " |
||
| 261 | ); |
||
| 262 | $success = false; |
||
| 263 | } |
||
| 264 | return $success; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 269 | * |
||
| 270 | * @param $actionName |
||
| 271 | 8 | * |
|
| 272 | * @return string |
||
| 273 | 8 | * |
|
| 274 | 2 | * @throws Exception |
|
| 275 | 2 | */ |
|
| 276 | 2 | public function generateRouteName($actionName) |
|
| 277 | 2 | { |
|
| 278 | 2 | if (!array_key_exists($actionName, $this->getConfiguration()->getParameter('actions'))) { |
|
| 279 | 2 | throw new Exception( |
|
| 280 | sprintf('Invalid action name %s for admin %s (available action are: %s)', |
||
| 281 | $actionName, |
||
| 282 | 8 | $this->getName(), |
|
| 283 | implode(', ', $this->getActionNames())) |
||
| 284 | 8 | ); |
|
| 285 | 8 | } |
|
| 286 | // get routing name pattern |
||
| 287 | 8 | $routingPattern = $this->getConfiguration()->getParameter('routing_name_pattern'); |
|
| 288 | // replace admin and action name in pattern |
||
| 289 | $routeName = str_replace('{admin}', Container::underscore($this->getName()), $routingPattern); |
||
| 290 | $routeName = str_replace('{action}', $actionName, $routeName); |
||
| 291 | |||
| 292 | return $routeName; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Load entities manually according to criteria. |
||
| 297 | * |
||
| 298 | * @param array $criteria |
||
| 299 | 5 | * @param array $orderBy |
|
| 300 | * @param int $limit |
||
| 301 | 5 | * @param int $offset |
|
| 302 | 5 | * @throws Exception |
|
| 303 | 5 | */ |
|
| 304 | 5 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
|
| 305 | { |
||
| 306 | 5 | $actionConfiguration = $this |
|
| 307 | ->getCurrentAction() |
||
| 308 | 5 | ->getConfiguration(); |
|
| 309 | $pager = $actionConfiguration->getParameter('pager'); |
||
| 310 | 5 | $requirePagination = $this |
|
| 311 | 5 | ->getCurrentAction() |
|
| 312 | 5 | ->requirePagination(); |
|
| 313 | |||
| 314 | 5 | if ($pager == 'pagerfanta' && $requirePagination) { |
|
| 315 | // adapter to pagerfanta |
||
| 316 | 5 | $adapter = new PagerFantaAdminAdapter($this->dataProvider, $criteria, $orderBy); |
|
| 317 | 5 | // create pager |
|
| 318 | $this->pager = new Pagerfanta($adapter); |
||
| 319 | $this->pager->setMaxPerPage($limit); |
||
| 320 | $this->pager->setCurrentPage($offset); |
||
| 321 | |||
| 322 | 5 | $entities = $this |
|
| 323 | 1 | ->pager |
|
| 324 | ->getCurrentPageResults(); |
||
| 325 | } else { |
||
| 326 | 5 | // if the current action should retrieve only one entity, the offset should be zero |
|
| 327 | 5 | if ($actionConfiguration->getParameter('load_strategy') !== AdminInterface::LOAD_STRATEGY_MULTIPLE) { |
|
| 328 | 5 | $offset = 0; |
|
| 329 | 5 | } |
|
| 330 | 5 | $entities = $this |
|
| 331 | ->dataProvider |
||
| 332 | ->findBy($criteria, $orderBy, $limit, $offset); |
||
| 333 | } |
||
| 334 | if (!is_array($entities) && !($entities instanceof Collection)) { |
||
| 335 | throw new Exception('The data provider should return either a collection or an array. Got '.gettype($entities).' instead'); |
||
| 336 | } |
||
| 337 | 2 | ||
| 338 | if (is_array($entities)) { |
||
| 339 | 2 | $entities = new ArrayCollection($entities); |
|
| 340 | } |
||
| 341 | $this->entities = $entities; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Return loaded entities |
||
| 346 | * |
||
| 347 | * @return Collection |
||
| 348 | */ |
||
| 349 | 1 | public function getEntities() |
|
| 350 | { |
||
| 351 | 1 | return $this->entities; |
|
| 352 | 1 | } |
|
| 353 | |||
| 354 | 1 | /** |
|
| 355 | 1 | * Return entity for current admin. If entity does not exist, it throws an exception. |
|
| 356 | * |
||
| 357 | 1 | * @return mixed |
|
| 358 | * |
||
| 359 | * @throws Exception |
||
| 360 | */ |
||
| 361 | public function getUniqueEntity() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Return admin name |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function getName() |
||
| 378 | 1 | { |
|
| 379 | return $this->name; |
||
| 380 | 1 | } |
|
| 381 | |||
| 382 | /** |
||
| 383 | 1 | * Return true if current action is granted for user. |
|
| 384 | 1 | * |
|
| 385 | * @param string $actionName Le plus grand de tous les héros |
||
| 386 | 1 | * @param array $roles |
|
| 387 | * |
||
| 388 | 1 | * @return bool |
|
| 389 | */ |
||
| 390 | 1 | public function isActionGranted($actionName, array $roles) |
|
| 391 | { |
||
| 392 | $isGranted = array_key_exists($actionName, $this->actions); |
||
| 393 | 1 | ||
| 394 | 1 | // if action exists |
|
| 395 | 1 | if ($isGranted) { |
|
| 396 | 1 | $isGranted = false; |
|
| 397 | 1 | /** @var ActionInterface $action */ |
|
| 398 | $action = $this->actions[$actionName]; |
||
| 399 | 1 | // checking roles permissions |
|
| 400 | foreach ($roles as $role) { |
||
| 401 | |||
| 402 | if ($role instanceof Role) { |
||
| 403 | $role = $role->getRole(); |
||
| 404 | } |
||
| 405 | 6 | if (in_array($role, $action->getPermissions())) { |
|
| 406 | $isGranted = true; |
||
| 407 | 6 | } |
|
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | return $isGranted; |
||
| 412 | } |
||
| 413 | 2 | ||
| 414 | /** |
||
| 415 | 2 | * @return ActionInterface[] |
|
| 416 | */ |
||
| 417 | public function getActions() |
||
| 418 | { |
||
| 419 | return $this->actions; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | 5 | * @return integer[] |
|
| 424 | */ |
||
| 425 | 5 | public function getActionNames() |
|
| 426 | 1 | { |
|
| 427 | 1 | return array_keys($this->actions); |
|
| 428 | 1 | } |
|
| 429 | |||
| 430 | /** |
||
| 431 | 5 | * @param $name |
|
| 432 | * @return ActionInterface |
||
| 433 | * @throws Exception |
||
| 434 | */ |
||
| 435 | public function getAction($name) |
||
| 436 | { |
||
| 437 | if (!array_key_exists($name, $this->getActions())) { |
||
| 438 | throw new Exception( |
||
| 439 | "Invalid action name \"{$name}\" for admin '{$this->getName()}'. Check your configuration" |
||
| 440 | ); |
||
| 441 | } |
||
| 442 | |||
| 443 | return $this->actions[$name]; |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Return if an action with specified name exists form this admin. |
||
| 448 | * |
||
| 449 | 12 | * @param $name |
|
| 450 | * @return bool |
||
| 451 | 12 | */ |
|
| 452 | 12 | public function hasAction($name) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @param ActionInterface $action |
||
| 459 | * @return void |
||
| 460 | 5 | */ |
|
| 461 | public function addAction(ActionInterface $action) |
||
| 462 | 5 | { |
|
| 463 | $this->actions[$action->getName()] = $action; |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Return the current action or an exception if it is not set. |
||
| 468 | * |
||
| 469 | 5 | * @return ActionInterface |
|
| 470 | * @throws Exception |
||
| 471 | */ |
||
| 472 | public function getCurrentAction() |
||
| 473 | { |
||
| 474 | if ($this->currentAction === null) { |
||
| 475 | // current action should be defined |
||
| 476 | throw new Exception( |
||
| 477 | 'Current action is null. You should initialize it (with handleRequest method for example)' |
||
| 478 | ); |
||
| 479 | } |
||
| 480 | |||
| 481 | return $this->currentAction; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Return if the current action has been initialized and set. |
||
| 486 | * |
||
| 487 | 14 | * @return boolean |
|
| 488 | */ |
||
| 489 | 14 | public function isCurrentActionDefined() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Return admin configuration object. |
||
| 496 | * |
||
| 497 | * @return AdminConfiguration |
||
| 498 | */ |
||
| 499 | public function getConfiguration() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Return an translation key for a message according to the Admin's translation pattern. |
||
| 506 | * |
||
| 507 | * @param string $message |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | protected function generateMessageTranslationKey($message) |
||
| 518 | } |
||
| 519 |