Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Exchange.php
Go to the documentation of this file.
1 <?php
7 
9 use PhpAmqpLib\Message\AMQPMessage;
10 use Magento\Framework\Communication\ConfigInterface as CommunicationConfigInterface;
12 
16 class Exchange implements ExchangeInterface
17 {
21  private $amqpConfig;
22 
26  private $communicationConfig;
27 
31  private $publisherConfig;
32 
36  private $exchange;
37 
46  public function __construct(
47  \Magento\Framework\Amqp\Config $amqpConfig,
48  PublisherConfig $publisherConfig,
49  CommunicationConfigInterface $communicationConfig,
50  \Magento\Framework\Amqp\Exchange $exchange
51  ) {
52  $this->amqpConfig = $amqpConfig;
53  $this->communicationConfig = $communicationConfig;
54  $this->publisherConfig = $publisherConfig;
55  $this->exchange = $exchange;
56  }
57 
61  public function enqueue($topic, array $envelopes)
62  {
63  $topicData = $this->communicationConfig->getTopic($topic);
64  $isSync = $topicData[CommunicationConfigInterface::TOPIC_IS_SYNCHRONOUS];
65 
66  if ($isSync) {
67  $responses = [];
68  foreach ($envelopes as $envelope) {
69  $responses[] = $this->exchange->enqueue($topic, $envelope);
70  }
71  return $responses;
72  }
73 
74  $channel = $this->amqpConfig->getChannel();
75  $publisher = $this->publisherConfig->getPublisher($topic);
76  $exchange = $publisher->getConnection()->getExchange();
77 
78  foreach ($envelopes as $envelope) {
79  $msg = new AMQPMessage($envelope->getBody(), $envelope->getProperties());
80  $channel->batch_basic_publish($msg, $exchange, $topic);
81  }
82  $channel->publish_batch();
83 
84  return null;
85  }
86 }
enqueue($topic, array $envelopes)
Definition: Exchange.php:61
__construct(\Magento\Framework\Amqp\Config $amqpConfig, PublisherConfig $publisherConfig, CommunicationConfigInterface $communicationConfig, \Magento\Framework\Amqp\Exchange $exchange)
Definition: Exchange.php:46