1
|
|
|
<?php |
2
|
|
|
namespace Da\Mailer\Queue\Backend\Sqs; |
3
|
|
|
|
4
|
|
|
use BadMethodCallException; |
5
|
|
|
use Da\Mailer\Queue\Backend\MailJobInterface; |
6
|
|
|
use Da\Mailer\Queue\Backend\QueueStoreAdapterInterface; |
7
|
|
|
|
8
|
|
|
class SqsQueueStoreAdapter implements QueueStoreAdapterInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string the name of the queue to store the messages |
12
|
|
|
*/ |
13
|
|
|
private $queueName; |
14
|
|
|
/** |
15
|
|
|
* @var string the URL of the queue to store the messages |
16
|
|
|
*/ |
17
|
|
|
private $queueUrl; |
18
|
|
|
/** |
19
|
|
|
* @var SqsQueueStoreAdapter |
20
|
|
|
*/ |
21
|
|
|
protected $connection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* PdoQueueStoreAdapter constructor. |
25
|
|
|
* |
26
|
|
|
* @param SqsQueueStoreConnection $connection |
27
|
|
|
* @param string $queueName the name of the queue in the SQS where the mail jobs are stored |
28
|
|
|
*/ |
29
|
|
|
public function __construct(SqsQueueStoreConnection $connection, $queueName = 'mail_queue') |
30
|
|
|
{ |
31
|
|
|
$this->connection = $connection; |
|
|
|
|
32
|
|
|
$this->queueName = $queueName; |
33
|
|
|
$this->init(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public function init() |
40
|
|
|
{ |
41
|
|
|
$this->getConnection()->connect(); |
|
|
|
|
42
|
|
|
|
43
|
|
|
// create new queue or get existing one |
44
|
|
|
$queue = $this->getConnection()->getInstance()->createQueue([ |
|
|
|
|
45
|
|
|
'QueueName' => $this->queueName, |
46
|
|
|
]); |
47
|
|
|
$this->queueUrl = $queue->get('QueueUrl'); |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return SqsQueueStoreConnection |
54
|
|
|
*/ |
55
|
|
|
public function getConnection() |
56
|
|
|
{ |
57
|
|
|
return $this->connection; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param MailJobInterface|SqsMailJob $mailJob |
62
|
|
|
* |
63
|
|
|
* @return bool whether it has been successfully queued or not |
64
|
|
|
*/ |
65
|
|
|
public function enqueue(MailJobInterface $mailJob) |
66
|
|
|
{ |
67
|
|
|
$result = $this->getConnection()->getInstance()->sendMessage([ |
|
|
|
|
68
|
|
|
'QueueUrl' => $this->queueUrl, |
69
|
|
|
'MessageBody' => $mailJob->getMessage(), |
70
|
|
|
'DelaySeconds' => $mailJob->getDelaySeconds(), |
|
|
|
|
71
|
|
|
]); |
72
|
|
|
$messageId = $result->get('MessageId'); |
73
|
|
|
return $messageId !== null && is_string($messageId); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns a MailJob fetched from Amazon SQS. |
78
|
|
|
* |
79
|
|
|
* @return MailJobInterface|SqsMailJob |
80
|
|
|
*/ |
81
|
|
|
public function dequeue() |
82
|
|
|
{ |
83
|
|
|
$result = $this->getConnection()->getInstance()->receiveMessage([ |
|
|
|
|
84
|
|
|
'QueueUrl' => $this->queueUrl, |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
if (($result = $result->getPath('Messages/*')) === null) { |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return new SqsMailJob([ |
92
|
|
|
'id' => $result['MessageId'], |
93
|
|
|
'receiptHandle' => $result['ReceiptHandle'], |
94
|
|
|
'message' => $result['Body'], |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param MailJobInterface|SqsMailJob $mailJob |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function ack(MailJobInterface $mailJob) |
104
|
|
|
{ |
105
|
|
|
if ($mailJob->isNewRecord()) { |
106
|
|
|
throw new BadMethodCallException('SqsMailJob cannot be a new object to be acknowledged'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($mailJob->getDeleted()) { |
|
|
|
|
110
|
|
|
$this->getConnection()->getInstance()->deleteMessage([ |
|
|
|
|
111
|
|
|
'QueueUrl' => $this->queueUrl, |
112
|
|
|
'ReceiptHandle' => $mailJob->getReceiptHandle(), |
|
|
|
|
113
|
|
|
]); |
114
|
|
|
return true; |
115
|
|
|
} elseif ($mailJob->getVisibilityTimeout() !== null) { |
|
|
|
|
116
|
|
|
$this->getConnection()->getInstance()->changeMessageVisibility([ |
|
|
|
|
117
|
|
|
'QueueUrl' => $this->queueUrl, |
118
|
|
|
'ReceiptHandle' => $mailJob->getReceiptHandle(), |
|
|
|
|
119
|
|
|
'VisibilityTimeout' => $mailJob->getVisibilityTimeout(), |
|
|
|
|
120
|
|
|
]); |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritdoc |
129
|
|
|
*/ |
130
|
|
|
public function isEmpty() |
131
|
|
|
{ |
132
|
|
|
$attributes = $this->getConnection()->getInstance()->getQueueAttributes([ |
|
|
|
|
133
|
|
|
'QueueUrl' => $this->queueUrl, |
134
|
|
|
'AttributeNames' => ['ApproximateNumberOfMessages'], |
135
|
|
|
]); |
136
|
|
|
return $attributes->getPath('Attributes/ApproximateNumberOfMessages') == 0; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..