StreamRepository.php 3.0 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\ResourceModel\EventStream;
  6. use Magento\Framework\Exception\CouldNotDeleteException;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. use Temando\Shipping\Rest\Adapter\EventStreamApiInterface;
  9. use Temando\Shipping\Rest\Exception\AdapterException;
  10. use Temando\Shipping\Rest\Request\ItemRequestInterfaceFactory;
  11. use Temando\Shipping\Rest\Request\StreamCreateRequestFactory;
  12. use Temando\Shipping\Rest\Request\Type\StreamRequestTypeFactory;
  13. /**
  14. * Temando Stream Repository
  15. *
  16. * @package Temando\Shipping\Model
  17. * @author Max Melzer <max.melzer@netresearch.de>
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link http://www.temando.com/
  20. */
  21. class StreamRepository implements StreamRepositoryInterface
  22. {
  23. /**
  24. * @var EventStreamApiInterface
  25. */
  26. private $apiAdapter;
  27. /**
  28. * @var StreamCreateRequestFactory
  29. */
  30. private $streamCreateRequestFactory;
  31. /**
  32. * @var StreamRequestTypeFactory
  33. */
  34. private $streamRequestTypeFactory;
  35. /**
  36. * @var ItemRequestInterfaceFactory
  37. */
  38. private $itemRequestFactory;
  39. /**
  40. * StreamRepository constructor.
  41. *
  42. * @param EventStreamApiInterface $apiAdapter
  43. * @param ItemRequestInterfaceFactory $itemRequestFactory
  44. * @param StreamRequestTypeFactory $streamRequuestTypeFactory
  45. * @param StreamCreateRequestFactory $streamCreateRequestFactory
  46. */
  47. public function __construct(
  48. EventStreamApiInterface $apiAdapter,
  49. ItemRequestInterfaceFactory $itemRequestFactory,
  50. StreamRequestTypeFactory $streamRequuestTypeFactory,
  51. StreamCreateRequestFactory $streamCreateRequestFactory
  52. ) {
  53. $this->apiAdapter = $apiAdapter;
  54. $this->itemRequestFactory = $itemRequestFactory;
  55. $this->streamRequestTypeFactory = $streamRequuestTypeFactory;
  56. $this->streamCreateRequestFactory = $streamCreateRequestFactory;
  57. }
  58. /**
  59. * @param string $streamId
  60. * @return void
  61. * @throws CouldNotSaveException
  62. */
  63. public function save($streamId)
  64. {
  65. try {
  66. $stream = $this->streamRequestTypeFactory->create(['streamId' => $streamId]);
  67. $request = $this->streamCreateRequestFactory->create(['stream' => $stream]);
  68. $this->apiAdapter->createStream($request);
  69. } catch (AdapterException $e) {
  70. throw new CouldNotSaveException(__('Unable to save event stream.'), $e);
  71. }
  72. }
  73. /**
  74. * @param string $streamId
  75. * @return void
  76. * @throws CouldNotDeleteException
  77. */
  78. public function delete($streamId)
  79. {
  80. try {
  81. $request = $this->itemRequestFactory->create(['entityId' => $streamId]);
  82. $this->apiAdapter->deleteStream($request);
  83. } catch (AdapterException $e) {
  84. throw new CouldNotDeleteException(__('Unable to delete event stream.'), $e);
  85. }
  86. }
  87. }