1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\Rpc;
use Magento\Framework\MessageQueue\PublisherInterface;
use Magento\Framework\MessageQueue\EnvelopeFactory;
use Magento\Framework\MessageQueue\ExchangeRepository;
use Magento\Framework\MessageQueue\MessageEncoder;
use Magento\Framework\MessageQueue\MessageValidator;
use Magento\Framework\MessageQueue\Publisher\ConfigInterface as PublisherConfig;
/**
* A MessageQueue Publisher to handle publishing a message.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Publisher implements PublisherInterface
{
/**
* @var ExchangeRepository
*/
private $exchangeRepository;
/**
* @var EnvelopeFactory
*/
private $envelopeFactory;
/**
* @var MessageEncoder
*/
private $messageEncoder;
/**
* @var MessageValidator
*/
private $messageValidator;
/**
* @var ResponseQueueNameBuilder
*/
private $responseQueueNameBuilder;
/**
* @var PublisherConfig
*/
private $publisherConfig;
//@codingStandardsIgnoreStart
/**
* Initialize dependencies.
*
* @param ExchangeRepository $exchangeRepository
* @param EnvelopeFactory $envelopeFactory
* @param null $messageQueueConfig @deprecated obsolete dependency
* @param null $amqpConfig @deprecated obsolete dependency
* @param MessageEncoder $messageEncoder
* @param MessageValidator $messageValidator
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
ExchangeRepository $exchangeRepository,
EnvelopeFactory $envelopeFactory,
$messageQueueConfig = null,
$amqpConfig = null,
MessageEncoder $messageEncoder,
MessageValidator $messageValidator
) {
$this->exchangeRepository = $exchangeRepository;
$this->envelopeFactory = $envelopeFactory;
$this->messageEncoder = $messageEncoder;
$this->messageValidator = $messageValidator;
}
//@codingStandardsIgnoreEnd
/**
* {@inheritdoc}
*/
public function publish($topicName, $data)
{
$this->messageValidator->validate($topicName, $data);
$data = $this->messageEncoder->encode($topicName, $data);
$replyTo = $this->getResponseQueueNameBuilder()->getQueueName($topicName);
$envelope = $this->envelopeFactory->create(
[
'body' => $data,
'properties' => [
'reply_to' => $replyTo,
'delivery_mode' => 2,
'correlation_id' => rand(),
'message_id' => md5(uniqid($topicName))
]
]
);
$connectionName = $this->getPublisherConfig()->getPublisher($topicName)->getConnection()->getName();
$exchange = $this->exchangeRepository->getByConnectionName($connectionName);
$responseMessage = $exchange->enqueue($topicName, $envelope);
return $this->messageEncoder->decode($topicName, $responseMessage, false);
}
/**
* Get response queue name builder.
*
* @return ResponseQueueNameBuilder
*
* @deprecated 102.0.1
*/
private function getResponseQueueNameBuilder()
{
if ($this->responseQueueNameBuilder === null) {
$this->responseQueueNameBuilder = \Magento\Framework\App\ObjectManager::getInstance()
->get(ResponseQueueNameBuilder::class);
}
return $this->responseQueueNameBuilder;
}
/**
* Get publisher config.
*
* @return PublisherConfig
*
* @deprecated 102.0.1
*/
private function getPublisherConfig()
{
if ($this->publisherConfig === null) {
$this->publisherConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(PublisherConfig::class);
}
return $this->publisherConfig;
}
}