diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php index 5cd7e232e1..eae1e0166a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php @@ -28,6 +28,7 @@ use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory; use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory; use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory; use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory; +use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransportFactory; use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransportFactory; use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory; use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory; @@ -184,5 +185,9 @@ return static function (ContainerConfigurator $container) { ->set('notifier.transport_factory.smsbiuras', SmsBiurasTransportFactory::class) ->parent('notifier.transport_factory.abstract') ->tag('texter.transport_factory') + + ->set('notifier.transport_factory.messagebird', MessageBirdTransportFactory::class) + ->parent('notifier.transport_factory.abstract') + ->tag('texter.transport_factory') ; }; diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/.gitattributes b/src/Symfony/Component/Notifier/Bridge/MessageBird/.gitattributes new file mode 100644 index 0000000000..84c7add058 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/.gitattributes @@ -0,0 +1,4 @@ +/Tests export-ignore +/phpunit.xml.dist export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/.gitignore b/src/Symfony/Component/Notifier/Bridge/MessageBird/.gitignore new file mode 100644 index 0000000000..c49a5d8df5 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/.gitignore @@ -0,0 +1,3 @@ +vendor/ +composer.lock +phpunit.xml diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md new file mode 100644 index 0000000000..1f2b652ac2 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/CHANGELOG.md @@ -0,0 +1,7 @@ +CHANGELOG +========= + +5.3 +--- + + * Add the bridge diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/LICENSE b/src/Symfony/Component/Notifier/Bridge/MessageBird/LICENSE new file mode 100644 index 0000000000..efb17f98e7 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/LICENSE @@ -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. diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php new file mode 100644 index 0000000000..9a140ac7bb --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php @@ -0,0 +1,86 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\MessageBird; + +use Symfony\Component\Notifier\Exception\TransportException; +use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; +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 Vasilij Duško + */ +final class MessageBirdTransport extends AbstractTransport +{ + protected const HOST = 'rest.messagebird.com'; + + private $token; + private $from; + + public function __construct(string $token, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) + { + $this->token = $token; + $this->from = $from; + + parent::__construct($client, $dispatcher); + } + + public function __toString(): string + { + return sprintf('messagebird://%s?from=%s', $this->getEndpoint(), $this->from); + } + + public function supports(MessageInterface $message): bool + { + return $message instanceof SmsMessage; + } + + protected function doSend(MessageInterface $message): SentMessage + { + if (!$message instanceof SmsMessage) { + throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); + } + + $endpoint = sprintf('https://%s/messages', $this->getEndpoint()); + $response = $this->client->request('POST', $endpoint, [ + 'auth_basic' => 'AccessKey:'.$this->token, + 'body' => [ + 'originator' => $this->from, + 'recipients' => $message->getPhone(), + 'body' => $message->getSubject(), + ], + ]); + + if (201 !== $response->getStatusCode()) { + if (!isset($response->toArray(false)['errors'])) { + throw new TransportException('Unable to send the SMS.', $response); + } + + $error = $response->toArray(false)['errors']; + + throw new TransportException('Unable to send the SMS: '.$error[0]['description'] ?? 'Unknown reason', $response); + } + + $success = $response->toArray(false); + + $sentMessage = new SentMessage($message, (string) $this); + if (isset($success['id'])) { + $sentMessage->setMessageId($success['id']); + } + + return $sentMessage; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransportFactory.php new file mode 100644 index 0000000000..07202682ca --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransportFactory.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\MessageBird; + +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 Vasilij Duško + */ +final class MessageBirdTransportFactory extends AbstractTransportFactory +{ + /** + * @return MessageBirdTransport + */ + public function create(Dsn $dsn): TransportInterface + { + $scheme = $dsn->getScheme(); + + if ('messagebird' !== $scheme) { + throw new UnsupportedSchemeException($dsn, 'messagebird', $this->getSupportedSchemes()); + } + + $token = $this->getUser($dsn); + $from = $dsn->getRequiredOption('from'); + $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); + $port = $dsn->getPort(); + + return (new MessageBirdTransport($token, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port); + } + + protected function getSupportedSchemes(): array + { + return ['messagebird']; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/README.md b/src/Symfony/Component/Notifier/Bridge/MessageBird/README.md new file mode 100644 index 0000000000..ea0ef76cab --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/README.md @@ -0,0 +1,23 @@ +MessageBird Notifier +==================== + +Provides [MessageBird](https://www.messagebird.com/) integration for Symfony Notifier. + +DSN example +----------- + +``` +MESSAGEBIRD_DSN=messagebird://TOKEN@default?from=FROM +``` + +where: +- `TOKEN` is your MessageBird token +- `FROM` is your sender + +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) diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportFactoryTest.php new file mode 100644 index 0000000000..55d26b2a91 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportFactoryTest.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\MessageBird\Tests; + +use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransportFactory; +use Symfony\Component\Notifier\Test\TransportFactoryTestCase; +use Symfony\Component\Notifier\Transport\TransportFactoryInterface; + +final class MessageBirdTransportFactoryTest extends TransportFactoryTestCase +{ + /** + * @return MessageBirdTransportFactory + */ + public function createFactory(): TransportFactoryInterface + { + return new MessageBirdTransportFactory(); + } + + public function createProvider(): iterable + { + yield [ + 'messagebird://host.test?from=0611223344', + 'messagebird://token@host.test?from=0611223344', + ]; + } + + public function supportsProvider(): iterable + { + yield [true, 'messagebird://token@default?from=0611223344']; + yield [false, 'somethingElse://token@default?from=0611223344']; + } + + public function missingRequiredOptionProvider(): iterable + { + yield 'missing option: from' => ['messagebird://token@default']; + } + + public function unsupportedSchemeProvider(): iterable + { + yield ['somethingElse://token@default?from=0611223344']; + yield ['somethingElse://token@default']; // missing "from" option + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportTest.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportTest.php new file mode 100644 index 0000000000..e3f3cc94df --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdTransportTest.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\MessageBird\Tests; + +use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransport; +use Symfony\Component\Notifier\Message\ChatMessage; +use Symfony\Component\Notifier\Message\MessageInterface; +use Symfony\Component\Notifier\Message\SmsMessage; +use Symfony\Component\Notifier\Test\TransportTestCase; +use Symfony\Component\Notifier\Transport\TransportInterface; +use Symfony\Contracts\HttpClient\HttpClientInterface; + +final class MessageBirdTransportTest extends TransportTestCase +{ + /** + * @return MessageBirdTransport + */ + public function createTransport(?HttpClientInterface $client = null): TransportInterface + { + return new MessageBirdTransport('token', 'from', $client ?? $this->createMock(HttpClientInterface::class)); + } + + public function toStringProvider(): iterable + { + yield ['messagebird://rest.messagebird.com?from=from', $this->createTransport()]; + } + + public function supportedMessagesProvider(): iterable + { + yield [new SmsMessage('0611223344', 'Hello!')]; + } + + public function unsupportedMessagesProvider(): iterable + { + yield [new ChatMessage('Hello!')]; + yield [$this->createMock(MessageInterface::class)]; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/composer.json b/src/Symfony/Component/Notifier/Bridge/MessageBird/composer.json new file mode 100644 index 0000000000..269084f5d0 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/composer.json @@ -0,0 +1,30 @@ +{ + "name": "symfony/message-bird-notifier", + "type": "symfony-bridge", + "description": "Symfony MessageBird Notifier Bridge", + "keywords": ["sms", "message-bird", "notifier"], + "homepage": "https://symfony.com", + "license": "MIT", + "authors": [ + { + "name": "Vasilij Duško", + "email": "vasilij@prado.lt" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "require": { + "php": ">=7.2.5", + "symfony/http-client": "^4.4|^5.2", + "symfony/notifier": "^5.3" + }, + "autoload": { + "psr-4": { "Symfony\\Component\\Notifier\\Bridge\\MessageBird\\": "" }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "minimum-stability": "dev" +} diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/phpunit.xml.dist b/src/Symfony/Component/Notifier/Bridge/MessageBird/phpunit.xml.dist new file mode 100644 index 0000000000..330fed14a6 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/phpunit.xml.dist @@ -0,0 +1,31 @@ + + + + + + + + + + ./Tests/ + + + + + + ./ + + ./Resources + ./Tests + ./vendor + + + + diff --git a/src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php b/src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php index 923f210371..51d6744be6 100644 --- a/src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php +++ b/src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php @@ -136,6 +136,10 @@ class UnsupportedSchemeException extends LogicException 'class' => Bridge\SmsBiuras\SmsBiurasTransportFactory::class, 'package' => 'symfony/sms-biuras-notifier', ], + 'messagebird' => [ + 'class' => Bridge\MessageBird\MessageBirdTransportFactory::class, + 'package' => 'symfony/message-bird-notifier', + ], ]; /** diff --git a/src/Symfony/Component/Notifier/Transport.php b/src/Symfony/Component/Notifier/Transport.php index 3a4df9d0e5..e57a3fb302 100644 --- a/src/Symfony/Component/Notifier/Transport.php +++ b/src/Symfony/Component/Notifier/Transport.php @@ -23,6 +23,7 @@ use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory; use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory; use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory; use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory; +use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransportFactory; use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransport; use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory; use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory; @@ -80,6 +81,7 @@ class Transport LightSmsTransportFactory::class, MicrosoftTeamsTransport::class, SmsBiurasTransportFactory::class, + MessageBirdTransportFactory::class, ]; private $factories;