-
Notifications
You must be signed in to change notification settings - Fork 3
/
MongodbProducer.php
155 lines (126 loc) · 4.21 KB
/
MongodbProducer.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
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace Enqueue\Mongodb;
use Interop\Queue\Destination;
use Interop\Queue\Exception\Exception;
use Interop\Queue\Exception\InvalidDestinationException;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Producer;
class MongodbProducer implements Producer
{
/**
* @var int|null
*/
private $priority;
/**
* @var int|null
*/
private $deliveryDelay;
/**
* @var int|null
*/
private $timeToLive;
/**
* @var MongodbContext
*/
private $context;
public function __construct(MongodbContext $context)
{
$this->context = $context;
}
/**
* @param MongodbDestination $destination
* @param MongodbMessage $message
*/
public function send(Destination $destination, Message $message): void
{
InvalidDestinationException::assertDestinationInstanceOf($destination, MongodbDestination::class);
InvalidMessageException::assertMessageInstanceOf($message, MongodbMessage::class);
if (null !== $this->priority && null === $message->getPriority()) {
$message->setPriority($this->priority);
}
if (null !== $this->deliveryDelay && null === $message->getDeliveryDelay()) {
$message->setDeliveryDelay($this->deliveryDelay);
}
if (null !== $this->timeToLive && null === $message->getTimeToLive()) {
$message->setTimeToLive($this->timeToLive);
}
$body = $message->getBody();
$publishedAt = null !== $message->getPublishedAt() ?
$message->getPublishedAt() :
(int) (microtime(true) * 10000)
;
$mongoMessage = [
'published_at' => $publishedAt,
'body' => $body,
'headers' => JSON::encode($message->getHeaders()),
'properties' => JSON::encode($message->getProperties()),
'priority' => $message->getPriority(),
'queue' => $destination->getName(),
'redelivered' => $message->isRedelivered(),
];
$delay = $message->getDeliveryDelay();
if ($delay) {
if (!is_int($delay)) {
throw new \LogicException(sprintf('Delay must be integer but got: "%s"', is_object($delay) ? get_class($delay) : gettype($delay)));
}
if ($delay <= 0) {
throw new \LogicException(sprintf('Delay must be positive integer but got: "%s"', $delay));
}
$mongoMessage['delayed_until'] = time() + (int) $delay / 1000;
}
$timeToLive = $message->getTimeToLive();
if ($timeToLive) {
if (!is_int($timeToLive)) {
throw new \LogicException(sprintf('TimeToLive must be integer but got: "%s"', is_object($timeToLive) ? get_class($timeToLive) : gettype($timeToLive)));
}
if ($timeToLive <= 0) {
throw new \LogicException(sprintf('TimeToLive must be positive integer but got: "%s"', $timeToLive));
}
$mongoMessage['time_to_live'] = time() + (int) $timeToLive / 1000;
}
try {
$collection = $this->context->getCollection();
$collection->insertOne($mongoMessage);
} catch (\Exception $e) {
throw new Exception('The transport has failed to send the message due to some internal error.', $e->getCode(), $e);
}
}
/**
* @return self
*/
public function setDeliveryDelay(int $deliveryDelay = null): Producer
{
$this->deliveryDelay = $deliveryDelay;
return $this;
}
public function getDeliveryDelay(): ?int
{
return $this->deliveryDelay;
}
/**
* @return self
*/
public function setPriority(int $priority = null): Producer
{
$this->priority = $priority;
return $this;
}
public function getPriority(): ?int
{
return $this->priority;
}
/**
* @return self
*/
public function setTimeToLive(int $timeToLive = null): Producer
{
$this->timeToLive = $timeToLive;
return $this;
}
public function getTimeToLive(): ?int
{
return $this->timeToLive;
}
}