-
Notifications
You must be signed in to change notification settings - Fork 6
/
SnsQsProducer.php
143 lines (116 loc) · 3.94 KB
/
SnsQsProducer.php
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
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace Enqueue\SnsQs;
use Enqueue\Sns\SnsContext;
use Enqueue\Sns\SnsProducer;
use Enqueue\Sqs\SqsContext;
use Enqueue\Sqs\SqsProducer;
use Interop\Queue\Destination;
use Interop\Queue\Exception\InvalidDestinationException;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Producer;
class SnsQsProducer implements Producer
{
/**
* @var SnsContext
*/
private $snsContext;
/**
* @var SnsProducer
*/
private $snsProducer;
/**
* @var SqsContext
*/
private $sqsContext;
/**
* @var SqsProducer
*/
private $sqsProducer;
public function __construct(SnsContext $snsContext, SqsContext $sqsContext)
{
$this->snsContext = $snsContext;
$this->sqsContext = $sqsContext;
}
/**
* @param SnsQsTopic $destination
* @param SnsQsMessage $message
*/
public function send(Destination $destination, Message $message): void
{
InvalidMessageException::assertMessageInstanceOf($message, SnsQsMessage::class);
if (false == $destination instanceof SnsQsTopic && false == $destination instanceof SnsQsQueue) {
throw new InvalidDestinationException(sprintf('The destination must be an instance of [%s|%s] but got %s.', SnsQsTopic::class, SnsQsQueue::class, is_object($destination) ? get_class($destination) : gettype($destination)));
}
if ($destination instanceof SnsQsTopic) {
$snsMessage = $this->snsContext->createMessage(
$message->getBody(),
$message->getProperties(),
$message->getHeaders()
);
$snsMessage->setMessageAttributes($message->getMessageAttributes());
$snsMessage->setMessageGroupId($message->getMessageGroupId());
$snsMessage->setMessageDeduplicationId($message->getMessageDeduplicationId());
$this->getSnsProducer()->send($destination, $snsMessage);
} else {
$sqsMessage = $this->sqsContext->createMessage(
$message->getBody(),
$message->getProperties(),
$message->getHeaders()
);
$sqsMessage->setMessageGroupId($message->getMessageGroupId());
$sqsMessage->setMessageDeduplicationId($message->getMessageDeduplicationId());
$this->getSqsProducer()->send($destination, $sqsMessage);
}
}
/**
* Delivery delay is supported by SQSProducer.
*/
public function setDeliveryDelay(int $deliveryDelay = null): Producer
{
$this->getSqsProducer()->setDeliveryDelay($deliveryDelay);
return $this;
}
/**
* Delivery delay is supported by SQSProducer.
*/
public function getDeliveryDelay(): ?int
{
return $this->getSqsProducer()->getDeliveryDelay();
}
public function setPriority(int $priority = null): Producer
{
$this->getSnsProducer()->setPriority($priority);
$this->getSqsProducer()->setPriority($priority);
return $this;
}
public function getPriority(): ?int
{
return $this->getSnsProducer()->getPriority();
}
public function setTimeToLive(int $timeToLive = null): Producer
{
$this->getSnsProducer()->setTimeToLive($timeToLive);
$this->getSqsProducer()->setTimeToLive($timeToLive);
return $this;
}
public function getTimeToLive(): ?int
{
return $this->getSnsProducer()->getTimeToLive();
}
private function getSnsProducer(): SnsProducer
{
if (null === $this->snsProducer) {
$this->snsProducer = $this->snsContext->createProducer();
}
return $this->snsProducer;
}
private function getSqsProducer(): SqsProducer
{
if (null === $this->sqsProducer) {
$this->sqsProducer = $this->sqsContext->createProducer();
}
return $this->sqsProducer;
}
}