Skip to content

Commit 16797d6

Browse files
Implement DeliveryDelay, Priority and TimeToLive in PheanstalkProducer
1 parent 749fb74 commit 16797d6

File tree

2 files changed

+384
-24
lines changed

2 files changed

+384
-24
lines changed

pkg/pheanstalk/PheanstalkProducer.php

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Interop\Queue\Destination;
88
use Interop\Queue\Exception\InvalidDestinationException;
99
use Interop\Queue\Exception\InvalidMessageException;
10-
use Interop\Queue\Exception\PriorityNotSupportedException;
1110
use Interop\Queue\Message;
1211
use Interop\Queue\Producer;
1312
use Pheanstalk\Pheanstalk;
@@ -19,6 +18,21 @@ class PheanstalkProducer implements Producer
1918
*/
2019
private $pheanstalk;
2120

21+
/**
22+
* @var int
23+
*/
24+
private $deliveryDelay;
25+
26+
/**
27+
* @var int
28+
*/
29+
private $priority;
30+
31+
/**
32+
* @var int
33+
*/
34+
private $timeToLive;
35+
2236
public function __construct(Pheanstalk $pheanstalk)
2337
{
2438
$this->pheanstalk = $pheanstalk;
@@ -35,18 +49,14 @@ public function send(Destination $destination, Message $message): void
3549

3650
$rawMessage = json_encode($message);
3751
if (JSON_ERROR_NONE !== json_last_error()) {
38-
throw new \InvalidArgumentException(sprintf(
39-
'Could not encode value into json. Error %s and message %s',
40-
json_last_error(),
41-
json_last_error_msg()
42-
));
52+
throw new \InvalidArgumentException(sprintf('Could not encode value into json. Error %s and message %s', json_last_error(), json_last_error_msg()));
4353
}
4454

4555
$this->pheanstalk->useTube($destination->getName())->put(
4656
$rawMessage,
47-
$message->getPriority(),
48-
$message->getDelay(),
49-
$message->getTimeToRun()
57+
$this->resolvePriority($message),
58+
$this->resolveDelay($message),
59+
$this->resolveTimeToLive($message)
5060
);
5161
}
5262

@@ -55,49 +65,79 @@ public function send(Destination $destination, Message $message): void
5565
*/
5666
public function setDeliveryDelay(int $deliveryDelay = null): Producer
5767
{
58-
if (null === $deliveryDelay) {
59-
return $this;
60-
}
68+
$this->deliveryDelay = $deliveryDelay;
6169

62-
throw new \LogicException('Not implemented');
70+
return $this;
6371
}
6472

6573
public function getDeliveryDelay(): ?int
6674
{
67-
return null;
75+
return $this->deliveryDelay;
6876
}
6977

7078
/**
7179
* @return PheanstalkProducer
7280
*/
7381
public function setPriority(int $priority = null): Producer
7482
{
75-
if (null === $priority) {
76-
return $this;
77-
}
83+
$this->priority = $priority;
7884

79-
throw PriorityNotSupportedException::providerDoestNotSupportIt();
85+
return $this;
8086
}
8187

8288
public function getPriority(): ?int
8389
{
84-
return null;
90+
return $this->priority;
8591
}
8692

8793
/**
8894
* @return PheanstalkProducer
8995
*/
9096
public function setTimeToLive(int $timeToLive = null): Producer
9197
{
92-
if (null === $timeToLive) {
93-
return $this;
94-
}
98+
$this->timeToLive = $timeToLive;
9599

96-
throw new \LogicException('Not implemented');
100+
return $this;
97101
}
98102

99103
public function getTimeToLive(): ?int
100104
{
101-
return null;
105+
return $this->timeToLive;
106+
}
107+
108+
private function resolvePriority(PheanstalkMessage $message): ?int
109+
{
110+
if (null === $this->priority) {
111+
return $message->getPriority();
112+
}
113+
114+
$priority = $this->priority;
115+
$this->priority = null;
116+
117+
return $priority;
118+
}
119+
120+
private function resolveDelay(PheanstalkMessage $message): ?int
121+
{
122+
if (null === $this->deliveryDelay) {
123+
return $message->getDelay();
124+
}
125+
126+
$delay = $this->deliveryDelay;
127+
$this->deliveryDelay = null;
128+
129+
return $delay / 1000;
130+
}
131+
132+
private function resolveTimeToLive(PheanstalkMessage $message): ?int
133+
{
134+
if (null === $this->timeToLive) {
135+
return $message->getTimeToRun();
136+
}
137+
138+
$ttl = $this->timeToLive;
139+
$this->timeToLive = null;
140+
141+
return $ttl / 1000;
102142
}
103143
}

0 commit comments

Comments
 (0)