|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cmp\Queues\Infrastructure\AWS\v20121105\Queue; |
|
4
|
|
|
|
|
5
|
|
|
use Aws\Sns\SnsClient; |
|
6
|
|
|
use Aws\Sqs\SqsClient; |
|
7
|
|
|
|
|
8
|
|
|
class Queue |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var SqsClient |
|
12
|
|
|
*/ |
|
13
|
|
|
private $sqs; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var SnsClient |
|
17
|
|
|
*/ |
|
18
|
|
|
private $sns; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param SqsClient $sqs |
|
22
|
|
|
* @param SnsClient $sns |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(SqsClient $sqs, SnsClient $sns) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->sqs = $sqs; |
|
27
|
|
|
$this->sns = $sns; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $region |
|
32
|
|
|
* |
|
33
|
|
|
* @return self |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function create($region) |
|
36
|
|
|
{ |
|
37
|
|
|
$sqs = SqsClient::factory(['region' => $region, 'version' => '2012-11-05',]); |
|
|
|
|
|
|
38
|
|
|
$sns = SnsClient::factory(['region' => $region, 'version' => '2010-03-31']); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
return new self($sqs, $sns); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Creates the queue, the topic and the binding between the queue and the topic- |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $queueName |
|
47
|
|
|
* @param string $topicName |
|
48
|
|
|
* |
|
49
|
|
|
* @return array with two values: queueUrl and topicArn |
|
50
|
|
|
*/ |
|
51
|
|
|
public function createQueueAndTopic($queueName, $topicName) |
|
52
|
|
|
{ |
|
53
|
|
|
$queueUrl = $this->createQueue($queueName); |
|
54
|
|
|
$topicArn = $this->createTopic($topicName); |
|
55
|
|
|
$this->bindQueueToTopic($queueUrl, $topicArn); |
|
56
|
|
|
|
|
57
|
|
|
return [ |
|
58
|
|
|
'queueUrl' => $queueUrl, |
|
59
|
|
|
'topicArn' => $topicArn, |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $name |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function createQueue($name) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->sqs->createQueue(['QueueName' => $name])->get('QueueUrl'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $name |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function createTopic($name) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->sns->createTopic(['Name' => $name])->get('TopicArn'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $queueUrl |
|
85
|
|
|
* @param string $topicArn |
|
86
|
|
|
*/ |
|
87
|
|
|
public function bindQueueToTopic($queueUrl, $topicArn) |
|
88
|
|
|
{ |
|
89
|
|
|
$queueArn = $this->sqs->getQueueArn($queueUrl); |
|
90
|
|
|
$this->sns->subscribe([ |
|
91
|
|
|
'TopicArn' => $topicArn, |
|
92
|
|
|
'Protocol' => 'sqs', |
|
93
|
|
|
'Endpoint' => $queueArn, |
|
94
|
|
|
]); |
|
95
|
|
|
|
|
96
|
|
|
$this->sqs->setQueueAttributes([ |
|
97
|
|
|
'QueueUrl' => $queueUrl, |
|
98
|
|
|
'Attributes' => [ |
|
99
|
|
|
'Policy' => json_encode([ |
|
100
|
|
|
'Version' => '2012-10-17', |
|
101
|
|
|
'Statement' => [ |
|
102
|
|
|
'Effect' => 'Allow', |
|
103
|
|
|
'Principal' => '*', |
|
104
|
|
|
'Action' => 'sqs:SendMessage', |
|
105
|
|
|
'Resource' => $queueArn, |
|
106
|
|
|
'Condition' => [ |
|
107
|
|
|
'ArnEquals' => [ |
|
108
|
|
|
'aws:SourceArn' => $topicArn, |
|
109
|
|
|
], |
|
110
|
|
|
], |
|
111
|
|
|
], |
|
112
|
|
|
]), |
|
113
|
|
|
] |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This method has been deprecated.