EventRepository.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\EventStream;
  6. use Magento\Framework\Exception\CouldNotDeleteException;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Temando\Shipping\Rest\Adapter\EventStreamApiInterface;
  9. use Temando\Shipping\Rest\EntityMapper\StreamEventResponseMapper;
  10. use Temando\Shipping\Rest\Exception\AdapterException;
  11. use Temando\Shipping\Rest\Request\ListRequestInterfaceFactory;
  12. use Temando\Shipping\Rest\Request\StreamEventItemRequestFactory;
  13. use Temando\Shipping\Rest\Response\DataObject\StreamEvent;
  14. use Temando\Shipping\Webservice\Pagination\PaginationFactory;
  15. /**
  16. * Temando Event Stream Repository
  17. *
  18. * @package Temando\Shipping\Model
  19. * @author Benjamin Heuer <benjamin.heuer@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 EventRepository implements EventRepositoryInterface
  24. {
  25. /**
  26. * @var EventStreamApiInterface
  27. */
  28. private $apiAdapter;
  29. /**
  30. * @var PaginationFactory
  31. */
  32. private $paginationFactory;
  33. /**
  34. * @var ListRequestInterfaceFactory
  35. */
  36. private $listRequestFactory;
  37. /**
  38. * @var StreamEventItemRequestFactory
  39. */
  40. private $itemRequestFactory;
  41. /**
  42. * @var StreamEventResponseMapper
  43. */
  44. private $streamEventMapper;
  45. /**
  46. * StreamEventRepository constructor.
  47. *
  48. * @param EventStreamApiInterface $apiAdapter
  49. * @param StreamEventItemRequestFactory $itemRequestFactory
  50. * @param ListRequestInterfaceFactory $listRequestFactory
  51. * @param StreamEventResponseMapper $streamEventMapper
  52. */
  53. public function __construct(
  54. EventStreamApiInterface $apiAdapter,
  55. StreamEventItemRequestFactory $itemRequestFactory,
  56. ListRequestInterfaceFactory $listRequestFactory,
  57. StreamEventResponseMapper $streamEventMapper
  58. ) {
  59. $this->apiAdapter = $apiAdapter;
  60. $this->itemRequestFactory = $itemRequestFactory;
  61. $this->listRequestFactory = $listRequestFactory;
  62. $this->streamEventMapper = $streamEventMapper;
  63. }
  64. /**
  65. * @param string $streamId
  66. * @param int|null $offset
  67. * @param int|null $limit
  68. *
  69. * @return \Temando\Shipping\Model\StreamEventInterface[]
  70. * @throws LocalizedException
  71. */
  72. public function getEventList($streamId, $offset = null, $limit = null)
  73. {
  74. try {
  75. $pagination = $this->paginationFactory->create([
  76. 'offset' => $offset,
  77. 'limit' => $limit,
  78. ]);
  79. $request = $this->listRequestFactory->create([
  80. 'parentId' => $streamId,
  81. 'pagination' => $pagination,
  82. ]);
  83. // convert api response to local (reduced) event objects
  84. $apiStreamEvents = $this->apiAdapter->getStreamEvents($request);
  85. $streamEvents = array_map(function (StreamEvent $apiEvent) {
  86. return $this->streamEventMapper->map($apiEvent);
  87. }, $apiStreamEvents);
  88. } catch (AdapterException $e) {
  89. throw new LocalizedException(__('Unable to load stream events.'), $e);
  90. }
  91. return $streamEvents;
  92. }
  93. /**
  94. * @param string $streamId
  95. * @param string $eventId
  96. *
  97. * @return void
  98. * @throws CouldNotDeleteException
  99. */
  100. public function delete($streamId, $eventId)
  101. {
  102. try {
  103. $request = $this->itemRequestFactory->create([
  104. 'streamId' => $streamId,
  105. 'entityId' => $eventId,
  106. ]);
  107. $this->apiAdapter->deleteStreamEvent($request);
  108. } catch (AdapterException $e) {
  109. throw new CouldNotDeleteException(__('Unable to delete stream event.'), $e);
  110. }
  111. }
  112. }