Skip to content

[Notifier] Add Infobip bridge #36480

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
Aug 10, 2020
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 @@ -97,6 +97,7 @@
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
Expand Down Expand Up @@ -2080,6 +2081,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
GoogleChatTransportFactory::class => 'notifier.transport_factory.googlechat',
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
RocketChatTransportFactory::class => 'notifier.transport_factory.rocketchat',
InfobipTransportFactory::class => 'notifier.transport_factory.infobip',
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
Expand Down Expand Up @@ -80,6 +81,10 @@
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

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

->set('notifier.transport_factory.null', NullTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Infobip/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitignore export-ignore
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Infobip/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.2.0
-----

* Added the bridge
89 changes: 89 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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\Infobip;

use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
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 Fabien Potencier <fabien@symfony.com>
* @author Jérémy Romey <jeremy@free-agent.fr>
*
* @experimental in 5.2
*/
final class InfobipTransport extends AbstractTransport
{
private $authToken;
private $from;

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

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

public function __toString(): string
{
return sprintf('infobip://%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 LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
}

$endpoint = sprintf('https://%s/sms/2/text/advanced', $this->getEndpoint());

$response = $this->client->request('POST', $endpoint, [
'headers' => [
'Authorization' => 'App '.$this->authToken,
],
'json' => [
'messages' => [
[
'from' => $this->from,
'destinations' => [
[
'to' => $message->getPhone(),
],
],
'text' => $message->getSubject(),
],
],
],
]);

if (200 !== $response->getStatusCode()) {
$content = $response->toArray(false);
$errorMessage = $content['requestError']['serviceException']['messageId'] ?? '';
$errorInfo = $content['requestError']['serviceException']['text'] ?? '';

throw new TransportException(sprintf('Unable to send the SMS: '.$errorMessage.' (%s).', $errorInfo), $response);
}

return new SentMessage($message, (string) $this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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\Infobip;

use Symfony\Component\Notifier\Exception\IncompleteDsnException;
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 Fabien Potencier <fabien@symfony.com>
* @author Jérémy Romey <jeremy@free-agent.fr>
*
* @experimental in 5.2
*/
final class InfobipTransportFactory extends AbstractTransportFactory
{
/**
* @return InfobipTransport
*/
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$authToken = $this->getUser($dsn);
$from = $dsn->getOption('from');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably throw an exception if not passed (have a look at the Freemobile transport factory for an example).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added it but we do not have that exception thrown for Nexmo and Twilio. Do you want me to add it too for those?

$host = $dsn->getHost();
$port = $dsn->getPort();

if (!$from) {
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn());
}

if ('infobip' === $scheme) {
return (new InfobipTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

throw new UnsupportedSchemeException($dsn, 'infobip', $this->getSupportedSchemes());
}

protected function getSupportedSchemes(): array
{
return ['infobip'];
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Infobip/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019-2020 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.
20 changes: 20 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Infobip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Infobip Notifier
================

Provides Infobip integration for Symfony Notifier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably document the DSN pattern here (we should do the same for all other transports if you have time to contribute that in another PR).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

DSN should be as follow:

```
infobip://authtoken@infobiphost?from=0611223344
```

`authtoken` and `infobiphost` are given by Infobip ; `from` is the 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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\Infobip\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\Dsn;

final class InfobipTransportFactoryTest extends TestCase
{
public function testCreateWithDsn(): void
{
$factory = new InfobipTransportFactory();

$dsn = 'infobip://authtoken@default?from=0611223344';
$transport = $factory->create(Dsn::fromString($dsn));
$transport->setHost('host.test');

$this->assertSame('infobip://host.test?from=0611223344', (string) $transport);
}

public function testCreateWithNoFromThrowsMalformed(): void
{
$factory = new InfobipTransportFactory();

$this->expectException(IncompleteDsnException::class);

$dsnIncomplete = 'infobip://authtoken@default';
$factory->create(Dsn::fromString($dsnIncomplete));
}

public function testSupportsInfobipScheme(): void
{
$factory = new InfobipTransportFactory();

$dsn = 'infobip://authtoken@default?from=0611223344';
$dsnUnsupported = 'unsupported://authtoken@default?from=0611223344';

$this->assertTrue($factory->supports(Dsn::fromString($dsn)));
$this->assertFalse($factory->supports(Dsn::fromString($dsnUnsupported)));
}

public function testNonInfobipSchemeThrows(): void
{
$factory = new InfobipTransportFactory();

$this->expectException(UnsupportedSchemeException::class);

$dsnUnsupported = 'unsupported://authtoken@default?from=0611223344';
$factory->create(Dsn::fromString($dsnUnsupported));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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\Infobip\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransport;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class InfobipTransportTest extends TestCase
{
public function testToStringContainsProperties(): void
{
$transport = $this->getTransport();

$this->assertSame('infobip://host.test?from=0611223344', (string) $transport);
}

public function testSupportsMessageInterface(): void
{
$transport = $this->getTransport();

$this->assertTrue($transport->supports(new SmsMessage('0611223344', 'Hello!')));
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class), 'Hello!'));
}

public function testSendNonSmsMessageThrowsException(): void
{
$transport = $this->getTransport();

$this->expectException(LogicException::class);

$transport->send($this->createMock(MessageInterface::class));
}

private function getTransport(): InfobipTransport
{
return (new InfobipTransport(
'authtoken',
'0611223344',
$this->createMock(HttpClientInterface::class)
))->setHost('host.test');
}
}
39 changes: 39 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Infobip/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "symfony/infobip-notifier",
"type": "symfony-bridge",
"description": "Symfony Infobip Notifier Bridge",
"keywords": ["sms", "infobip", "notifier"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Jérémy Romey",
"email": "jeremy@free-agent.fr"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": "^7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Infobip\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "5.2-dev"
}
}
}
Loading