DeliveryOptionManagement.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Quote;
  6. use Magento\Framework\Exception\CouldNotDeleteException;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. use Temando\Shipping\Model\Delivery\CollectionPointManagement;
  9. use Temando\Shipping\Model\Delivery\PickupLocationManagement;
  10. use Temando\Shipping\Model\ResourceModel\Repository\AddressRepositoryInterface;
  11. /**
  12. * Manage delivery options.
  13. *
  14. * @package Temando\Shipping\Model
  15. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  16. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. * @link https://www.temando.com/
  18. */
  19. class DeliveryOptionManagement
  20. {
  21. /**
  22. * Ship to regular address
  23. */
  24. const DELIVERY_OPTION_ADDRESS = 'toAddress';
  25. /**
  26. * Ship to collection point
  27. */
  28. const DELIVERY_OPTION_COLLECTION_POINT = 'toCollectionPoint';
  29. /**
  30. * Collect at pickup location (Click & Collect)
  31. */
  32. const DELIVERY_OPTION_PICKUP = 'clickAndCollect';
  33. /**
  34. * Shipping does not apply, e.g. virtual orders
  35. */
  36. const DELIVERY_OPTION_NONE = 'noShipping';
  37. /**
  38. * @var AddressRepositoryInterface
  39. */
  40. private $addressRepository;
  41. /**
  42. * @var CollectionPointManagement
  43. */
  44. private $collectionPointManagement;
  45. /**
  46. * @var PickupLocationManagement
  47. */
  48. private $pickupLocationManagement;
  49. /**
  50. * DeliveryOptionManagement constructor.
  51. *
  52. * @param AddressRepositoryInterface $addressRepository
  53. * @param CollectionPointManagement $collectionPointManagement
  54. * @param PickupLocationManagement $pickupLocationManagement
  55. */
  56. public function __construct(
  57. AddressRepositoryInterface $addressRepository,
  58. CollectionPointManagement $collectionPointManagement,
  59. PickupLocationManagement $pickupLocationManagement
  60. ) {
  61. $this->addressRepository = $addressRepository;
  62. $this->collectionPointManagement = $collectionPointManagement;
  63. $this->pickupLocationManagement = $pickupLocationManagement;
  64. }
  65. /**
  66. * Perform actions when a consumer changes the delivery option.
  67. * - Clean up previously selected option
  68. * - Set current option pending (i.e. option chosen, no details selected yet)
  69. *
  70. *
  71. * @param int $shippingAddressId
  72. * @param string $selectedOption
  73. *
  74. * @return void
  75. * @throws CouldNotDeleteException
  76. * @throws CouldNotSaveException
  77. */
  78. public function selectOption($shippingAddressId, $selectedOption)
  79. {
  80. switch ($selectedOption) {
  81. case self::DELIVERY_OPTION_ADDRESS:
  82. $this->collectionPointManagement->deleteSearchRequest($shippingAddressId);
  83. $this->pickupLocationManagement->deleteSearchRequest($shippingAddressId);
  84. break;
  85. case self::DELIVERY_OPTION_COLLECTION_POINT:
  86. $this->addressRepository->deleteByShippingAddressId($shippingAddressId);
  87. $this->pickupLocationManagement->deleteSearchRequest($shippingAddressId);
  88. $this->collectionPointManagement->saveSearchRequest($shippingAddressId, '', '', true);
  89. break;
  90. case self::DELIVERY_OPTION_PICKUP:
  91. $this->addressRepository->deleteByShippingAddressId($shippingAddressId);
  92. $this->collectionPointManagement->deleteSearchRequest($shippingAddressId);
  93. $this->pickupLocationManagement->saveSearchRequest($shippingAddressId, true);
  94. break;
  95. }
  96. }
  97. }