ShipmentService.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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;
  6. use Magento\Framework\Exception\CouldNotDeleteException;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Temando\Shipping\Model\ResourceModel\Repository\ShipmentRepositoryInterface;
  10. use Temando\Shipping\Model\ShipmentInterface;
  11. /**
  12. * Temando Shipment Service.
  13. *
  14. * The shipment service allows to execute additional operations after
  15. * performing CRUD operations at the platform.
  16. *
  17. * @package Temando\Shipping\Model
  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 ShipmentService
  23. {
  24. const OPERATION_CREATE = 'create';
  25. const OPERATION_READ = 'read';
  26. const OPERATION_UPDATE = 'update';
  27. const OPERATION_CANCEL = 'cancel';
  28. /**
  29. * @var ShipmentRepositoryInterface
  30. */
  31. private $shipmentRepository;
  32. /**
  33. * @var ShipmentOperationPool
  34. */
  35. private $operationPool;
  36. /**
  37. * ShipmentService constructor.
  38. * @param ShipmentRepositoryInterface $shipmentRepository
  39. * @param ShipmentOperationPool $operationPool
  40. */
  41. public function __construct(
  42. ShipmentRepositoryInterface $shipmentRepository,
  43. ShipmentOperationPool $operationPool
  44. ) {
  45. $this->shipmentRepository = $shipmentRepository;
  46. $this->operationPool = $operationPool;
  47. }
  48. /**
  49. * Cancel shipment at the platform, execute additional operations on local shipment entity.
  50. *
  51. * @param string $shipmentId
  52. * @param int $salesShipmentId
  53. * @return ShipmentInterface
  54. * @throws CouldNotDeleteException
  55. * @throws LocalizedException
  56. */
  57. public function cancel(string $shipmentId, int $salesShipmentId): ShipmentInterface
  58. {
  59. $shipment = $this->shipmentRepository->cancel($shipmentId);
  60. $cancelOperations = $this->operationPool->get(self::OPERATION_CANCEL);
  61. foreach ($cancelOperations as $operation) {
  62. $operation->execute($shipment, $salesShipmentId);
  63. }
  64. return $shipment;
  65. }
  66. /**
  67. * Read shipment form the platform, execute additional operations on local shipment entity.
  68. *
  69. * @param string $shipmentId
  70. * @param int $salesShipmentId
  71. * @return ShipmentInterface
  72. * @throws NoSuchEntityException
  73. * @throws LocalizedException
  74. */
  75. public function read(string $shipmentId, int $salesShipmentId): ShipmentInterface
  76. {
  77. $shipment = $this->shipmentRepository->getById($shipmentId);
  78. $readOperations = $this->operationPool->get(self::OPERATION_READ);
  79. foreach ($readOperations as $operation) {
  80. $operation->execute($shipment, $salesShipmentId);
  81. }
  82. return $shipment;
  83. }
  84. }