diff --git a/.gitignore b/.gitignore index 88af70a..2732133 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /vendor /docs/_build /coverage +.idea diff --git a/src/Provider/AwsProvider.php b/src/Provider/AwsProvider.php index c3b6243..d8326ca 100755 --- a/src/Provider/AwsProvider.php +++ b/src/Provider/AwsProvider.php @@ -23,6 +23,7 @@ namespace Uecode\Bundle\QPushBundle\Provider; use Aws\Sns\SnsClient; +use Aws\Sns\Exception\NotFoundException; use Aws\Sqs\SqsClient; use Aws\Sqs\Exception\SqsException; @@ -73,10 +74,11 @@ public function __construct($name, array $options, $client, Cache $cache, Logger $this->options = $options; $this->cache = $cache; $this->logger = $logger; + // get() method used for sdk v2, create methods for v3 $useGet = method_exists($client, 'get'); - $this->sqs = $useGet ? $client->get('Sqs') : $client->createSqs(); - $this->sns = $useGet ? $client->get('Sns') : $client->createSns(); + $this->sqs = $useGet ? $client->get('Sqs') : $client->createSqs(); + $this->sns = $useGet ? $client->get('Sns') : $client->createSns(); } public function getProvider() @@ -315,13 +317,17 @@ public function queueExists() return true; } - $result = $this->sqs->getQueueUrl([ - 'QueueName' => $this->getNameWithPrefix() - ]); + try { + $result = $this->sqs->getQueueUrl([ + 'QueueName' => $this->getNameWithPrefix() + ]); - if($this->queueUrl = $result->get('QueueUrl')) { - return true; - } + if ($this->queueUrl = $result->get('QueueUrl')) { + $this->cache->save($key, $this->queueUrl); + + return true; + } + } catch (SqsException $e) {} return false; } @@ -413,6 +419,24 @@ public function topicExists() return true; } + if (!empty($this->queueUrl)) { + $queueArn = $this->sqs->getQueueArn($this->queueUrl); + $topicArn = str_replace('sqs', 'sns', $queueArn); + + try { + $this->sns->getTopicAttributes([ + 'TopicArn' => $topicArn + ]); + } catch (NotFoundException $e) { + return false; + } + + $this->topicArn = $topicArn; + $this->cache->save($key, $this->topicArn); + + return true; + } + return false; } diff --git a/tests/MockClient/SnsMockClient.php b/tests/MockClient/SnsMockClient.php index a8197c7..7656aff 100644 --- a/tests/MockClient/SnsMockClient.php +++ b/tests/MockClient/SnsMockClient.php @@ -22,6 +22,7 @@ namespace Uecode\Bundle\QPushBundle\Tests\MockClient; +use Aws\Sns\Exception\NotFoundException; use Doctrine\Common\Collections\ArrayCollection; /** @@ -50,6 +51,19 @@ public function createTopic(array $args) ]); } + public function getTopicAttributes(array $args) + { + if ($args['TopicArn'] == null) { + throw new NotFoundException; + } + + return new ArrayCollection([ + 'Attributes' => [ + 'TopicArn' => 'long_topic_arn_string' + ] + ]); + } + public function listSubscriptionsByTopic(array $args) { return new ArrayCollection([