diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 26f4b6101872e..ee140bf1e0db4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -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; @@ -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', diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php index 65af5e03a76ac..0f1878c443d18 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php @@ -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; @@ -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') diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/.gitattributes b/src/Symfony/Component/Notifier/Bridge/Infobip/.gitattributes new file mode 100644 index 0000000000000..ebb9287043dc4 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/.gitattributes @@ -0,0 +1,3 @@ +/Tests export-ignore +/phpunit.xml.dist export-ignore +/.gitignore export-ignore diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/Infobip/CHANGELOG.md new file mode 100644 index 0000000000000..0d994e934e55a --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/CHANGELOG.md @@ -0,0 +1,7 @@ +CHANGELOG +========= + +5.2.0 +----- + + * Added the bridge diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransport.php b/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransport.php new file mode 100644 index 0000000000000..a480aabc6e43b --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransport.php @@ -0,0 +1,89 @@ + + * + * 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 + * @author Jérémy Romey + * + * @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); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransportFactory.php new file mode 100644 index 0000000000000..3c8a7968db0c8 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransportFactory.php @@ -0,0 +1,54 @@ + + * + * 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 + * @author Jérémy Romey + * + * @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']; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/LICENSE b/src/Symfony/Component/Notifier/Bridge/Infobip/LICENSE new file mode 100644 index 0000000000000..4bf0fef4ff3b0 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/LICENSE @@ -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. diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/README.md b/src/Symfony/Component/Notifier/Bridge/Infobip/README.md new file mode 100644 index 0000000000000..76419a6097e90 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/README.md @@ -0,0 +1,20 @@ +Infobip Notifier +================ + +Provides Infobip integration for Symfony Notifier. + +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) diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/Tests/InfobipTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Infobip/Tests/InfobipTransportFactoryTest.php new file mode 100644 index 0000000000000..2d3ab0804d7e4 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/Tests/InfobipTransportFactoryTest.php @@ -0,0 +1,63 @@ + + * + * 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)); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/Tests/InfobipTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Infobip/Tests/InfobipTransportTest.php new file mode 100644 index 0000000000000..4294f30962cf6 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/Tests/InfobipTransportTest.php @@ -0,0 +1,55 @@ + + * + * 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'); + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/composer.json b/src/Symfony/Component/Notifier/Bridge/Infobip/composer.json new file mode 100644 index 0000000000000..f726e98eefe56 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/composer.json @@ -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" + } + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/phpunit.xml.dist b/src/Symfony/Component/Notifier/Bridge/Infobip/phpunit.xml.dist new file mode 100644 index 0000000000000..09783ef58cdfb --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/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 84f7806d238ba..c4569430512ee 100644 --- a/src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php +++ b/src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php @@ -50,6 +50,10 @@ class UnsupportedSchemeException extends LogicException 'class' => Bridge\Twilio\TwilioTransportFactory::class, 'package' => 'symfony/twilio-notifier', ], + 'infobip' => [ + 'class' => Bridge\Infobip\InfobipTransportFactory::class, + 'package' => 'symfony/infobip-notifier', + ], 'firebase' => [ 'class' => Bridge\Firebase\FirebaseTransportFactory::class, 'package' => 'symfony/firebase-notifier', diff --git a/src/Symfony/Component/Notifier/Transport.php b/src/Symfony/Component/Notifier/Transport.php index a68234f8d412c..ff24f5f1434c3 100644 --- a/src/Symfony/Component/Notifier/Transport.php +++ b/src/Symfony/Component/Notifier/Transport.php @@ -13,6 +13,7 @@ use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory; use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory; +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; @@ -47,6 +48,7 @@ class Transport NexmoTransportFactory::class, RocketChatTransportFactory::class, TwilioTransportFactory::class, + InfobipTransportFactory::class, OvhCloudTransportFactory::class, FirebaseTransportFactory::class, SinchTransportFactory::class, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy