CollectionPointSearchProcessor.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Exception\LocalizedException;
  7. use Magento\Quote\Model\Quote\Address\RateRequest;
  8. use Temando\Shipping\Api\Data\Delivery\CollectionPointSearchResultInterfaceFactory;
  9. use Temando\Shipping\Api\Data\Delivery\QuoteCollectionPointInterface;
  10. use Temando\Shipping\Api\Data\Order\ShippingExperienceInterface;
  11. use Temando\Shipping\Model\Delivery\QuoteCollectionPoint;
  12. use Temando\Shipping\Model\OrderInterface;
  13. use Temando\Shipping\Model\ResourceModel\Delivery\CollectionPointSearchResult;
  14. use Temando\Shipping\Webservice\Response\Type\QualificationResponseType;
  15. /**
  16. * Temando Collection Point Search Processor.
  17. *
  18. * Persist collection points search result.
  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 CollectionPointSearchProcessor implements RatesProcessorInterface
  26. {
  27. /**
  28. * @var CollectionPointSearchResultInterfaceFactory
  29. */
  30. private $searchResultFactory;
  31. /**
  32. * CollectionPointSearchProcessor constructor.
  33. * @param CollectionPointSearchResultInterfaceFactory $searchResultFactory
  34. */
  35. public function __construct(CollectionPointSearchResultInterfaceFactory $searchResultFactory)
  36. {
  37. $this->searchResultFactory = $searchResultFactory;
  38. }
  39. /**
  40. * Persist collection points from rates response.
  41. *
  42. * @param RateRequest $rateRequest
  43. * @param OrderInterface $requestType
  44. * @param QualificationResponseType $responseType
  45. * @return ShippingExperienceInterface[]
  46. * @throws LocalizedException
  47. */
  48. public function postProcess(
  49. RateRequest $rateRequest,
  50. OrderInterface $requestType,
  51. QualificationResponseType $responseType
  52. ) {
  53. $searchRequest = $requestType->getCollectionPointSearchRequest();
  54. $collectionPoint = $requestType->getCollectionPoint();
  55. if ($searchRequest === null) {
  56. // no search, no collection points to persist
  57. return [];
  58. }
  59. if ($searchRequest->isPending()) {
  60. // no search parameters submitted yet, no collection points to persist
  61. return [];
  62. }
  63. if ($collectionPoint && $collectionPoint->getCollectionPointId()) {
  64. // delivery location was selected, no need to update collection points
  65. return [];
  66. }
  67. // persist collection points for a given search request
  68. $shippingAddressId = $searchRequest->getShippingAddressId();
  69. $collectionPoints = $responseType->getCollectionPoints();
  70. /** @var QuoteCollectionPoint $collectionPoint */
  71. foreach ($collectionPoints as $collectionPoint) {
  72. $collectionPoint->setData(QuoteCollectionPointInterface::RECIPIENT_ADDRESS_ID, $shippingAddressId);
  73. }
  74. /** @var CollectionPointSearchResult $collection */
  75. $collection = $this->searchResultFactory->create();
  76. $collection->addFilter(QuoteCollectionPointInterface::RECIPIENT_ADDRESS_ID, $shippingAddressId);
  77. $collection->walk('delete');
  78. $collection->setItems($collectionPoints);
  79. $collection->save();
  80. return [];
  81. }
  82. }