QuotePickupLocationRepository.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Delivery;
  6. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  7. use Magento\Framework\Api\SearchCriteriaBuilder;
  8. use Magento\Framework\Api\SearchCriteriaInterface;
  9. use Magento\Framework\Exception\CouldNotDeleteException;
  10. use Magento\Framework\Exception\CouldNotSaveException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Temando\Shipping\Api\Data\Delivery\PickupLocationSearchResultInterface;
  13. use Temando\Shipping\Api\Data\Delivery\PickupLocationSearchResultInterfaceFactory;
  14. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterface;
  15. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterfaceFactory;
  16. use Temando\Shipping\Model\Delivery\QuotePickupLocation;
  17. use Temando\Shipping\Model\ResourceModel\Delivery\QuotePickupLocation as PickupLocationResource;
  18. use Temando\Shipping\Model\ResourceModel\Repository\QuotePickupLocationRepositoryInterface;
  19. /**
  20. * Temando Quote Pickup Location Repository
  21. *
  22. * @package Temando\Shipping\Model
  23. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  24. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  25. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  26. * @link https://www.temando.com/
  27. */
  28. class QuotePickupLocationRepository implements QuotePickupLocationRepositoryInterface
  29. {
  30. /**
  31. * @var PickupLocationResource
  32. */
  33. private $resource;
  34. /**
  35. * @var SearchCriteriaBuilder
  36. */
  37. private $searchCriteriaBuilder;
  38. /**
  39. * @var QuotePickupLocationInterfaceFactory
  40. */
  41. private $pickupLocationFactory;
  42. /**
  43. * @var PickupLocationSearchResultInterfaceFactory
  44. */
  45. private $pickupLocationSearchResultFactory;
  46. /**
  47. * @var CollectionProcessorInterface
  48. */
  49. private $collectionProcessor;
  50. /**
  51. * PickupLocationRepository constructor.
  52. * @param PickupLocationResource $resource
  53. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  54. * @param QuotePickupLocationInterfaceFactory $pickupLocationFactory
  55. * @param PickupLocationSearchResultInterfaceFactory $pickupLocationSearchResultFactory
  56. * @param CollectionProcessorInterface $collectionProcessor
  57. */
  58. public function __construct(
  59. PickupLocationResource $resource,
  60. SearchCriteriaBuilder $searchCriteriaBuilder,
  61. QuotePickupLocationInterfaceFactory $pickupLocationFactory,
  62. PickupLocationSearchResultInterfaceFactory $pickupLocationSearchResultFactory,
  63. CollectionProcessorInterface $collectionProcessor
  64. ) {
  65. $this->resource = $resource;
  66. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  67. $this->pickupLocationFactory = $pickupLocationFactory;
  68. $this->pickupLocationSearchResultFactory = $pickupLocationSearchResultFactory;
  69. $this->collectionProcessor = $collectionProcessor;
  70. }
  71. /**
  72. * Load pickup location by entity id.
  73. *
  74. * @param int $entityId
  75. * @return QuotePickupLocationInterface
  76. * @throws NoSuchEntityException
  77. */
  78. public function get($entityId)
  79. {
  80. /** @var QuotePickupLocation $pickupLocation */
  81. $pickupLocation = $this->pickupLocationFactory->create();
  82. try {
  83. $this->resource->load($pickupLocation, $entityId);
  84. } catch (\Exception $exception) {
  85. throw new NoSuchEntityException(__('Pickup location with id "%1" does not exist.', $entityId));
  86. }
  87. return $pickupLocation;
  88. }
  89. /**
  90. * Load selected pickup location for given shipping address ID.
  91. *
  92. * Beware: AbstractDb::fetchItem will NOT decode serialized fields.
  93. *
  94. * @param int $addressId
  95. * @return QuotePickupLocationInterface
  96. * @throws NoSuchEntityException
  97. */
  98. public function getSelected($addressId)
  99. {
  100. $this->searchCriteriaBuilder->addFilter(
  101. QuotePickupLocationInterface::RECIPIENT_ADDRESS_ID,
  102. $addressId
  103. );
  104. $this->searchCriteriaBuilder->addFilter(
  105. QuotePickupLocationInterface::SELECTED,
  106. true
  107. );
  108. $this->searchCriteriaBuilder->setPageSize(1);
  109. $this->searchCriteriaBuilder->setCurrentPage(1);
  110. $criteria = $this->searchCriteriaBuilder->create();
  111. /** @var PickupLocationSearchResult $searchResult */
  112. $searchResult = $this->getList($criteria);
  113. /** @var QuotePickupLocationInterface $pickupLocation */
  114. $pickupLocation = $searchResult->fetchItem();
  115. if (!$pickupLocation) {
  116. $msg = __('Selected pickup location for address id "%1" does not exist.', $addressId);
  117. throw new NoSuchEntityException($msg);
  118. }
  119. return $pickupLocation;
  120. }
  121. /**
  122. * Save pickup location.
  123. *
  124. * @param QuotePickupLocationInterface $pickupLocation
  125. * @return QuotePickupLocationInterface
  126. * @throws CouldNotSaveException
  127. */
  128. public function save(QuotePickupLocationInterface $pickupLocation)
  129. {
  130. try {
  131. /** @var QuotePickupLocation $pickupLocation */
  132. $this->resource->save($pickupLocation);
  133. } catch (\Exception $exception) {
  134. throw new CouldNotSaveException(__('Unable to save pickup location.'), $exception);
  135. }
  136. return $pickupLocation;
  137. }
  138. /**
  139. * Delete pickup location.
  140. *
  141. * @param QuotePickupLocationInterface $pickupLocation
  142. * @return bool
  143. * @throws CouldNotDeleteException
  144. */
  145. public function delete(QuotePickupLocationInterface $pickupLocation)
  146. {
  147. try {
  148. /** @var QuotePickupLocation $pickupLocation */
  149. $this->resource->delete($pickupLocation);
  150. } catch (\Exception $exception) {
  151. throw new CouldNotDeleteException(__('Unable to delete pickup location.'), $exception);
  152. }
  153. return true;
  154. }
  155. /**
  156. * Load pickup locations.
  157. *
  158. * @param SearchCriteriaInterface $criteria
  159. * @return PickupLocationSearchResultInterface|PickupLocationSearchResult
  160. */
  161. public function getList(SearchCriteriaInterface $criteria)
  162. {
  163. /** @var PickupLocationSearchResult $searchResult */
  164. $searchResult = $this->pickupLocationSearchResultFactory->create();
  165. $searchResult->addExpressionFieldToSelect(
  166. 'sort_distance',
  167. 'COALESCE({{sd}}, POW(2, 32))',
  168. ['sd' => QuotePickupLocationInterface::DISTANCE]
  169. );
  170. $this->collectionProcessor->process($criteria, $searchResult);
  171. $searchResult->setSearchCriteria($criteria);
  172. return $searchResult;
  173. }
  174. }