UpdateShipmentStatus.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Sales\Service\Operation;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Magento\Sales\Api\ShipmentRepositoryInterface;
  8. use Temando\Shipping\Api\Shipment\ShipmentStatusInterface;
  9. use Temando\Shipping\Model\ShipmentInterface;
  10. /**
  11. * Temando Shipment Operation: Update Status.
  12. *
  13. * @package Temando\Shipping\Model
  14. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. */
  18. class UpdateShipmentStatus implements ShipmentOperationInterface
  19. {
  20. /**
  21. * @var ShipmentStatusInterface
  22. */
  23. private $shipmentStatus;
  24. /**
  25. * @var ShipmentRepositoryInterface
  26. */
  27. private $shipmentRepository;
  28. /**
  29. * SyncShipmentStatus constructor.
  30. * @param ShipmentStatusInterface $shipmentStatus
  31. * @param ShipmentRepositoryInterface $shipmentRepository
  32. */
  33. public function __construct(
  34. ShipmentStatusInterface $shipmentStatus,
  35. ShipmentRepositoryInterface $shipmentRepository
  36. ) {
  37. $this->shipmentStatus = $shipmentStatus;
  38. $this->shipmentRepository = $shipmentRepository;
  39. }
  40. /**
  41. * Update local shipment status if platform status differs.
  42. *
  43. * @param ShipmentInterface $shipment
  44. * @param int $salesShipmentId
  45. * @throws LocalizedException
  46. */
  47. public function execute(ShipmentInterface $shipment, int $salesShipmentId): void
  48. {
  49. $status = $this->shipmentStatus->getStatusCode($shipment->getStatus());
  50. if (!$status) {
  51. // unknown status, cannot update
  52. return;
  53. }
  54. $salesShipment = $this->shipmentRepository->get($salesShipmentId);
  55. if ($salesShipment->getShipmentStatus() == $status) {
  56. // status in sync, nothing to do
  57. return;
  58. }
  59. $salesShipment->setShipmentStatus($status);
  60. $this->shipmentRepository->save($salesShipment);
  61. }
  62. }