ShipmentRepositoryPlugin.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Plugin\Sales\Order;
  6. use Magento\Framework\Exception\CouldNotSaveException;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Sales\Api\Data\ShipmentExtensionFactory;
  9. use Magento\Sales\Api\Data\ShipmentInterface;
  10. use Magento\Sales\Api\ShipmentRepositoryInterface;
  11. use Temando\Shipping\Api\Data\Shipment\ShipmentReferenceInterface;
  12. use Temando\Shipping\Api\Data\Shipment\ShipmentReferenceInterfaceFactory;
  13. use Temando\Shipping\Model\ResourceModel\Repository\ShipmentReferenceRepositoryInterface;
  14. /**
  15. * Save and load external shipment reference to/from shipment
  16. *
  17. * @package Temando\Shipping\Plugin
  18. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  19. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link https://www.temando.com/
  21. */
  22. class ShipmentRepositoryPlugin
  23. {
  24. /**
  25. * @var ShipmentExtensionFactory
  26. */
  27. private $shipmentExtensionFactory;
  28. /**
  29. * @var ShipmentReferenceInterfaceFactory
  30. */
  31. private $shipmentReferenceFactory;
  32. /**
  33. * @var ShipmentReferenceRepositoryInterface
  34. */
  35. private $shipmentReferenceRepository;
  36. /**
  37. * ShipmentRepositoryPlugin constructor.
  38. * @param ShipmentExtensionFactory $extensionFactory
  39. * @param ShipmentReferenceInterfaceFactory $shipmentReferenceFactory
  40. * @param ShipmentReferenceRepositoryInterface $shipmentReferenceRepository
  41. */
  42. public function __construct(
  43. ShipmentExtensionFactory $extensionFactory,
  44. ShipmentReferenceInterfaceFactory $shipmentReferenceFactory,
  45. ShipmentReferenceRepositoryInterface $shipmentReferenceRepository
  46. ) {
  47. $this->shipmentExtensionFactory = $extensionFactory;
  48. $this->shipmentReferenceFactory = $shipmentReferenceFactory;
  49. $this->shipmentReferenceRepository = $shipmentReferenceRepository;
  50. }
  51. /**
  52. * Load extension attributes to shipment.
  53. *
  54. * @param ShipmentRepositoryInterface $subject
  55. * @param ShipmentInterface $shipment
  56. * @return ShipmentInterface
  57. */
  58. public function afterGet(
  59. ShipmentRepositoryInterface $subject,
  60. ShipmentInterface $shipment
  61. ) {
  62. $extensionAttributes = $shipment->getExtensionAttributes();
  63. if (empty($extensionAttributes)) {
  64. $extensionAttributes = $this->shipmentExtensionFactory->create();
  65. }
  66. try {
  67. /** @var \Temando\Shipping\Api\Data\Shipment\ShipmentReferenceInterface $extShipment */
  68. $extShipment = $this->shipmentReferenceRepository->getByShipmentId($shipment->getEntityId());
  69. $extensionAttributes->setExtShipmentId($extShipment->getExtShipmentId());
  70. $extensionAttributes->setExtReturnShipmentId($extShipment->getExtReturnShipmentId());
  71. $extensionAttributes->setExtLocationId($extShipment->getExtLocationId());
  72. $extensionAttributes->setExtTrackingUrl($extShipment->getExtTrackingUrl());
  73. $extensionAttributes->setExtTrackingReference($extShipment->getExtTrackingReference());
  74. } catch (LocalizedException $e) {
  75. $extensionAttributes->setExtShipmentId(null);
  76. $extensionAttributes->setExtLocationId(null);
  77. $extensionAttributes->setExtTrackingUrl(null);
  78. $extensionAttributes->setExtTrackingReference(null);
  79. }
  80. $shipment->setExtensionAttributes($extensionAttributes);
  81. return $shipment;
  82. }
  83. /**
  84. * When new shipments are created through the salesShipOrderV1 service,
  85. * additional data may be passed in as extension attributes, e.g. reference
  86. * IDs to external entities at the Temando platform.
  87. * These additional attributes need to be persisted in local storage.
  88. *
  89. * @see \Magento\Sales\Api\ShipOrderInterface::execute
  90. * @see ShipmentReferenceInterface
  91. *
  92. * @param ShipmentRepositoryInterface $subject
  93. * @param callable $proceed
  94. * @param ShipmentInterface|\Magento\Sales\Model\Order\Shipment $shipment
  95. * @return ShipmentInterface
  96. * @throws CouldNotSaveException
  97. */
  98. public function aroundSave(
  99. ShipmentRepositoryInterface $subject,
  100. callable $proceed,
  101. ShipmentInterface $shipment
  102. ) {
  103. $canSave = $shipment->isObjectNew();
  104. /** @var ShipmentInterface $shipment */
  105. $shipment = $proceed($shipment);
  106. $extensionAttributes = $shipment->getExtensionAttributes();
  107. $canSave = $canSave && $extensionAttributes && $extensionAttributes->getExtShipmentId();
  108. if ($canSave) {
  109. /** @var \Temando\Shipping\Api\Data\Shipment\ShipmentReferenceInterface $shipmentReference */
  110. $shipmentReference = $this->shipmentReferenceFactory->create();
  111. $shipmentReference->setShipmentId($shipment->getEntityId());
  112. $shipmentReference->setExtShipmentId($extensionAttributes->getExtShipmentId());
  113. $shipmentReference->setExtReturnShipmentId($extensionAttributes->getExtReturnShipmentId());
  114. $shipmentReference->setExtLocationId($extensionAttributes->getExtLocationId());
  115. $shipmentReference->setExtTrackingUrl($extensionAttributes->getExtTrackingUrl());
  116. $shipmentReference->setExtTrackingReference($extensionAttributes->getExtTrackingReference());
  117. $this->shipmentReferenceRepository->save($shipmentReference);
  118. }
  119. return $shipment;
  120. }
  121. }