CartCollectionPointManagement.php 4.2 KB

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