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 | 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 | 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 | */ |
||
| 161 | public function checkPermissions($user) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Create and return a new entity. |
||
| 182 | * |
||
| 183 | * @return object |
||
| 184 | */ |
||
| 185 | public function create() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 202 | * |
||
| 203 | * @return bool true if the entity was saved without errors |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function save() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Remove an entity with data provider |
||
| 232 | * |
||
| 233 | * @return bool true if the entity was saved without errors |
||
| 234 | */ |
||
| 235 | View Code Duplication | public function remove() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 262 | * |
||
| 263 | * @param $actionName |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | * |
||
| 267 | * @throws Exception |
||
| 268 | */ |
||
| 269 | public function generateRouteName($actionName) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Load entities manually according to criteria. |
||
| 286 | * |
||
| 287 | * @param array $criteria |
||
| 288 | * @param array $orderBy |
||
| 289 | * @param int $limit |
||
| 290 | * @param int $offset |
||
| 291 | */ |
||
| 292 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Return loaded entities |
||
| 323 | * |
||
| 324 | * @return mixed |
||
| 325 | */ |
||
| 326 | public function getEntities() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 333 | * |
||
| 334 | * @return mixed |
||
| 335 | * |
||
| 336 | * @throws Exception |
||
| 337 | */ |
||
| 338 | public function getUniqueEntity() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Return admin name |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function getName() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Return true if current action is granted for user. |
||
| 361 | * |
||
| 362 | * @param string $actionName Le plus grand de tous les héros |
||
| 363 | * @param array $roles |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | public function isActionGranted($actionName, array $roles) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return ActionInterface[] |
||
| 393 | */ |
||
| 394 | public function getActions() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | public function getActionNames() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param $name |
||
| 409 | * @return ActionInterface |
||
| 410 | * @throws Exception |
||
| 411 | */ |
||
| 412 | public function getAction($name) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Return if an action with specified name exists form this admin. |
||
| 423 | * |
||
| 424 | * @param $name |
||
| 425 | * @return bool |
||
| 426 | */ |
||
| 427 | public function hasAction($name) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param ActionInterface $action |
||
| 434 | * @return void |
||
| 435 | */ |
||
| 436 | public function addAction(ActionInterface $action) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return ActionInterface |
||
| 443 | */ |
||
| 444 | public function getCurrentAction() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Return admin configuration object |
||
| 451 | * |
||
| 452 | * @return AdminConfiguration |
||
| 453 | */ |
||
| 454 | public function getConfiguration() |
||
| 458 | } |
||
| 459 |