PickupLocationManagement.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\PickupLocationSearchRequestInterface;
  10. use Temando\Shipping\Api\Data\Delivery\PickupLocationSearchResultInterface;
  11. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterface;
  12. use Temando\Shipping\Model\Checkout\Delivery\PickupLocationManagement as CheckoutPickupLocationManagement;
  13. use Temando\Shipping\Model\ResourceModel\Repository\QuotePickupLocationRepositoryInterface;
  14. /**
  15. * Manage Pickup Location Access
  16. *
  17. * @deprecated since 1.5.1
  18. * @see \Temando\Shipping\Model\Checkout\Delivery\PickupLocationManagement
  19. *
  20. * @package Temando\Shipping\Model
  21. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  22. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  23. * @link https://www.temando.com/
  24. */
  25. class PickupLocationManagement
  26. {
  27. /**
  28. * @var CheckoutPickupLocationManagement
  29. */
  30. private $pickupLocationManagement;
  31. /**
  32. * @var QuotePickupLocationRepositoryInterface
  33. */
  34. private $pickupLocationRepository;
  35. /**
  36. * PickupLocationManagement constructor.
  37. *
  38. * @param CheckoutPickupLocationManagement $pickupLocationManagement
  39. * @param QuotePickupLocationRepositoryInterface $pickupLocationRepository
  40. */
  41. public function __construct(
  42. CheckoutPickupLocationManagement $pickupLocationManagement,
  43. QuotePickupLocationRepositoryInterface $pickupLocationRepository
  44. ) {
  45. $this->pickupLocationManagement = $pickupLocationManagement;
  46. $this->pickupLocationRepository = $pickupLocationRepository;
  47. }
  48. /**
  49. * Save new search parameters, delete previous search results.
  50. *
  51. * @param int $addressId
  52. * @param bool $active
  53. * @return PickupLocationSearchRequestInterface
  54. * @throws CouldNotSaveException
  55. */
  56. public function saveSearchRequest($addressId, $active)
  57. {
  58. return $this->pickupLocationManagement->saveSearchRequest($addressId, $active);
  59. }
  60. /**
  61. * Delete search parameters, delete previous search results.
  62. *
  63. * @param int $addressId
  64. * @return bool
  65. * @throws CouldNotDeleteException
  66. */
  67. public function deleteSearchRequest($addressId)
  68. {
  69. return $this->pickupLocationManagement->deleteSearchRequest($addressId);
  70. }
  71. /**
  72. * Load all collection location results for a given shipping address id.
  73. *
  74. * Sort by pseudo field `sort_distance` that gets added to handle null values.
  75. *
  76. * @see QuotePickupLocationRepository::getList
  77. * @param int $addressId
  78. * @return QuotePickupLocationInterface[]
  79. */
  80. public function getPickupLocations($addressId)
  81. {
  82. return $this->pickupLocationManagement->getPickupLocations($addressId);
  83. }
  84. /**
  85. * Delete all collect location search results for a given shipping address id.
  86. *
  87. * @param int $addressId
  88. * @return PickupLocationSearchResultInterface
  89. * @throws CouldNotDeleteException
  90. */
  91. public function deletePickupLocations($addressId)
  92. {
  93. return $this->pickupLocationManagement->deletePickupLocations($addressId);
  94. }
  95. /**
  96. * Mark a pickup location search result as selected for a given shipping address id.
  97. *
  98. * @param int $addressId
  99. * @param int $entityId
  100. * @return bool
  101. * @throws CouldNotSaveException
  102. */
  103. public function selectPickupLocation($addressId, $entityId)
  104. {
  105. $pickupLocations = $this->getPickupLocations($addressId);
  106. try {
  107. array_walk($pickupLocations, function (QuotePickupLocationInterface $pickupLocation) use ($entityId) {
  108. $isSelected = ($entityId == $pickupLocation->getEntityId());
  109. /** @var QuotePickupLocation $pickupLocation */
  110. $pickupLocation->setData(QuotePickupLocationInterface::SELECTED, $isSelected);
  111. $this->pickupLocationRepository->save($pickupLocation);
  112. });
  113. } catch (LocalizedException $exception) {
  114. throw new CouldNotSaveException(__('Unable to select pickup location.'), $exception);
  115. }
  116. return true;
  117. }
  118. }