/** * Checks to see if a Topic exists * * This method relies on in-memory cache and the Cache provider * to reduce the need to needlessly call the create method on an existing * Topic. * * @return boolean */ public function topicExists() { if (isset($this->topicArn)) { return true; } $key = $this->getNameWithPrefix() . '_arn'; if ($this->cache->contains($key)) { $this->topicArn = $this->cache->fetch($key); 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 (SnsException $e) { return false; } $this->topicArn = $topicArn; $this->cache->save($key, $this->topicArn); return true; } return false; }
<?php session_start(); require 'vendor/autoload.php'; use Aws\Sns\SnsClient; //Create topic and subscribe to it $email = $_POST['email']; $sns = new Aws\Sns\SnsClient(array('version' => 'latest', 'region' => 'us-east-1')); $ArnArray = $sns->createTopic(['Name' => 'mp2-jgl-pict']); $Arn = $ArnArray['TopicArn']; $settopicAttributes = $sns->setTopicAttributes(array('TopicArn' => "{$Arn}", 'AttributeName' => 'DisplayName', 'AttributeValue' => 'mp2-jgl-pict')); $topicAttributes = $sns->getTopicAttributes(array('TopicArn' => "{$Arn}", 'AttributeName' => 'DisplayName', 'AttributeValue' => 'mp2-jgl-pict')); $listSubscriptions = $sns->listSubscriptionsByTopic(array('TopicArn' => $Arn)); $subscribe = $sns->subscribe(array('TopicArn' => $Arn, 'Protocol' => 'email', 'Endpoint' => $email)); ?> <html> <body style="background-color:LemonChiffon"> <center> <h1 style=> Subscription confirmed</h1> <h2> <?php echo $email; ?> </h2> <h3> <?php echo "\r\n"; echo "You will receive an email you must confirm"; ?> </h3><a href="/index.php">Index page</a></center>
/** * @depends testCreatesTopic */ public function testListsTopicAttributes($topicArn) { $result = $this->sns->getTopicAttributes(array('TopicArn' => $topicArn)); $result = $result->toArray(); // Ensure that the map was deserialized correctly $this->assertArrayHasKey('TopicArn', $result['Attributes']); $this->assertArrayHasKey('Policy', $result['Attributes']); $this->assertArrayHasKey('Owner', $result['Attributes']); }