CollectionPointManagement.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Delivery;
  6. use Magento\Framework\Exception\CouldNotDeleteException;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Temando\Shipping\Api\Data\Delivery\CollectionPointSearchRequestInterface;
  10. use Temando\Shipping\Api\Data\Delivery\CollectionPointSearchResultInterface;
  11. use Temando\Shipping\Api\Data\Delivery\QuoteCollectionPointInterface;
  12. use Temando\Shipping\Model\Checkout\Delivery\CollectionPointManagement as CheckoutCollectionPointManagement;
  13. use Temando\Shipping\Model\ResourceModel\Repository\QuoteCollectionPointRepositoryInterface;
  14. /**
  15. * Manage Collection Point Access
  16. *
  17. * @deprecated since 1.5.1
  18. * @see \Temando\Shipping\Model\Checkout\Delivery\CollectionPointManagement
  19. *
  20. * @package Temando\Shipping\Model
  21. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  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 CollectionPointManagement
  27. {
  28. /**
  29. * @var CheckoutCollectionPointManagement
  30. */
  31. private $collectionPointManagement;
  32. /**
  33. * @var QuoteCollectionPointRepositoryInterface
  34. */
  35. private $collectionPointRepository;
  36. /**
  37. * CollectionPointManagement constructor.
  38. *
  39. * @param CheckoutCollectionPointManagement $collectionPointManagement
  40. * @param QuoteCollectionPointRepositoryInterface $collectionPointRepository
  41. */
  42. public function __construct(
  43. CheckoutCollectionPointManagement $collectionPointManagement,
  44. QuoteCollectionPointRepositoryInterface $collectionPointRepository
  45. ) {
  46. $this->collectionPointManagement = $collectionPointManagement;
  47. $this->collectionPointRepository = $collectionPointRepository;
  48. }
  49. /**
  50. * Save new search parameters, delete previous search results.
  51. *
  52. * @param int $addressId
  53. * @param string $countryId
  54. * @param string $postcode
  55. * @param bool $pending
  56. * @return CollectionPointSearchRequestInterface
  57. * @throws CouldNotSaveException
  58. */
  59. public function saveSearchRequest($addressId, $countryId, $postcode, $pending = false)
  60. {
  61. return $this->collectionPointManagement->saveSearchRequest($addressId, $countryId, $postcode, $pending);
  62. }
  63. /**
  64. * Delete search parameters, delete previous search results.
  65. *
  66. * @param int $addressId
  67. * @return bool
  68. * @throws CouldNotDeleteException
  69. */
  70. public function deleteSearchRequest($addressId)
  71. {
  72. return $this->collectionPointManagement->deleteSearchRequest($addressId);
  73. }
  74. /**
  75. * Load all collection point search results for a given shipping address id.
  76. *
  77. * Sort by pseudo field `sort_distance` that gets added to handle null values.
  78. *
  79. * @see QuoteCollectionPointRepository::getList
  80. * @param int $addressId
  81. * @return QuoteCollectionPointInterface[]
  82. */
  83. public function getCollectionPoints($addressId)
  84. {
  85. return $this->collectionPointManagement->getCollectionPoints($addressId);
  86. }
  87. /**
  88. * Delete all collection point search results for a given shipping address id.
  89. *
  90. * @param int $addressId
  91. * @return CollectionPointSearchResultInterface
  92. * @throws CouldNotDeleteException
  93. */
  94. public function deleteCollectionPoints($addressId)
  95. {
  96. return $this->collectionPointManagement->deleteCollectionPoints($addressId);
  97. }
  98. /**
  99. * Mark a collection point search result as selected for a given shipping address id.
  100. *
  101. * @param int $addressId
  102. * @param int $entityId
  103. * @return bool
  104. * @throws CouldNotSaveException
  105. */
  106. public function selectCollectionPoint($addressId, $entityId)
  107. {
  108. $collectionPoints = $this->getCollectionPoints($addressId);
  109. try {
  110. array_walk($collectionPoints, function (QuoteCollectionPointInterface $collectionPoint) use ($entityId) {
  111. $isSelected = ($entityId == $collectionPoint->getEntityId());
  112. /** @var QuoteCollectionPoint $collectionPoint */
  113. $collectionPoint->setData(QuoteCollectionPointInterface::SELECTED, $isSelected);
  114. $this->collectionPointRepository->save($collectionPoint);
  115. });
  116. } catch (LocalizedException $exception) {
  117. throw new CouldNotSaveException(__('Unable to select collection point.'), $exception);
  118. }
  119. return true;
  120. }
  121. }