feature #40646 [Notifier] Add MessageBird notifier bridge (StaffNowa)

This PR was squashed before being merged into the 5.3-dev branch.

Discussion
----------

[Notifier] Add MessageBird notifier bridge

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| License       | MIT
| Doc PR        | symfony/symfony-docs#15180
| Recipe PR   | symfony/recipes/pull/922

MessageBird notifier https://developers.messagebird.com/docs/conversations/send-messages-curl/

Commits
-------

761817ed81 [Notifier] Add MessageBird notifier bridge
This commit is contained in:
Oskar Stark 2021-04-15 16:52:16 +02:00
commit 1ca10f5c57
14 changed files with 360 additions and 0 deletions

View File

@ -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')
;
};

View File

@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore

View File

@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml

View File

@ -0,0 +1,7 @@
CHANGELOG
=========
5.3
---
* Add the bridge

View File

@ -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.

View File

@ -0,0 +1,86 @@
<?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\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 <vasilij@prado.lt>
*/
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;
}
}

View File

@ -0,0 +1,47 @@
<?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\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 <vasilij@prado.lt>
*/
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'];
}
}

View File

@ -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)

View File

@ -0,0 +1,52 @@
<?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\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
}
}

View File

@ -0,0 +1,47 @@
<?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\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)];
}
}

View File

@ -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"
}

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>
<testsuites>
<testsuite name="Symfony MessageBird Notifier Bridge Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View File

@ -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',
],
];
/**

View File

@ -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;