ShipmentReferenceRepository.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\ResourceModel\Shipment;
  6. use Magento\Framework\Api\FilterBuilder;
  7. use Magento\Framework\Api\Search\SearchCriteriaBuilderFactory;
  8. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  9. use Magento\Framework\Api\SearchCriteriaInterface;
  10. use Magento\Framework\Api\SortOrder;
  11. use Magento\Framework\Exception\CouldNotSaveException;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. use Magento\Sales\Api\ShipmentTrackRepositoryInterface;
  14. use Temando\Shipping\Api\Data\Shipment\ShipmentReferenceInterface;
  15. use Temando\Shipping\Api\Data\Shipment\ShipmentReferenceInterfaceFactory;
  16. use Temando\Shipping\Model\ResourceModel\Repository\ShipmentReferenceRepositoryInterface;
  17. use Temando\Shipping\Model\ResourceModel\Shipment\ShipmentReference as ShipmentReferenceResource;
  18. /**
  19. * Temando Shipment Repository
  20. *
  21. * @package Temando\Shipping\Model
  22. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  23. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * @link https://www.temando.com/
  25. */
  26. class ShipmentReferenceRepository implements ShipmentReferenceRepositoryInterface
  27. {
  28. /**
  29. * @var ShipmentReferenceResource
  30. */
  31. private $resource;
  32. /**
  33. * @var ShipmentReferenceInterfaceFactory
  34. */
  35. private $shipmentReferenceFactory;
  36. /**
  37. * @var ShipmentReferenceCollectionFactory
  38. */
  39. private $shipmentReferenceCollectionFactory;
  40. /**
  41. * @var CollectionProcessorInterface
  42. */
  43. private $collectionProcessor;
  44. /**
  45. * @var SearchCriteriaBuilderFactory
  46. */
  47. private $searchCriteriaBuilderFactory;
  48. /**
  49. * @var FilterBuilder
  50. */
  51. private $filterBuilder;
  52. /**
  53. * @var ShipmentTrackRepositoryInterface
  54. */
  55. private $shipmentTrackRepository;
  56. /**
  57. * ShipmentReferenceRepository constructor.
  58. * @param ShipmentReference $resource
  59. * @param ShipmentReferenceInterfaceFactory $shipmentReferenceFactory
  60. * @param ShipmentReferenceCollectionFactory $shipmentReferenceCollectionFactory
  61. * @param CollectionProcessorInterface $collectionProcessor
  62. * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
  63. * @param FilterBuilder $filterBuilder
  64. * @param ShipmentTrackRepositoryInterface $shipmentTrackRepository
  65. */
  66. public function __construct(
  67. ShipmentReferenceResource $resource,
  68. ShipmentReferenceInterfaceFactory $shipmentReferenceFactory,
  69. ShipmentReferenceCollectionFactory $shipmentReferenceCollectionFactory,
  70. CollectionProcessorInterface $collectionProcessor,
  71. SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
  72. FilterBuilder $filterBuilder,
  73. ShipmentTrackRepositoryInterface $shipmentTrackRepository
  74. ) {
  75. $this->resource = $resource;
  76. $this->shipmentReferenceFactory = $shipmentReferenceFactory;
  77. $this->shipmentReferenceCollectionFactory = $shipmentReferenceCollectionFactory;
  78. $this->collectionProcessor = $collectionProcessor;
  79. $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
  80. $this->filterBuilder = $filterBuilder;
  81. $this->shipmentTrackRepository = $shipmentTrackRepository;
  82. }
  83. /**
  84. * Load local track info.
  85. *
  86. * @param string $carrierCode
  87. * @param string $trackingNumber
  88. * @return \Magento\Sales\Api\Data\ShipmentTrackInterface
  89. * @throws \Magento\Framework\Exception\LocalizedException
  90. */
  91. public function getShipmentTrack($carrierCode, $trackingNumber)
  92. {
  93. $numberFilter = $this->filterBuilder
  94. ->setField('track_number')
  95. ->setValue($trackingNumber)
  96. ->setConditionType('eq')
  97. ->create();
  98. $carrierFilter = $this->filterBuilder
  99. ->setField('carrier_code')
  100. ->setValue($carrierCode)
  101. ->setConditionType('eq')
  102. ->create();
  103. // builder does not get reset properly on `create()`, instantiate a fresh one…
  104. $searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
  105. $searchCriteria = $searchCriteriaBuilder
  106. ->addFilter($numberFilter)
  107. ->addFilter($carrierFilter)
  108. ->addSortOrder('entity_id', SortOrder::SORT_DESC)
  109. ->setPageSize(1)
  110. ->create();
  111. /** @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection $shipmentTracksCollection */
  112. $shipmentTracksCollection = $this->shipmentTrackRepository->getList($searchCriteria);
  113. /** @var \Magento\Sales\Model\Order\Shipment\Track $shipmentTrack */
  114. $shipmentTrack = $shipmentTracksCollection->fetchItem();
  115. if (!$shipmentTrack) {
  116. throw NoSuchEntityException::singleField('track_number', $trackingNumber);
  117. }
  118. return $shipmentTrack;
  119. }
  120. /**
  121. * Save local reference to external shipment entity.
  122. *
  123. * @param ShipmentReferenceInterface $shipment
  124. * @return ShipmentReferenceInterface
  125. * @throws CouldNotSaveException
  126. */
  127. public function save(ShipmentReferenceInterface $shipment)
  128. {
  129. try {
  130. /** @var \Temando\Shipping\Model\Shipment\ShipmentReference $shipment */
  131. $this->resource->save($shipment);
  132. } catch (\Exception $exception) {
  133. throw new CouldNotSaveException(__('Unable to save shipment reference.'), $exception);
  134. }
  135. return $shipment;
  136. }
  137. /**
  138. * Load local reference to external shipment entity.
  139. *
  140. * @param int $entityId
  141. * @return ShipmentReferenceInterface
  142. * @throws NoSuchEntityException
  143. */
  144. public function getById($entityId)
  145. {
  146. /** @var \Temando\Shipping\Model\Shipment\ShipmentReference $shipment */
  147. $shipment = $this->shipmentReferenceFactory->create();
  148. $this->resource->load($shipment, $entityId);
  149. if (!$shipment->getId()) {
  150. throw new NoSuchEntityException(__('Shipment with id "%1" does not exist.', $entityId));
  151. }
  152. return $shipment;
  153. }
  154. /**
  155. * Load local reference to external shipment entity by Magento shipment ID.
  156. *
  157. * @param int $shipmentId
  158. * @return ShipmentReferenceInterface
  159. * @throws NoSuchEntityException
  160. */
  161. public function getByShipmentId($shipmentId)
  162. {
  163. $entityId = $this->resource->getIdByShipmentId($shipmentId);
  164. return $this->getById($entityId);
  165. }
  166. /**
  167. * Load local reference to external shipment entity by Temando shipment ID.
  168. *
  169. * @param string $extShipmentId
  170. *
  171. * @return ShipmentReferenceInterface
  172. * @throws NoSuchEntityException
  173. */
  174. public function getByExtShipmentId($extShipmentId)
  175. {
  176. $entityId = $this->resource->getIdByExtShipmentId($extShipmentId);
  177. return $this->getById($entityId);
  178. }
  179. /**
  180. * Load local reference to external shipment entity by Temando return shipment ID.
  181. *
  182. * @param string $extShipmentId
  183. *
  184. * @return ShipmentReferenceInterface
  185. * @throws NoSuchEntityException
  186. */
  187. public function getByExtReturnShipmentId($extShipmentId)
  188. {
  189. $entityId = $this->resource->getIdByExtReturnShipmentId($extShipmentId);
  190. return $this->getById($entityId);
  191. }
  192. /**
  193. * List shipment references that match specified search criteria.
  194. *
  195. * @param SearchCriteriaInterface $searchCriteria
  196. * @return ShipmentReferenceCollection
  197. */
  198. public function getList(SearchCriteriaInterface $searchCriteria)
  199. {
  200. $collection = $this->shipmentReferenceCollectionFactory->create();
  201. $this->collectionProcessor->process($searchCriteria, $collection);
  202. return $collection;
  203. }
  204. }