Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConsumerFactory.php
Go to the documentation of this file.
1 <?php
7 
15 use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
16 
22 {
28  private $objectManager = null;
29 
33  private $consumerConfig;
34 
38  private $communicationConfig;
39 
48  public function __construct(
49  QueueConfig $queueConfig,
50  ObjectManagerInterface $objectManager
51  ) {
52  $this->objectManager = $objectManager;
53  }
54 
63  public function get($consumerName, $batchSize = 0)
64  {
65  $consumerConfig = $this->getConsumerConfig()->getConsumer($consumerName);
66  if ($consumerConfig === null) {
67  throw new LocalizedException(
68  new Phrase('Specified consumer "%consumer" is not declared.', ['consumer' => $consumerName])
69  );
70  }
71 
72  return $this->objectManager->create(
73  $consumerConfig->getConsumerInstance(),
74  [
75  'configuration' => $this->createConsumerConfiguration($consumerConfig),
76  'batchSize' => $batchSize,
77  ]
78  );
79  }
80 
87  private function createConsumerConfiguration($consumerConfigItem)
88  {
89  $customConsumerHandlers = [];
90  foreach ($consumerConfigItem->getHandlers() as $handlerConfig) {
91  $customConsumerHandlers[] = [
92  $this->objectManager->create($handlerConfig->getType()),
93  $handlerConfig->getMethod()
94  ];
95  }
96  $topics = [];
97  foreach ($this->getCommunicationConfig()->getTopics() as $topicConfig) {
98  $topicName = $topicConfig[CommunicationConfig::TOPIC_NAME];
99  $topics[$topicName] = [
100  ConsumerConfigurationInterface::TOPIC_HANDLERS => $customConsumerHandlers
101  ?: $this->getHandlersFromCommunicationConfig($topicName),
102  ConsumerConfigurationInterface::TOPIC_TYPE => $topicConfig[CommunicationConfig::TOPIC_IS_SYNCHRONOUS]
105  ];
106  }
107  $configData = [
108  ConsumerConfigurationInterface::CONSUMER_NAME => $consumerConfigItem->getName(),
109  ConsumerConfigurationInterface::QUEUE_NAME => $consumerConfigItem->getQueue(),
111  ConsumerConfigurationInterface::MAX_MESSAGES => $consumerConfigItem->getMaxMessages(),
112  ];
113 
114  return $this->objectManager->create(
115  \Magento\Framework\MessageQueue\ConsumerConfiguration::class,
116  ['data' => $configData]
117  );
118  }
119 
127  private function getConsumerConfig()
128  {
129  if ($this->consumerConfig === null) {
130  $this->consumerConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(ConsumerConfig::class);
131  }
132  return $this->consumerConfig;
133  }
134 
142  private function getCommunicationConfig()
143  {
144  if ($this->communicationConfig === null) {
145  $this->communicationConfig = \Magento\Framework\App\ObjectManager::getInstance()
146  ->get(CommunicationConfig::class);
147  }
148  return $this->communicationConfig;
149  }
150 
157  private function getHandlersFromCommunicationConfig($topicName)
158  {
159  $topicConfig = $this->getCommunicationConfig()->getTopic($topicName);
160  $handlers = [];
161  foreach ($topicConfig[CommunicationConfig::TOPIC_HANDLERS] as $handlerConfig) {
162  $handlers[] = [
163  $this->objectManager->create($handlerConfig[CommunicationConfig::HANDLER_TYPE]),
164  $handlerConfig[CommunicationConfig::HANDLER_METHOD]
165  ];
166  }
167  return $handlers;
168  }
169 }
$objectManager
Definition: bootstrap.php:17
__construct(QueueConfig $queueConfig, ObjectManagerInterface $objectManager)