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 | 13 | 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 | 3 | public function handleRequest(Request $request, $user = null) |
|
| 123 | { |
||
| 124 | // set current action |
||
| 125 | 3 | $this->currentAction = $this->getAction($request->get('_route_params')['_action']); |
|
| 126 | // check if user is logged have required permissions to get current action |
||
| 127 | 3 | $this->checkPermissions($user); |
|
| 128 | |||
| 129 | // criteria filter request |
||
| 130 | 3 | $filter = new RequestFilter($this->currentAction->getConfiguration()->getCriteria()); |
|
| 131 | 3 | $criteriaFilter = $filter->filter($request); |
|
| 132 | |||
| 133 | // pager filter request |
||
| 134 | 3 | if ($this->currentAction->getConfiguration()->getPager() == 'pagerfanta') { |
|
| 135 | 2 | $filter = new PagerfantaFilter(); |
|
| 136 | 2 | $pagerFilter = $filter->filter($request); |
|
| 137 | 2 | } else { |
|
| 138 | // empty bag |
||
| 139 | 2 | $pagerFilter = new ParameterBag(); |
|
| 140 | } |
||
| 141 | |||
| 142 | // if load strategy is none, no entity should be loaded |
||
| 143 | 3 | if ($this->currentAction->getConfiguration()->getLoadStrategy() == Admin::LOAD_STRATEGY_NONE) { |
|
| 144 | 1 | return; |
|
| 145 | } |
||
| 146 | |||
| 147 | // load entities according to action and request |
||
| 148 | 3 | $this->load( |
|
| 149 | 3 | $criteriaFilter->all(), |
|
| 150 | 3 | $pagerFilter->get('order', []), |
|
| 151 | 3 | $this->configuration->getMaxPerPage(), |
|
| 152 | 3 | $pagerFilter->get('page', 1) |
|
| 153 | 3 | ); |
|
| 154 | 3 | } |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Check if user is allowed to be here |
||
| 158 | * |
||
| 159 | * @param UserInterface|string $user |
||
| 160 | * @throws Exception |
||
| 161 | */ |
||
| 162 | 3 | public function checkPermissions($user) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Create and return a new entity. |
||
| 186 | * |
||
| 187 | * @return object |
||
| 188 | */ |
||
| 189 | 3 | public function create() |
|
| 190 | { |
||
| 191 | // create an entity from the data provider |
||
| 192 | 3 | $entity = $this |
|
| 193 | ->dataProvider |
||
| 194 | 3 | ->create(); |
|
| 195 | |||
| 196 | // add it to the collection |
||
| 197 | 3 | $this |
|
| 198 | ->entities |
||
| 199 | 3 | ->add($entity); |
|
| 200 | |||
| 201 | 3 | return $entity; |
|
| 202 | } |
||
| 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 | 1 | View Code Duplication | public function save() |
| 210 | { |
||
| 211 | try { |
||
| 212 | 1 | foreach ($this->entities as $entity) { |
|
| 213 | 1 | $this |
|
| 214 | ->dataProvider |
||
| 215 | 1 | ->save($entity); |
|
| 216 | 1 | } |
|
| 217 | // inform user everything went fine |
||
| 218 | 1 | $this |
|
| 219 | ->messageHandler |
||
| 220 | 1 | ->handleSuccess('lag.admin.' . $this->name . '.saved'); |
|
| 221 | 1 | $success = true; |
|
| 222 | 1 | } catch (Exception $e) { |
|
| 223 | 1 | $this |
|
| 224 | ->messageHandler |
||
| 225 | 1 | ->handleError( |
|
| 226 | 1 | 'lag.admin.saved_errors', |
|
| 227 | 1 | "An error has occurred while saving an entity : {$e->getMessage()}, stackTrace: {$e->getTraceAsString()}" |
|
| 228 | 1 | ); |
|
| 229 | 1 | $success = false; |
|
| 230 | } |
||
| 231 | 1 | return $success; |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Remove an entity with data provider |
||
| 236 | * |
||
| 237 | * @return bool true if the entity was saved without errors |
||
| 238 | */ |
||
| 239 | 1 | View Code Duplication | public function remove() |
| 240 | { |
||
| 241 | try { |
||
| 242 | 1 | foreach ($this->entities as $entity) { |
|
| 243 | 1 | $this |
|
| 244 | ->dataProvider |
||
| 245 | 1 | ->remove($entity); |
|
| 246 | 1 | } |
|
| 247 | // inform user everything went fine |
||
| 248 | 1 | $this |
|
| 249 | ->messageHandler |
||
| 250 | 1 | ->handleSuccess('lag.admin.' . $this->name . '.deleted'); |
|
| 251 | 1 | $success = true; |
|
| 252 | 1 | } catch (Exception $e) { |
|
| 253 | 1 | $this |
|
| 254 | ->messageHandler |
||
| 255 | 1 | ->handleError( |
|
| 256 | 1 | 'lag.admin.deleted_errors', |
|
| 257 | 1 | "An error has occurred while deleting an entity : {$e->getMessage()}, stackTrace: {$e->getTraceAsString()} " |
|
| 258 | 1 | ); |
|
| 259 | 1 | $success = false; |
|
| 260 | } |
||
| 261 | 1 | return $success; |
|
| 262 | } |
||
| 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 | 1 | public function generateRouteName($actionName) |
|
| 274 | { |
||
| 275 | 1 | if (!array_key_exists($actionName, $this->getConfiguration()->getActions())) { |
|
| 276 | 1 | $message = 'Invalid action name %s for admin %s (available action are: %s)'; |
|
| 277 | 1 | throw new Exception(sprintf($message, $actionName, $this->getName(), implode(', ', $this->getActionNames()))); |
|
| 278 | } |
||
| 279 | // get routing name pattern |
||
| 280 | 1 | $routingPattern = $this->getConfiguration()->getRoutingNamePattern(); |
|
| 281 | // replace admin and action name in pattern |
||
| 282 | 1 | $routeName = str_replace('{admin}', Container::underscore($this->getName()), $routingPattern); |
|
| 283 | 1 | $routeName = str_replace('{action}', $actionName, $routeName); |
|
| 284 | |||
| 285 | 1 | return $routeName; |
|
| 286 | } |
||
| 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 | 3 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
|
| 297 | { |
||
| 298 | 3 | $pager = $this |
|
| 299 | 3 | ->getCurrentAction() |
|
| 300 | 3 | ->getConfiguration() |
|
| 301 | 3 | ->getPager(); |
|
| 302 | |||
| 303 | 3 | if ($pager == 'pagerfanta') { |
|
| 304 | // adapter to pager fanta |
||
| 305 | 2 | $adapter = new PagerFantaAdminAdapter($this->dataProvider, $criteria, $orderBy); |
|
| 306 | // create pager |
||
| 307 | 2 | $this->pager = new Pagerfanta($adapter); |
|
| 308 | 2 | $this->pager->setMaxPerPage($limit); |
|
| 309 | 2 | $this->pager->setCurrentPage($offset); |
|
| 310 | |||
| 311 | 2 | $entities = $this |
|
| 312 | ->pager |
||
| 313 | 2 | ->getCurrentPageResults(); |
|
| 314 | 2 | } else { |
|
| 315 | 2 | $entities = $this |
|
| 316 | ->dataProvider |
||
| 317 | 2 | ->findBy($criteria, $orderBy, $limit, $offset); |
|
| 318 | } |
||
| 319 | 3 | if (is_array($entities)) { |
|
| 320 | 1 | $entities = new ArrayCollection($entities); |
|
| 321 | 1 | } |
|
| 322 | 3 | $this->entities = $entities; |
|
| 323 | 3 | } |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Return loaded entities |
||
| 327 | * |
||
| 328 | * @return mixed |
||
| 329 | */ |
||
| 330 | 1 | public function getEntities() |
|
| 331 | { |
||
| 332 | 1 | return $this->entities; |
|
| 333 | } |
||
| 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 | 9 | 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 | 4 | public function getActions() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | 1 | public function getActionNames() |
|
| 407 | { |
||
| 408 | 1 | return array_keys($this->actions); |
|
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param $name |
||
| 413 | * @return ActionInterface |
||
| 414 | * @throws Exception |
||
| 415 | */ |
||
| 416 | 3 | 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 | 9 | public function addAction(ActionInterface $action) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @return ActionInterface |
||
| 449 | */ |
||
| 450 | 3 | public function getCurrentAction() |
|
| 451 | { |
||
| 452 | 3 | if ($this->currentAction === null) { |
|
| 453 | // current action should be defined |
||
| 454 | throw new Exception( |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Return admin configuration object |
||
| 464 | * |
||
| 465 | * @return AdminConfiguration |
||
| 466 | */ |
||
| 467 | 9 | public function getConfiguration() |
|
| 471 | } |
||
| 472 |