ExchangeRepository.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue;
  7. class ExchangeRepository
  8. {
  9. /**
  10. * @var \Magento\Framework\ObjectManagerInterface
  11. */
  12. private $objectManager;
  13. /**
  14. * @var ExchangeFactoryInterface
  15. */
  16. private $exchangeFactory;
  17. /**
  18. * Pool of exchange instances.
  19. *
  20. * @var ExchangeInterface[]
  21. */
  22. private $exchangePool = [];
  23. /**
  24. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  25. * @param string[] $exchanges
  26. *
  27. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  28. */
  29. public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, array $exchanges = [])
  30. {
  31. $this->objectManager = $objectManager;
  32. }
  33. /**
  34. * @param string $connectionName
  35. * @return ExchangeInterface
  36. * @throws \LogicException
  37. */
  38. public function getByConnectionName($connectionName)
  39. {
  40. if (!isset($this->exchangePool[$connectionName])) {
  41. $exchange = $this->getExchangeFactory()->create($connectionName);
  42. $this->exchangePool[$connectionName] = $exchange;
  43. }
  44. return $this->exchangePool[$connectionName];
  45. }
  46. /**
  47. * Get exchange factory.
  48. *
  49. * @return ExchangeFactoryInterface
  50. * @deprecated 102.0.1
  51. */
  52. private function getExchangeFactory()
  53. {
  54. if ($this->exchangeFactory === null) {
  55. $this->exchangeFactory = $this->objectManager->get(ExchangeFactoryInterface::class);
  56. }
  57. return $this->exchangeFactory;
  58. }
  59. }