EntityManager.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\EntityManager\Operation\CheckIfExistsInterface;
  8. use Magento\Framework\EntityManager\Operation\CreateInterface;
  9. use Magento\Framework\EntityManager\Operation\DeleteInterface;
  10. use Magento\Framework\EntityManager\Operation\ReadInterface;
  11. use Magento\Framework\EntityManager\Operation\UpdateInterface;
  12. /**
  13. * It's not recommended to use EntityManager and its infrastructure for your entities persistence.
  14. * In the nearest future new Persistence Entity Manager would be released which will cover all the requirements for
  15. * persistence layer along with Query API as performance efficient APIs for Read scenarios.
  16. *
  17. * Currently, it's recommended to use Resource Model infrastructure and make a successor of
  18. * Magento\Framework\Model\ResourceModel\Db\AbstractDb class or successor of
  19. * Magento\Eav\Model\Entity\AbstractEntity if EAV attributes support needed.
  20. *
  21. * For filtering operations, it's recommended to use successor of
  22. * Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection class.
  23. */
  24. class EntityManager
  25. {
  26. /**
  27. * @var OperationPool
  28. */
  29. private $operationPool;
  30. /**
  31. * @var CallbackHandler
  32. */
  33. private $callbackHandler;
  34. /**
  35. * @param OperationPool $operationPool
  36. * @param MetadataPool $metadataPool
  37. * @param TypeResolver $typeResolver
  38. * @param CallbackHandler $callbackHandler
  39. */
  40. public function __construct(
  41. OperationPool $operationPool,
  42. MetadataPool $metadataPool,
  43. TypeResolver $typeResolver,
  44. CallbackHandler $callbackHandler
  45. ) {
  46. $this->operationPool = $operationPool;
  47. $this->metadataPool = $metadataPool;
  48. $this->typeResolver = $typeResolver;
  49. $this->callbackHandler = $callbackHandler;
  50. }
  51. /**
  52. * @param object $entity
  53. * @param string $identifier
  54. * @param array $arguments
  55. * @return mixed
  56. * @throws \LogicException
  57. */
  58. public function load($entity, $identifier, $arguments = [])
  59. {
  60. $entityType = $this->typeResolver->resolve($entity);
  61. $operation = $this->operationPool->getOperation($entityType, 'read');
  62. if (!($operation instanceof ReadInterface)) {
  63. throw new \LogicException(get_class($operation) . ' must implement ' . ReadInterface::class);
  64. }
  65. $entity = $operation->execute($entity, $identifier, $arguments);
  66. return $entity;
  67. }
  68. /**
  69. * @param object $entity
  70. * @param array $arguments
  71. * @return object
  72. * @throws \LogicException
  73. * @throws \Exception
  74. */
  75. public function save($entity, $arguments = [])
  76. {
  77. $entityType = $this->typeResolver->resolve($entity);
  78. if ($this->has($entity)) {
  79. $operation = $this->operationPool->getOperation($entityType, 'update');
  80. if (!($operation instanceof UpdateInterface)) {
  81. throw new \LogicException(get_class($operation) . ' must implement ' . UpdateInterface::class);
  82. }
  83. } else {
  84. $operation = $this->operationPool->getOperation($entityType, 'create');
  85. if (!($operation instanceof CreateInterface)) {
  86. throw new \LogicException(get_class($operation) . ' must implement ' . CreateInterface::class);
  87. }
  88. }
  89. try {
  90. $entity = $operation->execute($entity, $arguments);
  91. $this->callbackHandler->process($entityType);
  92. } catch (\Exception $e) {
  93. $this->callbackHandler->clear($entityType);
  94. throw $e;
  95. }
  96. return $entity;
  97. }
  98. /**
  99. * @param object $entity
  100. * @return bool
  101. * @throws \LogicException
  102. */
  103. public function has($entity)
  104. {
  105. $entityType = $this->typeResolver->resolve($entity);
  106. $operation = $this->operationPool->getOperation($entityType, 'checkIfExists');
  107. if (!($operation instanceof CheckIfExistsInterface)) {
  108. throw new \LogicException(get_class($operation) . ' must implement ' . CheckIfExistsInterface::class);
  109. }
  110. return $operation->execute($entity);
  111. }
  112. /**
  113. * @param object $entity
  114. * @param array $arguments
  115. * @return bool
  116. * @throws \LogicException
  117. * @throws \Exception
  118. */
  119. public function delete($entity, $arguments = [])
  120. {
  121. $entityType = $this->typeResolver->resolve($entity);
  122. $operation = $this->operationPool->getOperation($entityType, 'delete');
  123. if (!($operation instanceof DeleteInterface)) {
  124. throw new \LogicException(get_class($operation) . ' must implement ' . DeleteInterface::class);
  125. }
  126. try {
  127. $operation->execute($entity, $arguments);
  128. $this->callbackHandler->process($entityType);
  129. } catch (\Exception $e) {
  130. $this->callbackHandler->clear($entityType);
  131. throw $e;
  132. }
  133. return true;
  134. }
  135. }