QuoteCollectionPointRepository.php 6.7 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\CollectionPointSearchResultInterface;
  13. use Temando\Shipping\Api\Data\Delivery\CollectionPointSearchResultInterfaceFactory;
  14. use Temando\Shipping\Api\Data\Delivery\QuoteCollectionPointInterface;
  15. use Temando\Shipping\Api\Data\Delivery\QuoteCollectionPointInterfaceFactory;
  16. use Temando\Shipping\Model\Delivery\QuoteCollectionPoint;
  17. use Temando\Shipping\Model\ResourceModel\Delivery\QuoteCollectionPoint as CollectionPointResource;
  18. use Temando\Shipping\Model\ResourceModel\Repository\QuoteCollectionPointRepositoryInterface;
  19. /**
  20. * Temando Quote Collection Point 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 QuoteCollectionPointRepository implements QuoteCollectionPointRepositoryInterface
  29. {
  30. /**
  31. * @var CollectionPointResource
  32. */
  33. private $resource;
  34. /**
  35. * @var SearchCriteriaBuilder
  36. */
  37. private $searchCriteriaBuilder;
  38. /**
  39. * @var QuoteCollectionPointInterfaceFactory
  40. */
  41. private $collectionPointFactory;
  42. /**
  43. * @var CollectionPointSearchResultInterfaceFactory
  44. */
  45. private $collectionPointSearchResultFactory;
  46. /**
  47. * @var CollectionProcessorInterface
  48. */
  49. private $collectionProcessor;
  50. /**
  51. * CollectionPointRepository constructor.
  52. * @param CollectionPointResource $resource
  53. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  54. * @param QuoteCollectionPointInterfaceFactory $collectionPointFactory
  55. * @param CollectionPointSearchResultInterfaceFactory $collectionPointSearchResultFactory
  56. * @param CollectionProcessorInterface $collectionProcessor
  57. */
  58. public function __construct(
  59. CollectionPointResource $resource,
  60. SearchCriteriaBuilder $searchCriteriaBuilder,
  61. QuoteCollectionPointInterfaceFactory $collectionPointFactory,
  62. CollectionPointSearchResultInterfaceFactory $collectionPointSearchResultFactory,
  63. CollectionProcessorInterface $collectionProcessor
  64. ) {
  65. $this->resource = $resource;
  66. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  67. $this->collectionPointFactory = $collectionPointFactory;
  68. $this->collectionPointSearchResultFactory = $collectionPointSearchResultFactory;
  69. $this->collectionProcessor = $collectionProcessor;
  70. }
  71. /**
  72. * Load collection point by entity id.
  73. *
  74. * @param int $entityId
  75. * @return QuoteCollectionPointInterface
  76. * @throws NoSuchEntityException
  77. */
  78. public function get($entityId)
  79. {
  80. /** @var QuoteCollectionPoint $collectionPoint */
  81. $collectionPoint = $this->collectionPointFactory->create();
  82. try {
  83. $this->resource->load($collectionPoint, $entityId);
  84. } catch (\Exception $exception) {
  85. throw new NoSuchEntityException(__('Collection point with id "%1" does not exist.', $entityId));
  86. }
  87. return $collectionPoint;
  88. }
  89. /**
  90. * Load selected collection point for given shipping address ID.
  91. *
  92. * Beware: AbstractDb::fetchItem will NOT decode serialized fields.
  93. *
  94. * @param int $addressId
  95. * @return QuoteCollectionPointInterface
  96. * @throws NoSuchEntityException
  97. */
  98. public function getSelected($addressId)
  99. {
  100. $this->searchCriteriaBuilder->addFilter(
  101. QuoteCollectionPointInterface::RECIPIENT_ADDRESS_ID,
  102. $addressId
  103. );
  104. $this->searchCriteriaBuilder->addFilter(
  105. QuoteCollectionPointInterface::SELECTED,
  106. true
  107. );
  108. $this->searchCriteriaBuilder->setPageSize(1);
  109. $this->searchCriteriaBuilder->setCurrentPage(1);
  110. $criteria = $this->searchCriteriaBuilder->create();
  111. /** @var CollectionPointSearchResult $searchResult */
  112. $searchResult = $this->getList($criteria);
  113. /** @var QuoteCollectionPointInterface $collectionPoint */
  114. $collectionPoint = $searchResult->fetchItem();
  115. if (!$collectionPoint) {
  116. $msg = __('Selected collection point for address id "%1" does not exist.', $addressId);
  117. throw new NoSuchEntityException($msg);
  118. }
  119. return $collectionPoint;
  120. }
  121. /**
  122. * Save collection point.
  123. *
  124. * @param QuoteCollectionPointInterface $collectionPoint
  125. * @return QuoteCollectionPointInterface
  126. * @throws CouldNotSaveException
  127. */
  128. public function save(QuoteCollectionPointInterface $collectionPoint)
  129. {
  130. try {
  131. /** @var QuoteCollectionPoint $collectionPoint */
  132. $this->resource->save($collectionPoint);
  133. } catch (\Exception $exception) {
  134. throw new CouldNotSaveException(__('Unable to save collection point.'), $exception);
  135. }
  136. return $collectionPoint;
  137. }
  138. /**
  139. * Delete collection point.
  140. *
  141. * @param QuoteCollectionPointInterface $collectionPoint
  142. * @return bool
  143. * @throws CouldNotDeleteException
  144. */
  145. public function delete(QuoteCollectionPointInterface $collectionPoint)
  146. {
  147. try {
  148. /** @var QuoteCollectionPoint $collectionPoint */
  149. $this->resource->delete($collectionPoint);
  150. } catch (\Exception $exception) {
  151. throw new CouldNotDeleteException(__('Unable to delete collection point.'), $exception);
  152. }
  153. return true;
  154. }
  155. /**
  156. * Load collection points.
  157. *
  158. * @param SearchCriteriaInterface $criteria
  159. * @return CollectionPointSearchResultInterface|CollectionPointSearchResult
  160. */
  161. public function getList(SearchCriteriaInterface $criteria)
  162. {
  163. /** @var CollectionPointSearchResult $searchResult */
  164. $searchResult = $this->collectionPointSearchResultFactory->create();
  165. $searchResult->addExpressionFieldToSelect(
  166. 'sort_distance',
  167. 'COALESCE({{sd}}, POW(2, 32))',
  168. ['sd' => QuoteCollectionPointInterface::DISTANCE]
  169. );
  170. $this->collectionProcessor->process($criteria, $searchResult);
  171. $searchResult->setSearchCriteria($criteria);
  172. return $searchResult;
  173. }
  174. }