Skip to content

Commit 514e324

Browse files
committed
senses
1 parent 84895f4 commit 514e324

File tree

4 files changed

+149
-11
lines changed

4 files changed

+149
-11
lines changed

SnsClient.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ public function subscribe(array $args): Result
4848
return $this->callApi('subscribe', $args);
4949
}
5050

51+
public function unsubscribe(array $args): Result
52+
{
53+
return $this->callApi('unsubscribe', $args);
54+
}
55+
56+
public function listSubscriptionsByTopic(array $args): Result
57+
{
58+
return $this->callApi('ListSubscriptionsByTopic', $args);
59+
}
60+
5161
public function getAWSClient(): AwsSnsClient
5262
{
5363
$this->resolveClient();

SnsContext.php

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,61 @@ public function declareTopic(SnsDestination $destination): void
7979

8080
public function subscribe(SnsSubscribe $subscribe): void
8181
{
82+
foreach ($this->getSubscriptions($subscribe->getTopic()) as $subscription) {
83+
if ($subscription['Protocol'] === $subscribe->getProtocol()
84+
&& $subscription['Endpoint'] === $subscribe->getEndpoint()) {
85+
return;
86+
}
87+
}
88+
8289
$this->client->subscribe([
8390
'Attributes' => $subscribe->getAttributes(),
8491
'Endpoint' => $subscribe->getEndpoint(),
85-
//'Protocol' => '<string>', // REQUIRED
86-
//'ReturnSubscriptionArn' => true || false,
87-
//'TopicArn' => '<string>', // REQUIRED
88-
//]);
92+
'Protocol' => $subscribe->getProtocol(),
93+
'ReturnSubscriptionArn' => $subscribe->isReturnSubscriptionArn(),
94+
'TopicArn' => $this->getTopicArn($subscribe->getTopic()),
8995
]);
9096
}
9197

98+
public function unsubscibe(SnsUnsubscribe $unsubscribe): void
99+
{
100+
foreach ($this->getSubscriptions($unsubscribe->getTopic()) as $subscription) {
101+
if ($subscription['Protocol'] != $unsubscribe->getProtocol()) {
102+
continue;
103+
}
104+
105+
if ($subscription['Endpoint'] != $unsubscribe->getEndpoint()) {
106+
continue;
107+
}
108+
109+
$this->client->unsubscribe([
110+
'SubscriptionArn' => $subscription['SubscriptionArn'],
111+
]);
112+
}
113+
}
114+
115+
public function getSubscriptions(SnsDestination $destination): array
116+
{
117+
$args = [
118+
'TopicArn' => $this->getTopicArn($destination),
119+
];
120+
121+
$subscriptions = [];
122+
while (true) {
123+
$result = $this->client->listSubscriptionsByTopic($args);
124+
125+
$subscriptions = array_merge($subscriptions, $result->get('Subscriptions'));
126+
127+
if (false == $result->hasKey('NextToken')) {
128+
break;
129+
}
130+
131+
$args['NextToken'] = $result->get('NextToken');
132+
}
133+
134+
return $subscriptions;
135+
}
136+
92137
public function getTopicArn(SnsDestination $destination): string
93138
{
94139
if (false == array_key_exists($destination->getTopicName(), $this->topicArns)) {

SnsSubscribe.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,67 @@
66

77
class SnsSubscribe
88
{
9-
private $destination;
9+
const PROTOCOL_SQS = 'sqs';
1010

11+
/**
12+
* @var SnsDestination
13+
*/
14+
private $topic;
15+
16+
/**
17+
* @var string
18+
*/
1119
private $endpoint;
1220

21+
/**
22+
* @var string
23+
*/
1324
private $protocol;
1425

26+
/**
27+
* @var
28+
*/
29+
private $returnSubscriptionArn;
30+
31+
/**
32+
* @var
33+
*/
1534
private $attributes;
1635

17-
public function __construct(SnsDestination $destination, string $endpoint, string $protocol, array $arguments)
18-
{
19-
$this->destination = $destination;
36+
public function __construct(
37+
SnsDestination $topic,
38+
string $endpoint,
39+
string $protocol,
40+
bool $returnSubscriptionArn = false,
41+
array $attributes = []
42+
) {
43+
$this->topic = $topic;
2044
$this->endpoint = $endpoint;
2145
$this->protocol = $protocol;
22-
$this->attributes = $arguments;
46+
$this->returnSubscriptionArn = $returnSubscriptionArn;
47+
$this->attributes = $attributes;
2348
}
2449

25-
public function getDestination(): SnsDestination
50+
public function getTopic(): SnsDestination
2651
{
27-
return $this->destination;
52+
return $this->topic;
2853
}
2954

3055
public function getEndpoint(): string
3156
{
3257
return $this->endpoint;
3358
}
3459

60+
public function getProtocol(): string
61+
{
62+
return $this->protocol;
63+
}
64+
65+
public function isReturnSubscriptionArn(): bool
66+
{
67+
return $this->returnSubscriptionArn;
68+
}
69+
3570
public function getAttributes(): array
3671
{
3772
return $this->attributes;

SnsUnsubscribe.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Enqueue\Sns;
6+
7+
class SnsUnsubscribe
8+
{
9+
/**
10+
* @var SnsDestination
11+
*/
12+
private $topic;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $endpoint;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $protocol;
23+
24+
public function __construct(
25+
SnsDestination $topic,
26+
string $endpoint,
27+
string $protocol
28+
) {
29+
$this->topic = $topic;
30+
$this->endpoint = $endpoint;
31+
$this->protocol = $protocol;
32+
}
33+
34+
public function getTopic(): SnsDestination
35+
{
36+
return $this->topic;
37+
}
38+
39+
public function getEndpoint(): string
40+
{
41+
return $this->endpoint;
42+
}
43+
44+
public function getProtocol(): string
45+
{
46+
return $this->protocol;
47+
}
48+
}

0 commit comments

Comments
 (0)