CollectionPointManifestProcessor.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\OrderCollectionPointInterface;
  10. use Temando\Shipping\Api\Data\Delivery\OrderCollectionPointInterfaceFactory;
  11. use Temando\Shipping\Api\Data\Delivery\QuoteCollectionPointInterface;
  12. use Temando\Shipping\Model\OrderInterface;
  13. use Temando\Shipping\Model\ResourceModel\Repository\OrderCollectionPointRepositoryInterface;
  14. use Temando\Shipping\Webservice\Response\Type\OrderResponseType;
  15. /**
  16. * Temando Collection Point Manifestation Processor.
  17. *
  18. * Assign the collection point selected during checkout to an order address.
  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 CollectionPointManifestProcessor implements SaveProcessorInterface
  26. {
  27. /**
  28. * @var HydratorInterface
  29. */
  30. private $hydrator;
  31. /**
  32. * @var OrderCollectionPointInterfaceFactory
  33. */
  34. private $collectionPointFactory;
  35. /**
  36. * @var OrderCollectionPointRepositoryInterface
  37. */
  38. private $collectionPointRepository;
  39. /**
  40. * CollectionPointManifestProcessor constructor.
  41. * @param HydratorInterface $hydrator
  42. * @param OrderCollectionPointInterfaceFactory $collectionPointFactory
  43. * @param OrderCollectionPointRepositoryInterface $collectionPointRepository
  44. */
  45. public function __construct(
  46. HydratorInterface $hydrator,
  47. OrderCollectionPointInterfaceFactory $collectionPointFactory,
  48. OrderCollectionPointRepositoryInterface $collectionPointRepository
  49. ) {
  50. $this->hydrator = $hydrator;
  51. $this->collectionPointFactory = $collectionPointFactory;
  52. $this->collectionPointRepository = $collectionPointRepository;
  53. }
  54. /**
  55. * Assign order shipping address to the selected collection point.
  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. $quoteCollectionPoint = $requestType->getCollectionPoint();
  69. if ($quoteCollectionPoint instanceof QuoteCollectionPointInterface) {
  70. $shippingAddressId = $salesOrder->getShippingAddress()->getId();
  71. $collectionPointData = $this->hydrator->extract($quoteCollectionPoint);
  72. $collectionPointData[OrderCollectionPointInterface::RECIPIENT_ADDRESS_ID] = $shippingAddressId;
  73. $orderCollectionPoint = $this->collectionPointFactory->create(['data' => $collectionPointData]);
  74. $this->collectionPointRepository->save($orderCollectionPoint);
  75. }
  76. }
  77. }