Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Publisher.php
Go to the documentation of this file.
1 <?php
7 
14 
20 class Publisher implements PublisherInterface
21 {
25  private $exchangeRepository;
26 
30  private $envelopeFactory;
31 
35  private $messageEncoder;
36 
40  private $messageValidator;
41 
45  private $responseQueueNameBuilder;
46 
50  private $publisherConfig;
51 
52  //@codingStandardsIgnoreStart
65  public function __construct(
66  ExchangeRepository $exchangeRepository,
67  EnvelopeFactory $envelopeFactory,
68  $messageQueueConfig = null,
69  $amqpConfig = null,
70  MessageEncoder $messageEncoder,
71  MessageValidator $messageValidator
72  ) {
73  $this->exchangeRepository = $exchangeRepository;
74  $this->envelopeFactory = $envelopeFactory;
75  $this->messageEncoder = $messageEncoder;
76  $this->messageValidator = $messageValidator;
77  }
78  //@codingStandardsIgnoreEnd
79 
83  public function publish($topicName, $data)
84  {
85  $this->messageValidator->validate($topicName, $data);
86  $data = $this->messageEncoder->encode($topicName, $data);
87  $replyTo = $this->getResponseQueueNameBuilder()->getQueueName($topicName);
88  $envelope = $this->envelopeFactory->create(
89  [
90  'body' => $data,
91  'properties' => [
92  'reply_to' => $replyTo,
93  'delivery_mode' => 2,
94  'correlation_id' => rand(),
95  'message_id' => md5(uniqid($topicName))
96  ]
97  ]
98  );
99  $connectionName = $this->getPublisherConfig()->getPublisher($topicName)->getConnection()->getName();
100  $exchange = $this->exchangeRepository->getByConnectionName($connectionName);
101  $responseMessage = $exchange->enqueue($topicName, $envelope);
102  return $this->messageEncoder->decode($topicName, $responseMessage, false);
103  }
104 
112  private function getResponseQueueNameBuilder()
113  {
114  if ($this->responseQueueNameBuilder === null) {
115  $this->responseQueueNameBuilder = \Magento\Framework\App\ObjectManager::getInstance()
116  ->get(ResponseQueueNameBuilder::class);
117  }
118  return $this->responseQueueNameBuilder;
119  }
120 
128  private function getPublisherConfig()
129  {
130  if ($this->publisherConfig === null) {
131  $this->publisherConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(PublisherConfig::class);
132  }
133  return $this->publisherConfig;
134  }
135 }
__construct(ExchangeRepository $exchangeRepository, EnvelopeFactory $envelopeFactory, $messageQueueConfig=null, $amqpConfig=null, MessageEncoder $messageEncoder, MessageValidator $messageValidator)
Definition: Publisher.php:65