AddressRepository.php 5.4 KB

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