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

Skip to content

Commit d9fa5f4

Browse files
committed
snsqs client driver
1 parent 8c0e3c6 commit d9fa5f4

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ services:
3232
- GPS_DSN=gps:?projectId=mqdev&emulatorHost=http://google-pubsub:8085
3333
- SQS_DSN=sqs:?key=key&secret=secret&region=us-east-1&endpoint=http://localstack:4576&version=latest
3434
- SNS_DSN=sns:?key=key&secret=secret&region=us-east-1&endpoint=http://localstack:4575&version=latest
35+
- SNSQS_DSN=snsqs:?key=key&secret=secret&region=us-east-1&sns_endpoint=http://localstack:4575&sqs_endpoint=http://localstack:4576&version=latest
3536
- WAMP_DSN=wamp://thruway:9090
3637
- REDIS_HOST=redis
3738
- REDIS_PORT=6379

pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ public function provideEnqueueConfigs()
136136
'transport' => getenv('MONGO_DSN'),
137137
],
138138
]];
139+
140+
yield 'snsqs' => [[
141+
'default' => [
142+
'transport' => [
143+
'dsn' => getenv('SNSQS_DSN'),
144+
],
145+
],
146+
]];
147+
139148
//
140149
// yield 'gps' => [[
141150
// 'transport' => [

pkg/enqueue/Resources.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Enqueue\Pheanstalk\PheanstalkConnectionFactory;
1515
use Enqueue\RdKafka\RdKafkaConnectionFactory;
1616
use Enqueue\Redis\RedisConnectionFactory;
17+
use Enqueue\SnsQs\SnsQsConnectionFactory;
1718
use Enqueue\Sqs\SqsConnectionFactory;
1819
use Enqueue\Stomp\StompConnectionFactory;
1920
use Enqueue\Wamp\WampConnectionFactory;
@@ -155,7 +156,7 @@ public static function getKnownConnections(): array
155156
'schemes' => ['sqs'],
156157
'supportedSchemeExtensions' => [],
157158
'package' => 'enqueue/sqs', ];
158-
$map[SqsConnectionFactory::class] = [
159+
$map[SnsQsConnectionFactory::class] = [
159160
'schemes' => ['snsqs'],
160161
'supportedSchemeExtensions' => [],
161162
'package' => 'enqueue/snsqs', ];

pkg/snsqs/SnsQsConnectionFactory.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@ class SnsQsConnectionFactory implements ConnectionFactory
3535
*
3636
* or
3737
*
38+
* $config = [
39+
* 'sns_key' => null, SNS option
40+
* 'sqs_secret' => null, SQS option
41+
* 'token' Option for both SNS and SQS
42+
* ].
43+
*
44+
* or
45+
*
3846
* snsqs:
39-
* snsqs:?key=aKey&secret=aSecret&token=aToken
47+
* snsqs:?key=aKey&secret=aSecret&sns_token=aSnsToken&sqs_token=aSqsToken
4048
*
4149
* @param array|string|null $config
4250
*/
@@ -51,17 +59,7 @@ public function __construct($config = 'snsqs:')
5159
if (array_key_exists('dsn', $config)) {
5260
$this->parseDsn($config['dsn']);
5361
} 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-
}
62+
$this->parseOptions($config);
6563
}
6664
} else {
6765
throw new \LogicException(sprintf('The config must be either an array of options, a DSN string, null or instance of %s', AwsSnsClient::class));
@@ -91,7 +89,29 @@ private function parseDsn(string $dsn): void
9189
));
9290
}
9391

94-
$this->snsConfig = 'sns:?'.$dsn->getQueryString();
95-
$this->sqsConfig = 'sqs:?'.$dsn->getQueryString();
92+
$this->parseOptions($dsn->getQuery());
93+
}
94+
95+
private function parseOptions(array $options): void
96+
{
97+
// set default options
98+
foreach ($options as $key => $value) {
99+
if (false === in_array(substr($key, 0, 4), ['sns_', 'sqs_'])) {
100+
$this->snsConfig[$key] = $value;
101+
$this->sqsConfig[$key] = $value;
102+
}
103+
}
104+
105+
// set transport specific options
106+
foreach ($options as $key => $value) {
107+
switch (substr($key, 0, 4)) {
108+
case 'sns_':
109+
$this->snsConfig[substr($key, 4)] = $value;
110+
break;
111+
case 'sqs_':
112+
$this->sqsConfig[substr($key, 4)] = $value;
113+
break;
114+
}
115+
}
96116
}
97117
}

pkg/test/SnsQsExtension.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ trait SnsQsExtension
99
{
1010
private function buildSnsQsContext(): SnsQsContext
1111
{
12-
$snsDsn = getenv('SNS_DSN');
13-
$sqsDsn = getenv('SQS_DSN');
14-
15-
if (false == $snsDsn || false == $sqsDsn) {
12+
if (false == $dsn = getenv('SNSQS_DSN')) {
1613
throw new \PHPUnit_Framework_SkippedTestError('Functional tests are not allowed in this environment');
1714
}
1815

19-
return (new SnsQsConnectionFactory(['sns' => $snsDsn, 'sqs' => $sqsDsn]))->createContext();
16+
return (new SnsQsConnectionFactory($dsn))->createContext();
2017
}
2118
}

0 commit comments

Comments
 (0)