Skip to content

Commit 9730b80

Browse files
author
Igor Paramonov
committed
[sns] fixed topic arns parse
1 parent e162b21 commit 9730b80

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

pkg/sns/SnsConnectionFactory.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public function __construct($config = 'sns:')
6060

6161
unset($config['dsn']);
6262
}
63+
if (\array_key_exists('topic_arns', $config) && \is_string($config['topic_arns'])) {
64+
$config['topic_arns'] = $this->extractTopicArns($config['topic_arns']);
65+
}
6366
} else {
6467
throw new \LogicException(\sprintf('The config must be either an array of options, a DSN string, null or instance of %s', AwsSnsClient::class));
6568
}
@@ -133,12 +136,30 @@ private function parseDsn(string $dsn): array
133136
'version' => $dsn->getString('version'),
134137
'lazy' => $dsn->getBool('lazy'),
135138
'endpoint' => $dsn->getString('endpoint'),
136-
'topic_arns' => ($topicArns = $dsn->getString('topic_arns'))
137-
? array_filter(explode(',', $topicArns))
138-
: [],
139+
'topic_arns' => $this->extractTopicArns($dsn->getString('topic_arns')),
139140
]), function ($value) { return null !== $value; });
140141
}
141142

143+
private function extractTopicArns(?string $topicArns): array
144+
{
145+
if (!$topicArns) {
146+
return [];
147+
}
148+
149+
return array_column(
150+
array_map(function ($topic) {
151+
list ($name, $arn) = explode('|', $topic);
152+
153+
return [
154+
'name' => $name,
155+
'arn' => $arn,
156+
];
157+
}, explode(';', $topicArns)),
158+
'arn',
159+
'name'
160+
);
161+
}
162+
142163
private function defaultConfig(): array
143164
{
144165
return [

pkg/sns/Tests/SnsConnectionFactoryConfigTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public static function provideConfigs()
159159
];
160160

161161
yield [
162-
['dsn' => 'sns:?topic_arns=arn:aws:sns:us-east-1:123456789012:topic1,arn:aws:sns:us-west-2:123456789012:topic2'],
162+
['dsn' => 'sns:?topic_arns=topic1|arn:aws:sns:us-east-1:123456789012:topic1;topic2|arn:aws:sns:us-west-2:123456789012:topic2'],
163163
[
164164
'key' => null,
165165
'secret' => null,
@@ -169,8 +169,8 @@ public static function provideConfigs()
169169
'lazy' => true,
170170
'endpoint' => null,
171171
'topic_arns' => [
172-
'arn:aws:sns:us-east-1:123456789012:topic1',
173-
'arn:aws:sns:us-west-2:123456789012:topic2',
172+
'topic1' => 'arn:aws:sns:us-east-1:123456789012:topic1',
173+
'topic2' => 'arn:aws:sns:us-west-2:123456789012:topic2',
174174
],
175175
],
176176
];

0 commit comments

Comments
 (0)