Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Notifier] Add Amazon SNS bridge#39141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -122,6 +122,7 @@ | ||
| "amphp/http-tunnel": "^1.0", | ||
| "async-aws/ses": "^1.0", | ||
| "async-aws/sqs": "^1.0", | ||
| "async-aws/sns": "^1.0", | ||
fabpot marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
jderusse marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| "cache/integration-tests": "dev-master", | ||
| "composer/package-versions-deprecated": "^1.8", | ||
| "doctrine/annotations": "^1.12", | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /Testsexport-ignore | ||
| /phpunit.xml.distexport-ignore | ||
| /.gitattributesexport-ignore | ||
| /.gitignoreexport-ignore |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| vendor/ | ||
| composer.lock | ||
| phpunit.xml |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace Symfony\Component\Notifier\Bridge\AmazonSns; | ||
| use AsyncAws\Sns\Input\PublishInput; | ||
| use Symfony\Component\Notifier\Message\MessageOptionsInterface; | ||
| /** | ||
| * @author Adrien Chinour <github@chinour.fr> | ||
| */ | ||
| final class AmazonSnsOptions implements MessageOptionsInterface | ||
| { | ||
| private $options = []; | ||
| private $recipient; | ||
| public function __construct(string $recipient, array $options = []) | ||
| { | ||
| $this->recipient = $recipient; | ||
| $this->options = $options; | ||
| } | ||
| public function toArray(): array | ||
| { | ||
| return $this->options; | ||
| } | ||
| public function getRecipientId(): ?string | ||
| { | ||
| return $this->recipient; | ||
| } | ||
| /** | ||
| * @param string $topic The Topic ARN for SNS message | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function recipient(string $topic): self | ||
| { | ||
| $this->recipient = $topic; | ||
| return $this; | ||
| } | ||
| /** | ||
| * @see PublishInput::$Subject | ||
| */ | ||
| public function subject(string $subject): self | ||
| { | ||
| $this->options['Subject'] = $subject; | ||
| return $this; | ||
| } | ||
| /** | ||
| * @see PublishInput::$MessageStructure | ||
| */ | ||
| public function messageStructure(string $messageStructure): self | ||
| { | ||
| $this->options['MessageStructure'] = $messageStructure; | ||
| return $this; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace Symfony\Component\Notifier\Bridge\AmazonSns; | ||
| use AsyncAws\Sns\SnsClient; | ||
| use Symfony\Component\Notifier\Exception\TransportException; | ||
| use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; | ||
| use Symfony\Component\Notifier\Message\ChatMessage; | ||
| use Symfony\Component\Notifier\Message\MessageInterface; | ||
| use Symfony\Component\Notifier\Message\SentMessage; | ||
| use Symfony\Component\Notifier\Message\SmsMessage; | ||
| use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
| use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
| use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
| /** | ||
| * @author Adrien Chinour <github@chinour.fr> | ||
| */ | ||
| final class AmazonSnsTransport extends AbstractTransport | ||
| { | ||
| private $snsClient; | ||
| public function __construct(SnsClient $snsClient, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
| { | ||
| $this->snsClient = $snsClient; | ||
| parent::__construct($client, $dispatcher); | ||
| } | ||
| public function __toString(): string | ||
| { | ||
| $configuration = $this->snsClient->getConfiguration(); | ||
| return sprintf('sns://%s?region=%s', $this->getEndpoint(), $configuration->get('region')); | ||
| } | ||
| public function supports(MessageInterface $message): bool | ||
| { | ||
| return $message instanceof SmsMessage || ($message instanceof ChatMessage && $message->getOptions() instanceof AmazonSnsOptions); | ||
| } | ||
| protected function doSend(MessageInterface $message): SentMessage | ||
| { | ||
| if (!$this->supports($message)) { | ||
| throw new UnsupportedMessageTypeException(__CLASS__, sprintf('"%s" or "%s"', SmsMessage::class, ChatMessage::class), $message); | ||
| } | ||
| if ($message instanceof ChatMessage && $message->getOptions() instanceof AmazonSnsOptions) { | ||
| $options = $message->getOptions()->toArray(); | ||
| } | ||
| $options['Message'] = $message->getSubject(); | ||
| $options[($message instanceof ChatMessage) ? 'TopicArn' : 'PhoneNumber'] = $message->getRecipientId(); | ||
fabpot marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| try { | ||
| $response = $this->snsClient->publish($options); | ||
| $message = new SentMessage($message, (string) $this); | ||
| $message->setMessageId($response->getMessageId()); | ||
| } catch (\Exception $exception) { | ||
| $info = isset($response) ? $response->info() : []; | ||
| throw new TransportException('Unable to send the message.', $info['response'] ?? null, $info['status'] ?? 0, $exception); | ||
| } | ||
| return $message; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace Symfony\Component\Notifier\Bridge\AmazonSns; | ||
| use AsyncAws\Sns\SnsClient; | ||
| use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
| use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
| use Symfony\Component\Notifier\Transport\Dsn; | ||
| use Symfony\Component\Notifier\Transport\TransportInterface; | ||
| /** | ||
| * @author Adrien Chinour <github@chinour.fr> | ||
| */ | ||
| final class AmazonSnsTransportFactory extends AbstractTransportFactory | ||
| { | ||
| public function create(Dsn $dsn): TransportInterface | ||
| { | ||
| $scheme = $dsn->getScheme(); | ||
| if ('sns' !== $scheme) { | ||
| throw new UnsupportedSchemeException($dsn, 'sns', $this->getSupportedSchemes()); | ||
| } | ||
| $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
| $port = $dsn->getPort(); | ||
| $options = null === $host ? [] : ['endpoint' => 'https://'.$host.($port ? ':'.$port : '')]; | ||
| if ($dsn->getUser()) { | ||
| $options += [ | ||
| 'accessKeyId' => $dsn->getUser(), | ||
| 'accessKeySecret' => $dsn->getPassword(), | ||
| ]; | ||
| } | ||
| if ($dsn->getOption('region')) { | ||
| $options['region'] = $dsn->getOption('region'); | ||
| } | ||
| if ($dsn->getOption('profile')) { | ||
| $options['profile'] = $dsn->getOption('profile'); | ||
| } | ||
| return (new AmazonSnsTransport(new SnsClient($options, null, $this->client), $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
| } | ||
| protected function getSupportedSchemes(): array | ||
| { | ||
| return ['sns']; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| CHANGELOG | ||
| ========= | ||
| 5.4 | ||
| --- | ||
| * Add the bridge |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Copyright (c) 2021 Fabien Potencier | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is furnished | ||
| to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Amazon Notifier | ||
| =============== | ||
| Provides [Amazon SNS](https://aws.amazon.com/de/sns/) integration for Symfony Notifier. | ||
| DSN example | ||
| ----------- | ||
| ``` | ||
| AMAZON_SNS_DSN=sns://ACCESS_ID:ACCESS_KEY@default?region=REGION | ||
| ``` | ||
| Resources | ||
| --------- | ||
| * [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
| * [Report issues](https://github.com/symfony/symfony/issues) and | ||
| [send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
| in the [main Symfony repository](https://github.com/symfony/symfony) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace Symfony\Component\Notifier\Bridge\AmazonSns\Tests; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsOptions; | ||
| class AmazonSnsOptionsTest extends TestCase | ||
| { | ||
| public function testGetRecipientId() | ||
| { | ||
| $options = new AmazonSnsOptions('my-topic'); | ||
| $this->assertSame('my-topic', $options->getRecipientId()); | ||
| } | ||
| public function testToArray() | ||
| { | ||
| $options = new AmazonSnsOptions('my-topic'); | ||
| $options->subject('value'); | ||
| $this->assertSame(['Subject' => 'value'], $options->toArray()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace Symfony\Component\Notifier\Bridge\AmazonSns\Tests; | ||
| use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory; | ||
| use Symfony\Component\Notifier\Test\TransportFactoryTestCase; | ||
| use Symfony\Component\Notifier\Transport\TransportFactoryInterface; | ||
| class AmazonSnsTransportFactoryTest extends TransportFactoryTestCase | ||
| { | ||
| public function createFactory(): TransportFactoryInterface | ||
| { | ||
| return new AmazonSnsTransportFactory(); | ||
| } | ||
| public function createProvider(): iterable | ||
| { | ||
| yield ['sns://host.test?region=us-east-1', 'sns://host.test']; | ||
| yield ['sns://host.test?region=us-east-1', 'sns://accessId:accessKey@host.test']; | ||
| yield ['sns://host.test?region=eu-west-3', 'sns://host.test?region=eu-west-3']; | ||
| } | ||
| public function supportsProvider(): iterable | ||
| { | ||
| yield [true, 'sns://default']; | ||
| yield [false, 'somethingElse://default']; | ||
| } | ||
| public function unsupportedSchemeProvider(): iterable | ||
| { | ||
| yield ['somethingElse://default']; | ||
| } | ||
| } |
Uh oh!
There was an error while loading.Please reload this page.