PickupLocationManifestProcessor.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\EntityManager\HydratorInterface;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Sales\Api\Data\OrderInterface as SalesOrderInterface;
  9. use Temando\Shipping\Api\Data\Delivery\OrderPickupLocationInterface;
  10. use Temando\Shipping\Api\Data\Delivery\OrderPickupLocationInterfaceFactory;
  11. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterface;
  12. use Temando\Shipping\Model\OrderInterface;
  13. use Temando\Shipping\Model\ResourceModel\Repository\OrderPickupLocationRepositoryInterface;
  14. use Temando\Shipping\Webservice\Response\Type\OrderResponseType;
  15. /**
  16. * Temando Pickup Location Manifestation Processor.
  17. *
  18. * Assign the pickup location selected during checkout to an order address.
  19. *
  20. * @package Temando\Shipping\Webservice
  21. * @author Sebastian Ertner <sebastian.ertner@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 PickupLocationManifestProcessor implements SaveProcessorInterface
  26. {
  27. /**
  28. * @var HydratorInterface
  29. */
  30. private $hydrator;
  31. /**
  32. * @var OrderPickupLocationInterfaceFactory
  33. */
  34. private $pickupLocationFactory;
  35. /**
  36. * @var OrderPickupLocationRepositoryInterface
  37. */
  38. private $pickupLocationRepository;
  39. /**
  40. * CollectionPointManifestProcessor constructor.
  41. * @param HydratorInterface $hydrator
  42. * @param OrderPickupLocationInterfaceFactory $pickupLocationFactory
  43. * @param OrderPickupLocationRepositoryInterface $pickupLocationRepository
  44. */
  45. public function __construct(
  46. HydratorInterface $hydrator,
  47. OrderPickupLocationInterfaceFactory $pickupLocationFactory,
  48. OrderPickupLocationRepositoryInterface $pickupLocationRepository
  49. ) {
  50. $this->hydrator = $hydrator;
  51. $this->pickupLocationFactory = $pickupLocationFactory;
  52. $this->pickupLocationRepository = $pickupLocationRepository;
  53. }
  54. /**
  55. * Assign order shipping address to the selected pickup location.
  56. *
  57. * @param SalesOrderInterface|\Magento\Sales\Model\Order $salesOrder
  58. * @param OrderInterface $requestType
  59. * @param OrderResponseType $responseType
  60. * @return void
  61. * @throws LocalizedException
  62. */
  63. public function postProcess(
  64. SalesOrderInterface $salesOrder,
  65. OrderInterface $requestType,
  66. OrderResponseType $responseType
  67. ) {
  68. /** @var \Temando\Shipping\Model\Delivery\QuotePickupLocation $pickupLocation */
  69. $quotePickupLocation = $requestType->getPickupLocation();
  70. if ($quotePickupLocation instanceof QuotePickupLocationInterface) {
  71. $shippingAddressId = $salesOrder->getShippingAddress()->getId();
  72. $pickupLocationData = $this->hydrator->extract($quotePickupLocation);
  73. $pickupLocationData[OrderPickupLocationInterface::RECIPIENT_ADDRESS_ID] = $shippingAddressId;
  74. $orderPickupLocation = $this->pickupLocationFactory->create(['data' => $pickupLocationData]);
  75. $this->pickupLocationRepository->save($orderPickupLocation);
  76. }
  77. }
  78. }