ExchangeRepository.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\Bulk;
  7. /**
  8. * Used to get exchange instance from the pool.
  9. */
  10. class ExchangeRepository
  11. {
  12. /**
  13. * @var ExchangeFactoryInterface
  14. */
  15. private $exchangeFactory;
  16. /**
  17. * Pool of exchange instances.
  18. *
  19. * @var ExchangeInterface[]
  20. */
  21. private $exchangePool = [];
  22. /**
  23. * @param ExchangeFactoryInterface $exchangeFactory
  24. */
  25. public function __construct(ExchangeFactoryInterface $exchangeFactory)
  26. {
  27. $this->exchangeFactory = $exchangeFactory;
  28. }
  29. /**
  30. * Get exchange from the pool for the specified connection type.
  31. *
  32. * @param string $connectionName
  33. * @return ExchangeInterface
  34. * @throws \LogicException
  35. */
  36. public function getByConnectionName($connectionName)
  37. {
  38. if (!isset($this->exchangePool[$connectionName])) {
  39. $exchange = $this->exchangeFactory->create($connectionName);
  40. $this->exchangePool[$connectionName] = $exchange;
  41. }
  42. return $this->exchangePool[$connectionName];
  43. }
  44. }