1
|
|
|
<?php |
2
|
|
|
namespace Spekkionu\PMG\Queue\Iron\Driver; |
3
|
|
|
|
4
|
|
|
use Spekkionu\PMG\Queue\Iron\Envelope\IronEnvelope; |
5
|
|
|
use IronMQ\IronMQ; |
6
|
|
|
use PMG\Queue\Driver\AbstractPersistanceDriver; |
7
|
|
|
use PMG\Queue\Exception; |
8
|
|
|
use PMG\Queue\Serializer\Serializer; |
9
|
|
|
use PMG\Queue\DefaultEnvelope; |
10
|
|
|
use PMG\Queue\Envelope; |
11
|
|
|
use PMG\Queue\Message; |
12
|
|
|
use PMG\Queue\Exception\InvalidEnvelope; |
13
|
|
|
|
14
|
|
|
class IronDriver extends AbstractPersistanceDriver |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var IronMQ |
18
|
|
|
*/ |
19
|
|
|
private $iron; |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $options; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param IronMQ $iron |
27
|
|
|
* @param Serializer $serializer |
28
|
|
|
* @param array $options Properties to pass with the message |
29
|
|
|
*/ |
30
|
8 |
|
public function __construct(IronMQ $iron, Serializer $serializer = null, array $options = []) |
31
|
|
|
{ |
32
|
8 |
|
parent::__construct($serializer); |
|
|
|
|
33
|
8 |
|
$this->iron = $iron; |
34
|
8 |
|
$this->options = $options; |
35
|
8 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
1 |
|
public function enqueue($queueName, Message $message) |
41
|
|
|
{ |
42
|
1 |
|
$env = new DefaultEnvelope($message); |
43
|
1 |
|
$data = $this->serialize($env); |
44
|
|
|
|
45
|
1 |
|
$job = $this->iron->postMessage($queueName, $data, $this->options); |
46
|
|
|
|
47
|
1 |
|
return new IronEnvelope($job->id, $env); |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritDoc |
53
|
|
|
*/ |
54
|
2 |
|
public function dequeue($queueName) |
55
|
|
|
{ |
56
|
2 |
|
$job = $this->iron->reserveMessage($queueName); |
57
|
|
|
|
58
|
2 |
|
if ($job) { |
59
|
1 |
|
$wrapped = $this->unserialize($job->body); |
60
|
1 |
|
$message = new DefaultEnvelope($wrapped->unwrap(), $job->reserved_count); |
61
|
1 |
|
$env = new IronEnvelope($job->id, $message, $job->reservation_id); |
62
|
|
|
|
63
|
1 |
|
return $env; |
64
|
|
|
} |
65
|
1 |
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
3 |
|
public function ack($queueName, Envelope $envelope) |
72
|
|
|
{ |
73
|
3 |
|
if (!$envelope instanceof IronEnvelope) { |
74
|
1 |
|
throw new InvalidEnvelope(sprintf( |
75
|
1 |
|
'%s requires that envelopes be instances of "%s", got "%s"', |
76
|
1 |
|
__CLASS__, |
77
|
1 |
|
IronEnvelope::class, |
78
|
|
|
get_class($envelope) |
79
|
|
|
)); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
$this->iron->deleteMessage($queueName, $envelope->getId(), $envelope->getReservationId()); |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
88
|
1 |
|
public function retry($queueName, Envelope $envelope) |
89
|
|
|
{ |
90
|
1 |
|
return $envelope->retry(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
1 |
|
public function fail($queueName, Envelope $envelope) |
97
|
|
|
{ |
98
|
1 |
|
return $this->ack($queueName, $envelope); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):