Skip to content

Commit

Permalink
Merge branch 'sdufel-fix-aws-exception'
Browse files Browse the repository at this point in the history
  • Loading branch information
k-k committed Dec 18, 2015
2 parents ce57ddf + b496957 commit eef2d69
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/vendor
/docs/_build
/coverage
.idea
40 changes: 32 additions & 8 deletions src/Provider/AwsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/MockClient/SnsMockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace Uecode\Bundle\QPushBundle\Tests\MockClient;

use Aws\Sns\Exception\NotFoundException;
use Doctrine\Common\Collections\ArrayCollection;

/**
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit eef2d69

Please sign in to comment.