ExchangeFactory.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue;
  7. /**
  8. * Factory class for @see \Magento\Framework\MessageQueue\ExchangeInterface
  9. *
  10. * @api
  11. * @since 102.0.1
  12. */
  13. class ExchangeFactory implements ExchangeFactoryInterface
  14. {
  15. /**
  16. * @var ExchangeFactoryInterface[]
  17. */
  18. private $exchangeFactories;
  19. /**
  20. * @var ConnectionTypeResolver
  21. */
  22. private $connectionTypeResolver;
  23. /**
  24. * Object Manager instance
  25. *
  26. * @var \Magento\Framework\ObjectManagerInterface
  27. * @since 102.0.1
  28. */
  29. protected $objectManager = null;
  30. /**
  31. * Initialize dependencies.
  32. *
  33. * @param ConnectionTypeResolver $connectionTypeResolver
  34. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  35. * @param ExchangeFactoryInterface[] $exchangeFactories
  36. */
  37. public function __construct(
  38. ConnectionTypeResolver $connectionTypeResolver,
  39. \Magento\Framework\ObjectManagerInterface $objectManager,
  40. array $exchangeFactories = []
  41. ) {
  42. $this->objectManager = $objectManager;
  43. $this->exchangeFactories = $exchangeFactories;
  44. $this->connectionTypeResolver = $connectionTypeResolver;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. * @since 102.0.1
  49. */
  50. public function create($connectionName, array $data = [])
  51. {
  52. $connectionType = $this->connectionTypeResolver->getConnectionType($connectionName);
  53. if (!isset($this->exchangeFactories[$connectionType])) {
  54. throw new \LogicException("Not found exchange for connection name '{$connectionName}' in config");
  55. }
  56. $factory = $this->exchangeFactories[$connectionType];
  57. $exchange = $factory->create($connectionName, $data);
  58. if (!$exchange instanceof ExchangeInterface) {
  59. $exchangeInterface = \Magento\Framework\MessageQueue\ExchangeInterface::class;
  60. throw new \LogicException(
  61. "Exchange for connection name '{$connectionName}' " .
  62. "does not implement interface '{$exchangeInterface}'"
  63. );
  64. }
  65. return $exchange;
  66. }
  67. }