DispatchRepository.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Dispatch;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Temando\Shipping\Model\DispatchInterface;
  9. use Temando\Shipping\Model\ResourceModel\Repository\DispatchRepositoryInterface;
  10. use Temando\Shipping\Rest\Adapter\CompletionApiInterface;
  11. use Temando\Shipping\Rest\EntityMapper\DispatchResponseMapper;
  12. use Temando\Shipping\Rest\Exception\AdapterException;
  13. use Temando\Shipping\Rest\Request\ItemRequestInterfaceFactory;
  14. /**
  15. * Temando Dispatch Repository
  16. *
  17. * @package Temando\Shipping\Model
  18. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  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 DispatchRepository implements DispatchRepositoryInterface
  24. {
  25. /**
  26. * @var CompletionApiInterface
  27. */
  28. private $apiAdapter;
  29. /**
  30. * @var ItemRequestInterfaceFactory
  31. */
  32. private $completionRequestFactory;
  33. /**
  34. * @var DispatchResponseMapper
  35. */
  36. private $dispatchMapper;
  37. /**
  38. * DispatchRepository constructor.
  39. * @param CompletionApiInterface $apiAdapter
  40. * @param ItemRequestInterfaceFactory $completionRequestFactory
  41. * @param DispatchResponseMapper $dispatchMapper
  42. */
  43. public function __construct(
  44. CompletionApiInterface $apiAdapter,
  45. ItemRequestInterfaceFactory $completionRequestFactory,
  46. DispatchResponseMapper $dispatchMapper
  47. ) {
  48. $this->apiAdapter = $apiAdapter;
  49. $this->completionRequestFactory = $completionRequestFactory;
  50. $this->dispatchMapper = $dispatchMapper;
  51. }
  52. /**
  53. * @param string $dispatchId
  54. * @return DispatchInterface
  55. * @throws NoSuchEntityException
  56. * @throws LocalizedException
  57. */
  58. public function getById($dispatchId)
  59. {
  60. if (!$dispatchId) {
  61. throw new LocalizedException(__('An error occurred while loading data.'));
  62. }
  63. try {
  64. $request = $this->completionRequestFactory->create(['entityId' => $dispatchId]);
  65. $apiCompletion = $this->apiAdapter->getCompletion($request);
  66. $dispatch = $this->dispatchMapper->map($apiCompletion);
  67. } catch (AdapterException $e) {
  68. if ($e->getCode() === 404) {
  69. throw NoSuchEntityException::singleField('completionId', $dispatchId);
  70. }
  71. throw new LocalizedException(__('An error occurred while loading data.'), $e);
  72. }
  73. return $dispatch;
  74. }
  75. }