1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\Hermes\Driver; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Tomaj\Hermes\MessageInterface; |
8
|
|
|
use Tomaj\Hermes\MessageSerializer; |
9
|
|
|
use Aws\Sqs\SqsClient; |
10
|
|
|
|
11
|
|
|
class AmazonSqsDriver implements DriverInterface |
12
|
|
|
{ |
13
|
|
|
use SerializerAwareTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var SqsClient |
17
|
|
|
*/ |
18
|
|
|
private $client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $queueName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* string |
27
|
|
|
*/ |
28
|
|
|
private $queueUrl; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* integer |
32
|
|
|
*/ |
33
|
|
|
private $sleepInterval = 0; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create new Amazon SQS driver. |
37
|
|
|
* |
38
|
|
|
* You have to create aws client instnace and provide it to this driver. |
39
|
|
|
* You can use service builder or factory method. |
40
|
|
|
* |
41
|
|
|
* <code> |
42
|
|
|
* use Aws\Sqs\SqsClient; |
43
|
|
|
* |
44
|
|
|
* $client = SqsClient::factory(array( |
45
|
|
|
* 'profile' => '<profile in your aws credentials file>', |
46
|
|
|
* 'region' => '<region name>' |
47
|
|
|
* )); |
48
|
|
|
* </code> |
49
|
|
|
* |
50
|
|
|
* or |
51
|
|
|
* |
52
|
|
|
* <code> |
53
|
|
|
* use Aws\Common\Aws; |
54
|
|
|
* |
55
|
|
|
* // Create a service builder using a configuration file |
56
|
|
|
* $aws = Aws::factory('/path/to/my_config.json'); |
57
|
|
|
* |
58
|
|
|
* // Get the client from the builder by namespace |
59
|
|
|
* $client = $aws->get('Sqs'); |
60
|
|
|
* </code> |
61
|
|
|
* |
62
|
|
|
* More examples see: https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-sqs.html |
63
|
|
|
* |
64
|
|
|
* |
65
|
|
|
* @see examples/sqs folder |
66
|
|
|
* |
67
|
|
|
* @param AMQPChannel $client |
68
|
|
|
* @param string $queueName |
69
|
|
|
* @param array $queueAttributes |
70
|
|
|
*/ |
71
|
|
|
public function __construct(SqsClient $client, $queueName, $queueAttributes = []) |
72
|
|
|
{ |
73
|
|
|
$this->client = $client; |
74
|
|
|
$this->queueName = $queueName; |
75
|
|
|
$this->serializer = new MessageSerializer(); |
76
|
|
|
|
77
|
|
|
$result = $client->createQueue([ |
78
|
|
|
'QueueName' => $queueName, |
79
|
|
|
'Attributes' => $queueAttributes, |
80
|
|
|
]); |
81
|
|
|
$this->queueUrl = $result->get('QueueUrl'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function send(MessageInterface $message) |
88
|
|
|
{ |
89
|
|
|
$this->client->sendMessage([ |
90
|
|
|
'QueueUrl' => $this->queueUrl, |
91
|
|
|
'MessageBody' => $this->serializer->serialize($message), |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function wait(Closure $callback) |
99
|
|
|
{ |
100
|
|
|
while (true) { |
101
|
|
|
$result = $this->client->receiveMessage(array( |
102
|
|
|
'QueueUrl' => $this->queueUrl, |
103
|
|
|
'WaitTimeSeconds' => 20, |
104
|
|
|
)); |
105
|
|
|
|
106
|
|
|
$messages = $result['Messages']; |
107
|
|
|
|
108
|
|
|
if ($messages) { |
109
|
|
|
foreach ($messages as $message) { |
110
|
|
|
$hermesMessage = $this->serializer->unserialize($message['Body']); |
111
|
|
|
$callback($hermesMessage); |
112
|
|
|
$result = $this->client->deleteMessage(array( |
|
|
|
|
113
|
|
|
'QueueUrl' => $this->queueUrl, |
114
|
|
|
'ReceiptHandle' => $message['ReceiptHandle'], |
115
|
|
|
)); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($this->sleepInterval) { |
120
|
|
|
sleep($this->sleepInterval); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.