GuestCartCollectionPointManagement.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Checkout\Delivery;
  6. use Magento\Framework\Exception\CouldNotDeleteException;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Quote\Model\GuestCart\GuestShippingAddressManagementInterface;
  10. use Temando\Shipping\Api\Checkout\GuestCartCollectionPointManagementInterface;
  11. use Temando\Shipping\Api\Data\Delivery\CollectionPointSearchRequestInterface;
  12. /**
  13. * Process "collection point" delivery option (guest checkout).
  14. *
  15. * @package Temando\Shipping\Model
  16. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  17. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  18. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link https://www.temando.com/
  20. */
  21. class GuestCartCollectionPointManagement implements GuestCartCollectionPointManagementInterface
  22. {
  23. /**
  24. * @var GuestShippingAddressManagementInterface
  25. */
  26. private $addressManagement;
  27. /**
  28. * @var CollectionPointManagement
  29. */
  30. private $collectionPointManagement;
  31. /**
  32. * GuestCartCollectionPointManagement constructor.
  33. *
  34. * @param GuestShippingAddressManagementInterface $addressManagement
  35. * @param CollectionPointManagement $collectionPointManagement
  36. */
  37. public function __construct(
  38. GuestShippingAddressManagementInterface $addressManagement,
  39. CollectionPointManagement $collectionPointManagement
  40. ) {
  41. $this->addressManagement = $addressManagement;
  42. $this->collectionPointManagement = $collectionPointManagement;
  43. }
  44. /**
  45. * Save a customer's search for collection points.
  46. *
  47. * @param string $cartId
  48. * @param string $countryId
  49. * @param string $postcode
  50. * @return CollectionPointSearchRequestInterface
  51. * @throws CouldNotSaveException
  52. */
  53. public function saveSearchRequest(
  54. string $cartId,
  55. string $countryId,
  56. string $postcode
  57. ): CollectionPointSearchRequestInterface {
  58. try {
  59. $shippingAddress = $this->addressManagement->get($cartId);
  60. } catch (NoSuchEntityException $exception) {
  61. throw new CouldNotSaveException(__('Unable to load shipping address for specified quote.'));
  62. }
  63. return $this->collectionPointManagement->saveSearchRequest($shippingAddress->getId(), $countryId, $postcode);
  64. }
  65. /**
  66. * Delete a customer's search for collection points.
  67. *
  68. * @param string $cartId
  69. * @return bool
  70. * @throws CouldNotDeleteException
  71. */
  72. public function deleteSearchRequest(string $cartId): bool
  73. {
  74. try {
  75. $shippingAddress = $this->addressManagement->get($cartId);
  76. } catch (NoSuchEntityException $exception) {
  77. throw new CouldNotDeleteException(__('Unable to load shipping address for specified quote.'));
  78. }
  79. return $this->collectionPointManagement->deleteSearchRequest($shippingAddress->getId());
  80. }
  81. /**
  82. * Retrieve collection points matching the customer's search parameters.
  83. *
  84. * @param string $cartId
  85. * @return \Temando\Shipping\Api\Data\Delivery\QuoteCollectionPointInterface[]
  86. */
  87. public function getCollectionPoints(string $cartId): array
  88. {
  89. try {
  90. $shippingAddress = $this->addressManagement->get($cartId);
  91. } catch (NoSuchEntityException $exception) {
  92. return [];
  93. }
  94. return $this->collectionPointManagement->getCollectionPoints($shippingAddress->getId());
  95. }
  96. /**
  97. * Select a given collection point for checkout.
  98. *
  99. * @param string $cartId
  100. * @param string $collectionPointId
  101. * @return bool
  102. * @throws CouldNotSaveException
  103. */
  104. public function selectCollectionPoint(string $cartId, string $collectionPointId): bool
  105. {
  106. try {
  107. $shippingAddress = $this->addressManagement->get($cartId);
  108. } catch (NoSuchEntityException $exception) {
  109. throw new CouldNotSaveException(__('Unable to load shipping address for specified quote.'));
  110. }
  111. return $this->collectionPointManagement->selectCollectionPoint($shippingAddress->getId(), $collectionPointId);
  112. }
  113. }