PickupLocationRatesProcessor.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Webservice\Processor\OrderOperation;
  6. use Magento\Framework\Api\SearchCriteriaBuilder;
  7. use Magento\Quote\Model\Quote\Address\RateRequest;
  8. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterface;
  9. use Temando\Shipping\Api\Data\Order\ShippingExperienceInterface;
  10. use Temando\Shipping\Api\Data\Order\ShippingExperienceInterfaceFactory;
  11. use Temando\Shipping\Model\OrderInterface;
  12. use Temando\Shipping\Model\ResourceModel\Delivery\PickupLocationSearchResult;
  13. use Temando\Shipping\Model\ResourceModel\Repository\QuotePickupLocationRepositoryInterface;
  14. use Temando\Shipping\Webservice\Response\Type\QualificationResponseType;
  15. /**
  16. * Temando Pickup Location Rates Processor.
  17. *
  18. * Read experiences/rates for a pickup location selected during checkout.
  19. *
  20. * @package Temando\Shipping\Webservice
  21. * @author Christoph Aßmann <christoph.assmann@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 PickupLocationRatesProcessor implements RatesProcessorInterface
  26. {
  27. /**
  28. * @var SearchCriteriaBuilder
  29. */
  30. private $searchCriteriaBuilder;
  31. /**
  32. * @var QuotePickupLocationRepositoryInterface
  33. */
  34. private $pickupLocationRepository;
  35. /**
  36. * @var ShippingExperienceInterfaceFactory
  37. */
  38. private $shippingExperienceFactory;
  39. /**
  40. * PickupLocationRatesProcessor constructor.
  41. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  42. * @param QuotePickupLocationRepositoryInterface $pickupLocationRepository
  43. * @param ShippingExperienceInterfaceFactory $shippingExperienceFactory
  44. */
  45. public function __construct(
  46. SearchCriteriaBuilder $searchCriteriaBuilder,
  47. QuotePickupLocationRepositoryInterface $pickupLocationRepository,
  48. ShippingExperienceInterfaceFactory $shippingExperienceFactory
  49. ) {
  50. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  51. $this->pickupLocationRepository = $pickupLocationRepository;
  52. $this->shippingExperienceFactory = $shippingExperienceFactory;
  53. }
  54. /**
  55. * Load shipping experiences for the selected pickup location.
  56. *
  57. * @param RateRequest $rateRequest
  58. * @param OrderInterface $requestType
  59. * @param QualificationResponseType $responseType
  60. * @return ShippingExperienceInterface[]
  61. */
  62. public function postProcess(
  63. RateRequest $rateRequest,
  64. OrderInterface $requestType,
  65. QualificationResponseType $responseType
  66. ) {
  67. $pickupLocation = $requestType->getPickupLocation();
  68. if ($pickupLocation === null) {
  69. // no selected pickup location in request
  70. return [];
  71. }
  72. $this->searchCriteriaBuilder->addFilter(
  73. QuotePickupLocationInterface::RECIPIENT_ADDRESS_ID,
  74. $pickupLocation->getRecipientAddressId()
  75. );
  76. $this->searchCriteriaBuilder->addFilter(
  77. QuotePickupLocationInterface::PICKUP_LOCATION_ID,
  78. $pickupLocation->getPickupLocationId()
  79. );
  80. $this->searchCriteriaBuilder->setPageSize(1);
  81. $this->searchCriteriaBuilder->setCurrentPage(1);
  82. $searchCriteria = $this->searchCriteriaBuilder->create();
  83. /** @var PickupLocationSearchResult $searchResult */
  84. $searchResult = $this->pickupLocationRepository->getList($searchCriteria);
  85. /** @var QuotePickupLocationInterface $pickupLocation */
  86. $pickupLocation = $searchResult->getFirstItem();
  87. $experiences = array_map(function (array $shippingExperience) {
  88. return $this->shippingExperienceFactory->create([
  89. ShippingExperienceInterface::CODE => $shippingExperience['code'],
  90. ShippingExperienceInterface::COST => $shippingExperience['cost'],
  91. ShippingExperienceInterface::LABEL => $shippingExperience['label'],
  92. ]);
  93. }, $pickupLocation->getShippingExperiences());
  94. return $experiences;
  95. }
  96. }