Skip to content

[Notifier] Add Sendberry notifier bridge #45195

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransportFactory;
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransportFactory;
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory as SendinblueNotifierTransportFactory;
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
Expand Down Expand Up @@ -2445,6 +2446,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
OrangeSmsTransportFactory::class => 'notifier.transport_factory.orange-sms',
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovh-cloud',
RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
SendberryTransportFactory::class => 'notifier.transport_factory.sendberry',
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
SlackTransportFactory::class => 'notifier.transport_factory.slack',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransportFactory;
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransportFactory;
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
Expand Down Expand Up @@ -155,6 +156,10 @@
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.sendberry', SendberryTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.sendinblue', SendinblueTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sendberry/.gitignore
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,7 @@
CHANGELOG
=========

6.1
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sendberry/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2022 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.
25 changes: 25 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sendberry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Sendberry Notifier
==================

Provides [Sendberry](https://sendberry.com/) integration for Symfony Notifier.

DSN example
-----------

```
SENDBERRY_DSN=sendberry://USERNAME:PASSWORD@default?auth_key=AUTH_KEY&from=FROM
```

where:
- `USERNAME` is your user-defined access name.
- `PASSWORD` is your user-defined access password.
- `AUTH_KEY` is your authentication key generated by the platform.
- `FROM` is your sender name (phone number or sender name).

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,107 @@
<?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\Sendberry;

use Symfony\Component\Notifier\Exception\IncompleteDsnException;
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\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Vasilij Duško <vasilij@prado.lt>
*/
final class SendberryTransport extends AbstractTransport
{
protected const HOST = 'api.sendberry.com';

private string $username;
private string $password;
private string $authKey;
private string $from;

public function __construct(string $username, string $password, string $authKey, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->username = $username;
$this->password = $password;
$this->authKey = $authKey;
$this->from = $from;

parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('sendberry://%s:%s@%s?auth_key=%s&from=%s', $this->username, $this->password, $this->getEndpoint(), $this->authKey, $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);
}

if (!preg_match('/^[+]+[1-9][0-9]{9,14}$/', $this->from)) {
if ('' === $this->from) {
throw new IncompleteDsnException('This phone number is invalid.');
}

if (!preg_match('/^[a-zA-Z0-9 ]+$/', $this->from)) {
throw new IncompleteDsnException('The Sender ID is invalid.');
}
}

$endpoint = sprintf('https://%s/SMS/SEND', $this->getEndpoint());
$response = $this->client->request('POST', $endpoint, [
'json' => [
'from' => $this->from,
'to' => [$message->getPhone()],
'content' => $message->getSubject(),
'key' => $this->authKey,
'name' => $this->username,
'password' => $this->password,
],
]);

try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Sendberry server.', $response, 0, $e);
}

if (200 !== $statusCode) {
throw new TransportException('Unable to send the SMS.', $response);
}

$responseArr = $response->toArray();
if (isset($responseArr['status']) && 'ok' !== $responseArr['status']) {
throw new TransportException(sprintf("Unable to send the SMS. \n%s\n.", implode("\n", $responseArr['message'])), $response);
}

$sentMessage = new SentMessage($message, (string) $this);
if (isset($responseArr['ID'])) {
$sentMessage->setMessageId($responseArr['ID']);
}

return $sentMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\Sendberry;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;

/**
* @author Vasilij Duško <vasilij@prado.lt>
*/
final class SendberryTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): SendberryTransport
{
$scheme = $dsn->getScheme();

if ('sendberry' !== $scheme) {
throw new UnsupportedSchemeException($dsn, 'sendberry', $this->getSupportedSchemes());
}

$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

return (new SendberryTransport($this->getUser($dsn), $this->getPassword($dsn), $dsn->getRequiredOption('auth_key'), $dsn->getRequiredOption('from'), $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

protected function getSupportedSchemes(): array
{
return ['sendberry'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Sendberry\Tests;

use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransportFactory;
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;

final class SendberryTransportFactoryTest extends TransportFactoryTestCase
{
public function createFactory(): SendberryTransportFactory
{
return new SendberryTransportFactory();
}

public function createProvider(): iterable
{
yield [
'sendberry://user:password@host.test?auth_key=auth_key&from=+0611223344',
'sendberry://user:password@host.test?auth_key=auth_key&from=%2B0611223344',
];
}

public function supportsProvider(): iterable
{
yield [true, 'sendberry://api_key@default?from=%2B0611223344'];
yield [false, 'somethingElse://api_key@default?from=0611223344'];
}

public function missingRequiredOptionProvider(): iterable
{
yield 'missing option: auth_key' => ['sendberry://username:password@default?from=from'];
yield 'missing option: from' => ['sendberry://username:password@default?auth_key=auth_key'];
}

public function unsupportedSchemeProvider(): iterable
{
yield ['somethingElse://api_key@default?from=+0611223344'];
yield ['somethingElse://api_key@default']; // missing "from" option
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Sendberry\Tests;

use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransport;
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\Contracts\HttpClient\HttpClientInterface;

final class SendberryTransportTest extends TransportTestCase
{
public function createTransport(HttpClientInterface $client = null): SendberryTransport
{
return new SendberryTransport('username', 'password', 'auth_key', 'from', $client ?? $this->createMock(HttpClientInterface::class));
}

public function toStringProvider(): iterable
{
yield ['sendberry://username:password@api.sendberry.com?auth_key=auth_key&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)];
}
}
30 changes: 30 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sendberry/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "symfony/sendberry-notifier",
"type": "symfony-notifier-bridge",
"description": "Symfony Sendberry Notifier Bridge",
"keywords": ["sms", "sendberry", "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": ">=8.0.2",
"symfony/http-client": "^5.4|^6.0",
"symfony/notifier": "^5.4|^6.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Sendberry\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev"
}
Loading