Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged
fabpot merged 1 commit intosymfony:5.4fromadrien-chinour:5.x
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionscomposer.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,6 +122,7 @@
"amphp/http-tunnel": "^1.0",
"async-aws/ses": "^1.0",
"async-aws/sqs": "^1.0",
"async-aws/sns": "^1.0",
"cache/integration-tests": "dev-master",
"composer/package-versions-deprecated": "^1.8",
"doctrine/annotations": "^1.12",
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -110,6 +110,7 @@
use Symfony\Component\Mime\MimeTypeGuesserInterface;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
Expand DownExpand Up@@ -2420,6 +2421,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $

$classToServices = [
AllMySmsTransportFactory::class => 'notifier.transport_factory.allmysms',
AmazonSnsTransportFactory::class => 'notifier.transport_factory.amazonsns',
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
Expand DownExpand Up@@ -2462,6 +2464,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $

foreach ($classToServices as $class => $service) {
switch ($package = substr($service, \strlen('notifier.transport_factory.'))) {
case 'amazonsns': $package = 'amazon-sns'; break;
case 'fakechat': $package = 'fake-chat'; break;
case 'fakesms': $package = 'fake-sms'; break;
case 'freemobile': $package = 'free-mobile'; break;
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
Expand DownExpand Up@@ -177,6 +178,11 @@
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.amazonsns', AmazonSnsTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')
->tag('chatter.transport_factory')

->set('notifier.transport_factory.null', NullTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
/Testsexport-ignore
/phpunit.xml.distexport-ignore
/.gitattributesexport-ignore
/.gitignoreexport-ignore
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
View file
Open in desktop
Original file line numberDiff line numberDiff 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;
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff 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();

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;
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff 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'];
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.4
---

* Add the bridge
19 changes: 19 additions & 0 deletionssrc/Symfony/Component/Notifier/Bridge/AmazonSns/LICENSE
View file
Open in desktop
Original file line numberDiff line numberDiff 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.
19 changes: 19 additions & 0 deletionssrc/Symfony/Component/Notifier/Bridge/AmazonSns/README.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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)
View file
Open in desktop
Original file line numberDiff line numberDiff 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());
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff 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'];
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp