CollectionPointRatesProcessor.php 4.1 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\QuoteCollectionPointInterface;
  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\CollectionPointSearchResult;
  13. use Temando\Shipping\Model\ResourceModel\Repository\QuoteCollectionPointRepositoryInterface;
  14. use Temando\Shipping\Webservice\Response\Type\QualificationResponseType;
  15. /**
  16. * Temando Collection Point Rates Processor.
  17. *
  18. * Read experiences/rates for a collection point 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 CollectionPointRatesProcessor implements RatesProcessorInterface
  26. {
  27. /**
  28. * @var SearchCriteriaBuilder
  29. */
  30. private $searchCriteriaBuilder;
  31. /**
  32. * @var QuoteCollectionPointRepositoryInterface
  33. */
  34. private $collectionPointRepository;
  35. /**
  36. * @var ShippingExperienceInterfaceFactory
  37. */
  38. private $shippingExperienceFactory;
  39. /**
  40. * CollectionPointRatesProcessor constructor.
  41. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  42. * @param QuoteCollectionPointRepositoryInterface $collectionPointRepository
  43. * @param ShippingExperienceInterfaceFactory $shippingExperienceFactory
  44. */
  45. public function __construct(
  46. SearchCriteriaBuilder $searchCriteriaBuilder,
  47. QuoteCollectionPointRepositoryInterface $collectionPointRepository,
  48. ShippingExperienceInterfaceFactory $shippingExperienceFactory
  49. ) {
  50. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  51. $this->collectionPointRepository = $collectionPointRepository;
  52. $this->shippingExperienceFactory = $shippingExperienceFactory;
  53. }
  54. /**
  55. * Load shipping experiences for the selected collection point.
  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. $collectionPoint = $requestType->getCollectionPoint();
  68. if ($collectionPoint === null) {
  69. // no selected collection point in request
  70. return [];
  71. }
  72. $this->searchCriteriaBuilder->addFilter(
  73. QuoteCollectionPointInterface::RECIPIENT_ADDRESS_ID,
  74. $collectionPoint->getRecipientAddressId()
  75. );
  76. $this->searchCriteriaBuilder->addFilter(
  77. QuoteCollectionPointInterface::COLLECTION_POINT_ID,
  78. $collectionPoint->getCollectionPointId()
  79. );
  80. $this->searchCriteriaBuilder->setPageSize(1);
  81. $this->searchCriteriaBuilder->setCurrentPage(1);
  82. $searchCriteria = $this->searchCriteriaBuilder->create();
  83. /** @var CollectionPointSearchResult $searchResult */
  84. $searchResult = $this->collectionPointRepository->getList($searchCriteria);
  85. /** @var QuoteCollectionPointInterface $collectionPoint */
  86. $collectionPoint = $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. }, $collectionPoint->getShippingExperiences());
  94. return $experiences;
  95. }
  96. }