1
|
|
|
<?php |
2
|
|
|
namespace Da\Mailer\Queue\Backend\Sqs; |
3
|
|
|
|
4
|
|
|
use Da\Mailer\Exception\InvalidCallException; |
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 SqsQueueStoreConnection |
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
|
5 |
|
public function __construct(SqsQueueStoreConnection $connection, $queueName = 'mail_queue') |
30
|
|
|
{ |
31
|
5 |
|
$this->connection = $connection; |
32
|
5 |
|
$this->queueName = $queueName; |
33
|
5 |
|
$this->init(); |
34
|
5 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return SqsQueueStoreAdapter |
38
|
|
|
*/ |
39
|
5 |
|
public function init() |
40
|
|
|
{ |
41
|
5 |
|
$this->getConnection()->connect(); |
42
|
|
|
|
43
|
|
|
// create new queue or get existing one |
44
|
5 |
|
$queue = $this->getConnection()->getInstance()->createQueue([ |
45
|
5 |
|
'QueueName' => $this->queueName, |
46
|
5 |
|
]); |
47
|
5 |
|
$this->queueUrl = $queue->get('QueueUrl'); |
48
|
|
|
|
49
|
5 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return SqsQueueStoreConnection |
54
|
|
|
*/ |
55
|
5 |
|
public function getConnection() |
56
|
|
|
{ |
57
|
5 |
|
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
|
3 |
|
public function enqueue(MailJobInterface $mailJob) |
66
|
|
|
{ |
67
|
3 |
|
$result = $this->getConnection()->getInstance()->sendMessage([ |
68
|
3 |
|
'QueueUrl' => $this->queueUrl, |
69
|
3 |
|
'MessageBody' => $mailJob->getMessage(), |
70
|
3 |
|
'DelaySeconds' => $mailJob->getDelaySeconds(), |
|
|
|
|
71
|
3 |
|
'Attempt' => $mailJob->getAttempt(), |
72
|
3 |
|
]); |
73
|
|
|
$messageId = $result->get('MessageId'); |
74
|
|
|
|
75
|
|
|
return $messageId !== null && is_string($messageId); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns a MailJob fetched from Amazon SQS. |
80
|
|
|
* |
81
|
|
|
* @return MailJobInterface|SqsMailJob |
82
|
|
|
*/ |
83
|
|
|
public function dequeue() |
84
|
|
|
{ |
85
|
|
|
$result = $this->getConnection()->getInstance()->receiveMessage([ |
86
|
|
|
'QueueUrl' => $this->queueUrl, |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
if (($result = $result->getPath('Messages/*')) === null) { |
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return new SqsMailJob([ |
94
|
|
|
'id' => $result['MessageId'], |
95
|
|
|
'receiptHandle' => $result['ReceiptHandle'], |
96
|
|
|
'message' => $result['Body'], |
97
|
|
|
'attempt' => $result['Attempt'], |
98
|
|
|
]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param MailJobInterface|SqsMailJob $mailJob |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
1 |
|
public function ack(MailJobInterface $mailJob) |
107
|
|
|
{ |
108
|
1 |
|
if ($mailJob->isNewRecord()) { |
109
|
1 |
|
throw new InvalidCallException('SqsMailJob cannot be a new object to be acknowledged'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($mailJob->getDeleted()) { |
|
|
|
|
113
|
|
|
$this->getConnection()->getInstance()->deleteMessage([ |
114
|
|
|
'QueueUrl' => $this->queueUrl, |
115
|
|
|
'ReceiptHandle' => $mailJob->getReceiptHandle(), |
|
|
|
|
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
return true; |
119
|
|
|
} elseif ($mailJob->getVisibilityTimeout() !== null) { |
|
|
|
|
120
|
|
|
$this->getConnection()->getInstance()->changeMessageVisibility([ |
121
|
|
|
'QueueUrl' => $this->queueUrl, |
122
|
|
|
'ReceiptHandle' => $mailJob->getReceiptHandle(), |
|
|
|
|
123
|
|
|
'VisibilityTimeout' => $mailJob->getVisibilityTimeout(), |
|
|
|
|
124
|
|
|
]); |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
public function isEmpty() |
136
|
|
|
{ |
137
|
|
|
$attributes = $this->getConnection()->getInstance()->getQueueAttributes([ |
138
|
|
|
'QueueUrl' => $this->queueUrl, |
139
|
|
|
'AttributeNames' => ['ApproximateNumberOfMessages'], |
140
|
|
|
]); |
141
|
|
|
|
142
|
|
|
return $attributes->getPath('Attributes/ApproximateNumberOfMessages') == 0; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: