ShipmentRepository.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\ResourceModel\Shipment;
  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. use Temando\Shipping\Rest\Adapter\ShipmentApiInterface;
  12. use Temando\Shipping\Rest\EntityMapper\ShipmentResponseMapper;
  13. use Temando\Shipping\Rest\Exception\AdapterException;
  14. use Temando\Shipping\Rest\Request\ItemRequestInterfaceFactory;
  15. /**
  16. * Temando Shipment Repository
  17. *
  18. * @package Temando\Shipping\Model
  19. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  20. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  21. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link https://www.temando.com/
  23. */
  24. class ShipmentRepository implements ShipmentRepositoryInterface
  25. {
  26. /**
  27. * @var ShipmentApiInterface
  28. */
  29. private $apiAdapter;
  30. /**
  31. * @var ItemRequestInterfaceFactory
  32. */
  33. private $requestFactory;
  34. /**
  35. * @var ShipmentResponseMapper
  36. */
  37. private $shipmentMapper;
  38. /**
  39. * ShipmentRepository constructor.
  40. * @param ShipmentApiInterface $apiAdapter
  41. * @param ItemRequestInterfaceFactory $requestFactory
  42. * @param ShipmentResponseMapper $shipmentMapper
  43. */
  44. public function __construct(
  45. ShipmentApiInterface $apiAdapter,
  46. ItemRequestInterfaceFactory $requestFactory,
  47. ShipmentResponseMapper $shipmentMapper
  48. ) {
  49. $this->apiAdapter = $apiAdapter;
  50. $this->requestFactory = $requestFactory;
  51. $this->shipmentMapper = $shipmentMapper;
  52. }
  53. /**
  54. * Load external shipment entity from platform.
  55. *
  56. * @param string $shipmentId
  57. * @return ShipmentInterface
  58. * @throws NoSuchEntityException
  59. * @throws LocalizedException
  60. */
  61. public function getById(string $shipmentId): ShipmentInterface
  62. {
  63. if (!$shipmentId) {
  64. throw new LocalizedException(__('An error occurred while loading data.'));
  65. }
  66. try {
  67. $request = $this->requestFactory->create(['entityId' => $shipmentId]);
  68. $apiShipment = $this->apiAdapter->getShipment($request);
  69. $shipment = $this->shipmentMapper->map($apiShipment);
  70. } catch (AdapterException $e) {
  71. if ($e->getCode() === 404) {
  72. throw NoSuchEntityException::singleField('shipmentId', $shipmentId);
  73. }
  74. throw new LocalizedException(__('An error occurred while loading data.'), $e);
  75. }
  76. return $shipment;
  77. }
  78. /**
  79. * Cancel external shipment at the platform.
  80. *
  81. * @param string $shipmentId
  82. * @return ShipmentInterface
  83. * @throws CouldNotDeleteException
  84. */
  85. public function cancel(string $shipmentId): ShipmentInterface
  86. {
  87. try {
  88. $request = $this->requestFactory->create(['entityId' => $shipmentId]);
  89. $apiShipment = $this->apiAdapter->cancelShipment($request);
  90. $shipment = $this->shipmentMapper->map($apiShipment);
  91. } catch (AdapterException $e) {
  92. throw new CouldNotDeleteException(__('Unable to cancel shipment: %1.', $e->getMessage()), $e);
  93. }
  94. return $shipment;
  95. }
  96. }