Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a35a323

Browse files
committed
senses
1 parent 2af7a2d commit a35a323

22 files changed

+937
-13
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"Enqueue\\SimpleClient\\": "pkg/simple-client/",
7777
"Enqueue\\Sqs\\": "pkg/sqs/",
7878
"Enqueue\\Sns\\": "pkg/sns/",
79+
"Enqueue\\SnsQs\\": "pkg/snsqs/",
7980
"Enqueue\\Stomp\\": "pkg/stomp/",
8081
"Enqueue\\Test\\": "pkg/test/",
8182
"Enqueue\\Dsn\\": "pkg/dsn/",

pkg/sns/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();

pkg/sns/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)) {

pkg/sns/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;

pkg/sns/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+
}

pkg/snsqs/.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/Tests export-ignore
2+
.gitattributes export-ignore
3+
.gitignore export-ignore
4+
.travis.yml export-ignore
5+
phpunit.xml.dist export-ignore

pkg/snsqs/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*~
2+
/composer.lock
3+
/composer.phar
4+
/phpunit.xml
5+
/vendor/
6+
/.idea/

pkg/snsqs/.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
sudo: false
2+
3+
git:
4+
depth: 10
5+
6+
language: php
7+
8+
php:
9+
- '7.1'
10+
- '7.2'
11+
12+
cache:
13+
directories:
14+
- $HOME/.composer/cache
15+
16+
install:
17+
- composer self-update
18+
- composer install --prefer-source
19+
20+
script:
21+
- vendor/bin/phpunit --exclude-group=functional

pkg/snsqs/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2018 Max Kotliar
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is furnished
9+
to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.

pkg/snsqs/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<h2 align="center">Supporting Enqueue</h2>
2+
3+
Enqueue is an MIT-licensed open source project with its ongoing development made possible entirely by the support of community and our customers. If you'd like to join them, please consider:
4+
5+
- [Become a sponsor](https://www.patreon.com/makasim)
6+
- [Become our client](http://forma-pro.com/)
7+
8+
---
9+
10+
# Amazon SNS-SQS Transport
11+
12+
[![Gitter](https://badges.gitter.im/php-enqueue/Lobby.svg)](https://gitter.im/php-enqueue/Lobby)
13+
[![Build Status](https://travis-ci.org/php-enqueue/snsqs.png?branch=master)](https://travis-ci.org/php-enqueue/snsqs)
14+
[![Total Downloads](https://poser.pugx.org/enqueue/snsqs/d/total.png)](https://packagist.org/packages/enqueue/snsqs)
15+
[![Latest Stable Version](https://poser.pugx.org/enqueue/snsqs/version.png)](https://packagist.org/packages/enqueue/snsqs)
16+
17+
This is an implementation of Queue Interop specification. It allows you to send and consume message using [Amazon SNS-SQS](https://aws.amazon.com/snsqs/) service.
18+
19+
## Resources
20+
21+
* [Site](https://enqueue.forma-pro.com/)
22+
* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/snsqs.md)
23+
* [Questions](https://gitter.im/php-enqueue/Lobby)
24+
* [Issue Tracker](https://github.com/php-enqueue/enqueue-dev/issues)
25+
26+
## License
27+
28+
It is released under the [MIT License](LICENSE).

pkg/snsqs/SnsQsConnectionFactory.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Enqueue\SnsQs;
6+
7+
use Enqueue\Dsn\Dsn;
8+
use Enqueue\Sns\SnsConnectionFactory;
9+
use Enqueue\Sqs\SqsConnectionFactory;
10+
use Interop\Queue\ConnectionFactory;
11+
use Interop\Queue\Context;
12+
13+
class SnsQsConnectionFactory implements ConnectionFactory
14+
{
15+
/**
16+
* @var string|array|null
17+
*/
18+
private $snsConfig;
19+
20+
/**
21+
* @var string|array|null
22+
*/
23+
private $sqsConfig;
24+
25+
/**
26+
* $config = [
27+
* 'key' => null AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
28+
* 'secret' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
29+
* 'token' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
30+
* 'region' => null, (string, required) Region to connect to. See http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of available regions.
31+
* 'version' => '2012-11-05', (string, required) The version of the webservice to utilize
32+
* 'lazy' => true, Enable lazy connection (boolean)
33+
* 'endpoint' => null (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack
34+
* ].
35+
*
36+
* or
37+
*
38+
* snsqs:
39+
* snsqs:?key=aKey&secret=aSecret&token=aToken
40+
*
41+
* @param array|string|null $config
42+
*/
43+
public function __construct($config = 'snsqs:')
44+
{
45+
if (empty($config)) {
46+
$this->snsConfig = [];
47+
$this->sqsConfig = [];
48+
} elseif (is_string($config)) {
49+
$this->parseDsn($config);
50+
} elseif (is_array($config)) {
51+
if (array_key_exists('dsn', $config)) {
52+
$this->parseDsn($config['dsn']);
53+
} else {
54+
if (array_key_exists('sns', $config)) {
55+
$this->snsConfig = $config['sns'];
56+
} else {
57+
$this->snsConfig = $config;
58+
}
59+
60+
if (array_key_exists('sqs', $config)) {
61+
$this->sqsConfig = $config['sqs'];
62+
} else {
63+
$this->sqsConfig = $config;
64+
}
65+
}
66+
} else {
67+
throw new \LogicException(sprintf('The config must be either an array of options, a DSN string, null or instance of %s', AwsSnsClient::class));
68+
}
69+
}
70+
71+
/**
72+
* @return SnsQsContext
73+
*/
74+
public function createContext(): Context
75+
{
76+
return new SnsQsContext(function() {
77+
return (new SnsConnectionFactory($this->snsConfig))->createContext();
78+
}, function() {
79+
return (new SqsConnectionFactory($this->sqsConfig))->createContext();
80+
});
81+
}
82+
83+
private function parseDsn(string $dsn): void
84+
{
85+
$dsn = Dsn::parseFirst($dsn);
86+
87+
if ('snsqs' !== $dsn->getSchemeProtocol()) {
88+
throw new \LogicException(sprintf(
89+
'The given scheme protocol "%s" is not supported. It must be "snsqs"',
90+
$dsn->getSchemeProtocol()
91+
));
92+
}
93+
94+
$this->snsConfig = 'sns:?'.$dsn->getQueryString();
95+
$this->sqsConfig = 'sqs:?'.$dsn->getQueryString();
96+
}
97+
}

0 commit comments

Comments
 (0)