-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.2.0 | ||
----- | ||
|
||
* Added the bridge |
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'); | ||
$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']; | ||
} | ||
} |
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Infobip Notifier | ||
================ | ||
|
||
Provides Infobip integration for Symfony Notifier. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
} | ||
} |
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" | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?