ShipmentRepository.php 5.3 KB

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