ShippingLabelConverter.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Sales\Plugin;
  8. /**
  9. * Plugin to convert shipping label from blob to base64encoded string
  10. */
  11. class ShippingLabelConverter
  12. {
  13. /**
  14. * Convert shipping label from blob to base64encoded string
  15. *
  16. * @param \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository
  17. * @param \Magento\Sales\Api\Data\ShipmentSearchResultInterface $searchResult
  18. * @return \Magento\Sales\Api\Data\ShipmentSearchResultInterface
  19. *
  20. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  21. */
  22. public function afterGetList(
  23. \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository,
  24. \Magento\Sales\Api\Data\ShipmentSearchResultInterface $searchResult
  25. ) {
  26. /** @var \Magento\Sales\Model\Order\Shipment $item */
  27. foreach ($searchResult->getItems() as $item) {
  28. if ($item->getShippingLabel() !== null) {
  29. $item->setShippingLabel(base64_encode($item->getShippingLabel()));
  30. }
  31. }
  32. return $searchResult;
  33. }
  34. /**
  35. * Convert shipping label from blob to base64encoded string
  36. *
  37. * @param \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository
  38. * @param \Magento\Sales\Api\Data\ShipmentInterface $shipment
  39. * @return \Magento\Sales\Api\Data\ShipmentInterface
  40. *
  41. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  42. */
  43. public function afterGet(
  44. \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository,
  45. \Magento\Sales\Api\Data\ShipmentInterface $shipment
  46. ) {
  47. if ($shipment->getShippingLabel() !== null) {
  48. $shipment->setShippingLabel(base64_encode($shipment->getShippingLabel()));
  49. }
  50. return $shipment;
  51. }
  52. }