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:
| 1 | <?php |
||
| 24 | class Admin implements AdminInterface |
||
| 25 | { |
||
| 26 | use AdminTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Do not load entities on handleRequest (for create method for example) |
||
| 30 | */ |
||
| 31 | const LOAD_STRATEGY_NONE = 'strategy_none'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Load one entity on handleRequest (edit method for example) |
||
| 35 | */ |
||
| 36 | const LOAD_STRATEGY_UNIQUE = 'strategy_unique'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Load multiple entities on handleRequest (list method for example) |
||
| 40 | */ |
||
| 41 | const LOAD_STRATEGY_MULTIPLE = 'strategy_multiple'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Entities collection. |
||
| 45 | * |
||
| 46 | * @var ArrayCollection |
||
| 47 | */ |
||
| 48 | protected $entities; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var MessageHandlerInterface |
||
| 52 | */ |
||
| 53 | protected $messageHandler; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var EntityManagerInterface |
||
| 57 | */ |
||
| 58 | protected $entityManager; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var DataProviderInterface |
||
| 62 | */ |
||
| 63 | protected $dataProvider; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Admin configuration object |
||
| 67 | * |
||
| 68 | * @var AdminConfiguration |
||
| 69 | */ |
||
| 70 | protected $configuration; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Admin configured actions |
||
| 74 | * |
||
| 75 | * @var ActionInterface[] |
||
| 76 | */ |
||
| 77 | protected $actions = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Admin current action. It will be set after calling the handleRequest() |
||
| 81 | * |
||
| 82 | * @var ActionInterface |
||
| 83 | */ |
||
| 84 | protected $currentAction; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Admin name |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $name; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Admin constructor. |
||
| 95 | * |
||
| 96 | * @param string $name |
||
| 97 | * @param DataProviderInterface $dataProvider |
||
| 98 | * @param AdminConfiguration $configuration |
||
| 99 | * @param MessageHandlerInterface $messageHandler |
||
| 100 | */ |
||
| 101 | 8 | public function __construct( |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Load entities and set current action according to request |
||
| 116 | * |
||
| 117 | * @param Request $request |
||
| 118 | * @param null $user |
||
| 119 | * @return void |
||
| 120 | * @throws AdminException |
||
| 121 | */ |
||
| 122 | 2 | public function handleRequest(Request $request, $user = null) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Check if user is allowed to be here |
||
| 158 | * |
||
| 159 | * @param UserInterface|string $user |
||
| 160 | * @throws Exception |
||
| 161 | */ |
||
| 162 | 2 | public function checkPermissions($user) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Create and return a new entity. |
||
| 186 | * |
||
| 187 | * @return object |
||
| 188 | */ |
||
| 189 | public function create() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 206 | * |
||
| 207 | * @return bool true if the entity was saved without errors |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function save() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Remove an entity with data provider |
||
| 236 | * |
||
| 237 | * @return bool true if the entity was saved without errors |
||
| 238 | */ |
||
| 239 | View Code Duplication | public function remove() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 266 | * |
||
| 267 | * @param $actionName |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | * |
||
| 271 | * @throws Exception |
||
| 272 | */ |
||
| 273 | public function generateRouteName($actionName) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Load entities manually according to criteria. |
||
| 290 | * |
||
| 291 | * @param array $criteria |
||
| 292 | * @param array $orderBy |
||
| 293 | * @param int $limit |
||
| 294 | * @param int $offset |
||
| 295 | */ |
||
| 296 | 2 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Return loaded entities |
||
| 327 | * |
||
| 328 | * @return mixed |
||
| 329 | */ |
||
| 330 | public function getEntities() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 337 | * |
||
| 338 | * @return mixed |
||
| 339 | * |
||
| 340 | * @throws Exception |
||
| 341 | */ |
||
| 342 | public function getUniqueEntity() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Return admin name |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | 8 | public function getName() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Return true if current action is granted for user. |
||
| 365 | * |
||
| 366 | * @param string $actionName Le plus grand de tous les héros |
||
| 367 | * @param array $roles |
||
| 368 | * |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | 1 | public function isActionGranted($actionName, array $roles) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @return ActionInterface[] |
||
| 397 | */ |
||
| 398 | 3 | public function getActions() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | public function getActionNames() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param $name |
||
| 413 | * @return ActionInterface |
||
| 414 | * @throws Exception |
||
| 415 | */ |
||
| 416 | 2 | public function getAction($name) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Return if an action with specified name exists form this admin. |
||
| 429 | * |
||
| 430 | * @param $name |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | public function hasAction($name) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param ActionInterface $action |
||
| 440 | * @return void |
||
| 441 | */ |
||
| 442 | 8 | public function addAction(ActionInterface $action) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @return ActionInterface |
||
| 449 | */ |
||
| 450 | 2 | public function getCurrentAction() |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Return admin configuration object |
||
| 457 | * |
||
| 458 | * @return AdminConfiguration |
||
| 459 | */ |
||
| 460 | 8 | public function getConfiguration() |
|
| 464 | } |
||
| 465 |