ShipmentDocumentFactoryPlugin.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\FileSystemException;
  7. use Magento\Framework\Filesystem\Driver\Https as HttpsDownloader;
  8. use Magento\Sales\Api\Data\OrderInterface;
  9. use Magento\Sales\Api\Data\ShipmentCommentCreationInterface;
  10. use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface;
  11. use Magento\Sales\Api\Data\ShipmentExtensionFactory;
  12. use Magento\Sales\Api\Data\ShipmentInterface;
  13. use Magento\Sales\Model\Order\ShipmentDocumentFactory;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * ShipmentDocumentFactoryPlugin
  17. *
  18. * @package Temando\Shipping\Plugin
  19. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  20. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  21. * @link https://www.temando.com/
  22. */
  23. class ShipmentDocumentFactoryPlugin
  24. {
  25. /**
  26. * @var ShipmentExtensionFactory
  27. */
  28. private $shipmentExtensionFactory;
  29. /**
  30. * @var HttpsDownloader
  31. */
  32. private $downloader;
  33. /**
  34. * @var LoggerInterface
  35. */
  36. private $logger;
  37. /**
  38. * ShipmentDocumentFactoryPlugin constructor.
  39. * @param ShipmentExtensionFactory $extensionFactory
  40. * @param HttpsDownloader $downloader
  41. * @param LoggerInterface $logger
  42. */
  43. public function __construct(
  44. ShipmentExtensionFactory $extensionFactory,
  45. HttpsDownloader $downloader,
  46. LoggerInterface $logger
  47. ) {
  48. $this->shipmentExtensionFactory = $extensionFactory;
  49. $this->downloader = $downloader;
  50. $this->logger = $logger;
  51. }
  52. /**
  53. * The salesShipOrderV1 service allows to add extension attributes within
  54. * the "arguments" object. However, the ShipmentDocumentFactory does not
  55. * process it.
  56. * We need to add the extension attributes to the shipment entity ourselves.
  57. * Additionally, label contents are fetched from the given download URL.
  58. *
  59. * @see \Magento\Sales\Api\ShipOrderInterface::execute
  60. * @see \Magento\Sales\Model\Order\ShipmentDocumentFactory::create()
  61. *
  62. * @param ShipmentDocumentFactory $subject
  63. * @param callable $proceed
  64. * @param OrderInterface $order
  65. * @param array $items
  66. * @param array $tracks
  67. * @param ShipmentCommentCreationInterface|null $comment
  68. * @param bool $appendComment
  69. * @param array $packages
  70. * @param ShipmentCreationArgumentsInterface|null $arguments
  71. * @return ShipmentInterface
  72. */
  73. public function aroundCreate(
  74. ShipmentDocumentFactory $subject,
  75. callable $proceed,
  76. OrderInterface $order,
  77. array $items = [],
  78. array $tracks = [],
  79. ShipmentCommentCreationInterface $comment = null,
  80. $appendComment = false,
  81. array $packages = [],
  82. ShipmentCreationArgumentsInterface $arguments = null
  83. ) {
  84. /** @var ShipmentInterface $shipment */
  85. $shipment = $proceed($order, $items, $tracks, $comment, $appendComment, $packages, $arguments);
  86. if (!$arguments) {
  87. // no shipment creation arguments available
  88. return $shipment;
  89. }
  90. if (!$shipment->getExtensionAttributes()) {
  91. // extension arguments not initialized yet
  92. $extensionAttributes = $this->shipmentExtensionFactory->create();
  93. $shipment->setExtensionAttributes($extensionAttributes);
  94. }
  95. // shift external shipment id to shipment
  96. $extShipmentId = $arguments->getExtensionAttributes()->getExtShipmentId();
  97. if ($extShipmentId) {
  98. $shipment->getExtensionAttributes()->setExtShipmentId($extShipmentId);
  99. }
  100. // shift external return shipment id to shipment
  101. $extReturnShipmentId = $arguments->getExtensionAttributes()->getExtReturnShipmentId();
  102. if ($extReturnShipmentId) {
  103. $shipment->getExtensionAttributes()->setExtReturnShipmentId($extReturnShipmentId);
  104. }
  105. // shift external location id to shipment
  106. $extLocationId = $arguments->getExtensionAttributes()->getExtLocationId();
  107. if ($extLocationId) {
  108. $shipment->getExtensionAttributes()->setExtLocationId($extLocationId);
  109. }
  110. // shift external tracking url to shipment
  111. $extTrackingUrl = $arguments->getExtensionAttributes()->getExtTrackingUrl();
  112. if ($extTrackingUrl) {
  113. $shipment->getExtensionAttributes()->setExtTrackingUrl($extTrackingUrl);
  114. }
  115. // shift external tracking reference to shipment
  116. $extTrackingReference = $arguments->getExtensionAttributes()->getExtTrackingReference();
  117. if ($extTrackingReference) {
  118. $shipment->getExtensionAttributes()->setExtTrackingReference($extTrackingReference);
  119. }
  120. // download label and attach to shipment
  121. $count = 0;
  122. $labelUri = $arguments->getExtensionAttributes()->getShippingLabel();
  123. $labelUri = preg_replace('#^https://#', '', $labelUri, 1, $count);
  124. if ($count) {
  125. try {
  126. $labelContent = $this->downloader->fileGetContents($labelUri);
  127. } catch (FileSystemException $exception) {
  128. $this->logger->critical('Shipping label download failed', ['exception' => $exception]);
  129. $labelContent = '';
  130. }
  131. $shipment->setShippingLabel($labelContent);
  132. }
  133. return $shipment;
  134. }
  135. }