OperationPool.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\EntityManager;
  7. use Magento\Framework\ObjectManagerInterface as ObjectManager;
  8. use Magento\Framework\EntityManager\Operation\CheckIfExists;
  9. use Magento\Framework\EntityManager\Operation\Read;
  10. use Magento\Framework\EntityManager\Operation\Create;
  11. use Magento\Framework\EntityManager\Operation\Update;
  12. use Magento\Framework\EntityManager\Operation\Delete;
  13. /**
  14. * Class OperationPool
  15. */
  16. class OperationPool
  17. {
  18. /**
  19. * @var array
  20. */
  21. private $defaultOperations = [
  22. 'checkIfExists' => CheckIfExists::class,
  23. 'read' => Read::class,
  24. 'create' => Create::class,
  25. 'update' => Update::class,
  26. 'delete' => Delete::class,
  27. ];
  28. /**
  29. * @var array
  30. */
  31. private $operations;
  32. /**
  33. * @var ObjectManager
  34. */
  35. private $objectManager;
  36. /**
  37. * OperationPool constructor.
  38. * @param ObjectManager $objectManager
  39. * @param string[] $operations
  40. */
  41. public function __construct(
  42. ObjectManager $objectManager,
  43. $operations = []
  44. ) {
  45. $this->objectManager = $objectManager;
  46. $this->operations = array_replace_recursive(
  47. ['default' => $this->defaultOperations],
  48. $operations
  49. );
  50. }
  51. /**
  52. * Returns operation by name by entity type
  53. *
  54. * @param string $entityType
  55. * @param string $operationName
  56. * @return OperationInterface
  57. */
  58. public function getOperation($entityType, $operationName)
  59. {
  60. if (!isset($this->operations[$entityType][$operationName])) {
  61. return $this->objectManager->get($this->operations['default'][$operationName]);
  62. }
  63. return $this->objectManager->get($this->operations[$entityType][$operationName]);
  64. }
  65. }