CreditmemoRepository.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. use Magento\Framework\Exception\CouldNotDeleteException;
  9. use Magento\Framework\Exception\CouldNotSaveException;
  10. use Magento\Framework\Exception\InputException;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. use Magento\Sales\Api\Data\CreditmemoSearchResultInterfaceFactory as SearchResultFactory;
  14. use Magento\Sales\Model\ResourceModel\Metadata;
  15. /**
  16. * Repository class for @see \Magento\Sales\Api\Data\CreditmemoInterface
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class CreditmemoRepository implements \Magento\Sales\Api\CreditmemoRepositoryInterface
  20. {
  21. /**
  22. * @var Metadata
  23. */
  24. protected $metadata;
  25. /**
  26. * @var SearchResultFactory
  27. */
  28. protected $searchResultFactory = null;
  29. /**
  30. * \Magento\Sales\Api\Data\CreditmemoInterface[]
  31. *
  32. * @var array
  33. */
  34. protected $registry = [];
  35. /**
  36. * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
  37. */
  38. private $collectionProcessor;
  39. /**
  40. * CreditmemoRepository constructor.
  41. * @param Metadata $metadata
  42. * @param SearchResultFactory $searchResultFactory
  43. * @param CollectionProcessorInterface|null $collectionProcessor
  44. */
  45. public function __construct(
  46. Metadata $metadata,
  47. SearchResultFactory $searchResultFactory,
  48. CollectionProcessorInterface $collectionProcessor = null
  49. ) {
  50. $this->metadata = $metadata;
  51. $this->searchResultFactory = $searchResultFactory;
  52. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  53. }
  54. /**
  55. * Loads a specified credit memo.
  56. *
  57. * @param int $id The credit memo ID.
  58. * @return \Magento\Sales\Api\Data\CreditmemoInterface Credit memo interface.
  59. * @throws InputException
  60. * @throws NoSuchEntityException
  61. */
  62. public function get($id)
  63. {
  64. if (!$id) {
  65. throw new InputException(__('An ID is needed. Set the ID and try again.'));
  66. }
  67. if (!isset($this->registry[$id])) {
  68. /** @var \Magento\Sales\Api\Data\CreditmemoInterface $entity */
  69. $entity = $this->metadata->getNewInstance()->load($id);
  70. if (!$entity->getEntityId()) {
  71. throw new NoSuchEntityException(
  72. __("The entity that was requested doesn't exist. Verify the entity and try again.")
  73. );
  74. }
  75. $this->registry[$id] = $entity;
  76. }
  77. return $this->registry[$id];
  78. }
  79. /**
  80. * Create credit memo instance
  81. *
  82. * @return \Magento\Sales\Api\Data\CreditmemoInterface
  83. */
  84. public function create()
  85. {
  86. return $this->metadata->getNewInstance();
  87. }
  88. /**
  89. * Lists credit memos that match specified search criteria.
  90. *
  91. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria The search criteria.
  92. * @return \Magento\Sales\Api\Data\CreditmemoSearchResultInterface Credit memo search result interface.
  93. */
  94. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  95. {
  96. /** @var \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Collection $searchResult */
  97. $searchResult = $this->searchResultFactory->create();
  98. $this->collectionProcessor->process($searchCriteria, $searchResult);
  99. $searchResult->setSearchCriteria($searchCriteria);
  100. return $searchResult;
  101. }
  102. /**
  103. * Deletes a specified credit memo.
  104. *
  105. * @param \Magento\Sales\Api\Data\CreditmemoInterface $entity The credit memo.
  106. * @return bool
  107. * @throws CouldNotDeleteException
  108. */
  109. public function delete(\Magento\Sales\Api\Data\CreditmemoInterface $entity)
  110. {
  111. try {
  112. $this->metadata->getMapper()->delete($entity);
  113. unset($this->registry[$entity->getEntityId()]);
  114. } catch (\Exception $e) {
  115. throw new CouldNotDeleteException(__("The credit memo couldn't be deleted."), $e);
  116. }
  117. return true;
  118. }
  119. /**
  120. * Performs persist operations for a specified credit memo.
  121. *
  122. * @param \Magento\Sales\Api\Data\CreditmemoInterface $entity The credit memo.
  123. * @return \Magento\Sales\Api\Data\CreditmemoInterface Credit memo interface.
  124. * @throws CouldNotSaveException
  125. */
  126. public function save(\Magento\Sales\Api\Data\CreditmemoInterface $entity)
  127. {
  128. try {
  129. $this->metadata->getMapper()->save($entity);
  130. $this->registry[$entity->getEntityId()] = $entity;
  131. } catch (LocalizedException $e) {
  132. throw new CouldNotSaveException(__($e->getMessage()), $e);
  133. } catch (\Exception $e) {
  134. throw new CouldNotSaveException(__("The credit memo couldn't be saved."), $e);
  135. }
  136. return $this->registry[$entity->getEntityId()];
  137. }
  138. /**
  139. * Retrieve collection processor
  140. *
  141. * @deprecated 101.0.0
  142. * @return CollectionProcessorInterface
  143. */
  144. private function getCollectionProcessor()
  145. {
  146. if (!$this->collectionProcessor) {
  147. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  148. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  149. );
  150. }
  151. return $this->collectionProcessor;
  152. }
  153. }