Skip to content

Commit 229ca2e

Browse files
committed
snsqs client driver
1 parent 3f3daa1 commit 229ca2e

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

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
}

0 commit comments

Comments
 (0)