1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ozean12\GooglePubSubBundle\Service\Publisher; |
4
|
|
|
|
5
|
|
|
use Google\Cloud\Core\Exception\ConflictException; |
6
|
|
|
use Google\Cloud\PubSub\PubSubClient; |
7
|
|
|
use Google\Cloud\PubSub\Topic; |
8
|
|
|
use JMS\Serializer\Serializer; |
9
|
|
|
use JMS\Serializer\SerializationContext; |
10
|
|
|
use Ozean12\GooglePubSubBundle\DTO\MessageDataDTOInterface; |
11
|
|
|
use Ozean12\GooglePubSubBundle\DTO\PublishMessageResultDTO; |
12
|
|
|
use Ozean12\GooglePubSubBundle\Service\AbstractClient; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Publisher |
16
|
|
|
*/ |
17
|
|
|
class Publisher extends AbstractClient |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Topic |
21
|
|
|
*/ |
22
|
|
|
private $topic; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $topicName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Publisher constructor. |
31
|
|
|
* |
32
|
|
|
* @param string $topic |
33
|
|
|
* @param PubSubClient $client |
34
|
|
|
* @param Serializer $serializer |
35
|
|
|
* @param string $suffix |
36
|
|
|
*/ |
37
|
|
|
public function __construct($topic, PubSubClient $client, Serializer $serializer, string $suffix = '') |
38
|
|
|
{ |
39
|
|
|
parent::__construct($client, $serializer); |
40
|
|
|
|
41
|
|
|
$this->topicName = $topic.$suffix; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param MessageDataDTOInterface $data |
46
|
|
|
* @param array $attributes |
47
|
|
|
* @param array $options |
48
|
|
|
* @param array $publisherOptions |
49
|
|
|
* @return PublishMessageResultDTO |
50
|
|
|
*/ |
51
|
|
|
public function publish( |
52
|
|
|
MessageDataDTOInterface $data, |
53
|
|
|
array $attributes = [], |
54
|
|
|
$options = [], |
55
|
|
|
$publisherOptions = [] |
56
|
|
|
) { |
57
|
|
|
$this->setupTopic(); |
58
|
|
|
|
59
|
|
|
$context = (new SerializationContext())->setSerializeNull(true); |
60
|
|
|
if (!empty($publisherOptions['serializationGroups'])) { |
61
|
|
|
$context->setGroups($publisherOptions['serializationGroups']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$message = [ |
65
|
|
|
'data' => $this->serializer->serialize($data, 'json', $context), |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
if (!empty($attributes)) { |
69
|
|
|
$message['attributes'] = $attributes; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$result = $this->topic->publish($message, $options); |
73
|
|
|
/** @var PublishMessageResultDTO $resultDTO */ |
74
|
|
|
$resultDTO = $this->serializer->fromArray($result, PublishMessageResultDTO::class); |
75
|
|
|
|
76
|
|
|
$this->logInfo('Message(s) {messages} submitted to topic {topic}', [ |
77
|
|
|
'messages' => join(', ', $resultDTO->getMessageIds()), |
78
|
|
|
'topic' => $this->topicName, |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
return $resultDTO; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create or fetch topic |
86
|
|
|
*/ |
87
|
|
|
private function setupTopic() |
88
|
|
|
{ |
89
|
|
|
if ($this->topic instanceof Topic) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
$this->topic = $this->client->createTopic($this->topicName); |
95
|
|
|
$this->logInfo('New topic {topic} created', ['topic' => $this->topicName]); |
96
|
|
|
} catch (ConflictException $exception) { // topic already exists |
97
|
|
|
$this->topic = $this->client->topic($this->topicName); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|