PickupLocationSearchProcessor.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\CouldNotSaveException;
  7. use Magento\Quote\Model\Quote\Address\RateRequest;
  8. use Temando\Shipping\Api\Data\Delivery\PickupLocationSearchResultInterfaceFactory;
  9. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterface;
  10. use Temando\Shipping\Api\Data\Order\ShippingExperienceInterface;
  11. use Temando\Shipping\Model\Delivery\QuotePickupLocation;
  12. use Temando\Shipping\Model\OrderInterface;
  13. use Temando\Shipping\Model\ResourceModel\Delivery\PickupLocationSearchResult;
  14. use Temando\Shipping\Webservice\Response\Type\QualificationResponseType;
  15. /**
  16. * Temando Pickup Location Search Processor.
  17. *
  18. * Persist pickup location 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 PickupLocationSearchProcessor implements RatesProcessorInterface
  26. {
  27. /**
  28. * @var PickupLocationSearchResultInterfaceFactory
  29. */
  30. private $searchResultFactory;
  31. /**
  32. * PickupLocationSearchProcessor constructor.
  33. * @param PickupLocationSearchResultInterfaceFactory $searchResultFactory
  34. */
  35. public function __construct(PickupLocationSearchResultInterfaceFactory $searchResultFactory)
  36. {
  37. $this->searchResultFactory = $searchResultFactory;
  38. }
  39. /**
  40. * Persist pickup locations from rates response.
  41. *
  42. * @param RateRequest $rateRequest
  43. * @param OrderInterface $requestType
  44. * @param QualificationResponseType $responseType
  45. * @return ShippingExperienceInterface[]
  46. * @throws CouldNotSaveException
  47. */
  48. public function postProcess(
  49. RateRequest $rateRequest,
  50. OrderInterface $requestType,
  51. QualificationResponseType $responseType
  52. ) {
  53. $searchRequest = $requestType->getPickupLocationSearchRequest();
  54. $pickupLocation = $requestType->getPickupLocation();
  55. if ($searchRequest === null) {
  56. // no search, no pickup location to persist
  57. return [];
  58. }
  59. if ($pickupLocation && $pickupLocation->getPickupLocationId()) {
  60. // delivery location was selected, no need to update pickup locations
  61. return [];
  62. }
  63. // persist pickup locations for a given search request
  64. $shippingAddressId = $searchRequest->getShippingAddressId();
  65. $pickupLocations = $responseType->getPickupLocations();
  66. /** @var QuotePickupLocation $pickupLocation */
  67. foreach ($pickupLocations as $pickupLocation) {
  68. $pickupLocation->setData(QuotePickupLocationInterface::RECIPIENT_ADDRESS_ID, $shippingAddressId);
  69. }
  70. /** @var PickupLocationSearchResult $collection */
  71. $collection = $this->searchResultFactory->create();
  72. $collection->addFilter(QuotePickupLocationInterface::RECIPIENT_ADDRESS_ID, $shippingAddressId);
  73. $collection->walk('delete');
  74. $collection->setItems($pickupLocations);
  75. $collection->save();
  76. return [];
  77. }
  78. }