Skip to content

Commit e162b21

Browse files
author
Igor Paramonov
committed
[sns] added possibility to define already existing topics (prevent create topic call) php-enqueue#1022
1 parent b57d068 commit e162b21

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

pkg/sns/SnsConnectionFactory.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ class SnsConnectionFactory implements ConnectionFactory
2424

2525
/**
2626
* $config = [
27-
* 'key' => null AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
27+
* 'key' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
2828
* 'secret' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
2929
* 'token' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
3030
* '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.
3131
* 'version' => '2012-11-05', (string, required) The version of the webservice to utilize
3232
* '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
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+
* 'topic_arns' => [], (array<string>) The list of existing topic arns
3435
* ].
3536
*
3637
* or
@@ -71,7 +72,7 @@ public function __construct($config = 'sns:')
7172
*/
7273
public function createContext(): Context
7374
{
74-
return new SnsContext($this->establishConnection(), $this->config);
75+
return new SnsContext($this->establishConnection(), $this->config, $this->config['topic_arns']);
7576
}
7677

7778
private function establishConnection(): SnsClient
@@ -132,6 +133,9 @@ private function parseDsn(string $dsn): array
132133
'version' => $dsn->getString('version'),
133134
'lazy' => $dsn->getBool('lazy'),
134135
'endpoint' => $dsn->getString('endpoint'),
136+
'topic_arns' => ($topicArns = $dsn->getString('topic_arns'))
137+
? array_filter(explode(',', $topicArns))
138+
: [],
135139
]), function ($value) { return null !== $value; });
136140
}
137141

@@ -145,6 +149,7 @@ private function defaultConfig(): array
145149
'version' => '2010-03-31',
146150
'lazy' => true,
147151
'endpoint' => null,
152+
'topic_arns' => [],
148153
];
149154
}
150155
}

pkg/sns/SnsContext.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ class SnsContext implements Context
3131

3232
private $topicArns;
3333

34-
public function __construct(SnsClient $client, array $config)
34+
public function __construct(SnsClient $client, array $config, array $topicArns = [])
3535
{
3636
$this->client = $client;
3737
$this->config = $config;
38-
39-
$this->topicArns = [];
38+
$this->topicArns = $topicArns;
4039
}
4140

4241
/**

pkg/sns/Tests/SnsConnectionFactoryConfigTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static function provideConfigs()
6464
'version' => '2010-03-31',
6565
'lazy' => true,
6666
'endpoint' => null,
67+
'topic_arns' => [],
6768
],
6869
];
6970

@@ -77,6 +78,7 @@ public static function provideConfigs()
7778
'version' => '2010-03-31',
7879
'lazy' => true,
7980
'endpoint' => null,
81+
'topic_arns' => [],
8082
],
8183
];
8284

@@ -90,6 +92,7 @@ public static function provideConfigs()
9092
'version' => '2010-03-31',
9193
'lazy' => true,
9294
'endpoint' => null,
95+
'topic_arns' => [],
9396
],
9497
];
9598

@@ -103,6 +106,7 @@ public static function provideConfigs()
103106
'version' => '2010-03-31',
104107
'lazy' => false,
105108
'endpoint' => null,
109+
'topic_arns' => [],
106110
],
107111
];
108112

@@ -116,6 +120,7 @@ public static function provideConfigs()
116120
'version' => '2010-03-31',
117121
'lazy' => false,
118122
'endpoint' => null,
123+
'topic_arns' => [],
119124
],
120125
];
121126

@@ -129,6 +134,7 @@ public static function provideConfigs()
129134
'version' => '2010-03-31',
130135
'lazy' => false,
131136
'endpoint' => null,
137+
'topic_arns' => [],
132138
],
133139
];
134140

@@ -148,6 +154,24 @@ public static function provideConfigs()
148154
'version' => '2010-03-31',
149155
'lazy' => false,
150156
'endpoint' => 'http://localstack:1111',
157+
'topic_arns' => [],
158+
],
159+
];
160+
161+
yield [
162+
['dsn' => 'sns:?topic_arns=arn:aws:sns:us-east-1:123456789012:topic1,arn:aws:sns:us-west-2:123456789012:topic2'],
163+
[
164+
'key' => null,
165+
'secret' => null,
166+
'token' => null,
167+
'region' => null,
168+
'version' => '2010-03-31',
169+
'lazy' => true,
170+
'endpoint' => null,
171+
'topic_arns' => [
172+
'arn:aws:sns:us-east-1:123456789012:topic1',
173+
'arn:aws:sns:us-west-2:123456789012:topic2',
174+
],
151175
],
152176
];
153177
}

pkg/sns/Tests/SnsConnectionFactoryTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testCouldBeConstructedWithEmptyConfiguration()
3333
'region' => null,
3434
'version' => '2010-03-31',
3535
'endpoint' => null,
36+
'topic_arns' => [],
3637
], 'config', $factory);
3738
}
3839

@@ -48,6 +49,7 @@ public function testCouldBeConstructedWithCustomConfiguration()
4849
'region' => null,
4950
'version' => '2010-03-31',
5051
'endpoint' => null,
52+
'topic_arns' => [],
5153
], 'config', $factory);
5254
}
5355

0 commit comments

Comments
 (0)