Skip to content

Commit e138cba

Browse files
jeremyFreeAgentfabpot
authored andcommitted
[Notifier] Add Infobip bridge
1 parent b912af9 commit e138cba

File tree

14 files changed

+393
-0
lines changed

14 files changed

+393
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
9898
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
9999
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
100+
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
100101
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
101102
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
102103
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
@@ -2080,6 +2081,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20802081
GoogleChatTransportFactory::class => 'notifier.transport_factory.googlechat',
20812082
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
20822083
RocketChatTransportFactory::class => 'notifier.transport_factory.rocketchat',
2084+
InfobipTransportFactory::class => 'notifier.transport_factory.infobip',
20832085
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
20842086
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
20852087
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
1515
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
1616
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
17+
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
1718
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
1819
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
@@ -80,6 +81,10 @@
8081
->parent('notifier.transport_factory.abstract')
8182
->tag('texter.transport_factory')
8283

84+
->set('notifier.transport_factory.infobip', InfobipTransportFactory::class)
85+
->parent('notifier.transport_factory.abstract')
86+
->tag('texter.transport_factory')
87+
8388
->set('notifier.transport_factory.null', NullTransportFactory::class)
8489
->parent('notifier.transport_factory.abstract')
8590
->tag('chatter.transport_factory')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitignore export-ignore
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.2.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Infobip;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SentMessage;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Fabien Potencier <fabien@symfony.com>
25+
* @author Jérémy Romey <jeremy@free-agent.fr>
26+
*
27+
* @experimental in 5.2
28+
*/
29+
final class InfobipTransport extends AbstractTransport
30+
{
31+
private $authToken;
32+
private $from;
33+
34+
public function __construct(string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->authToken = $authToken;
37+
$this->from = $from;
38+
39+
parent::__construct($client, $dispatcher);
40+
}
41+
42+
public function __toString(): string
43+
{
44+
return sprintf('infobip://%s?from=%s', $this->getEndpoint(), $this->from);
45+
}
46+
47+
public function supports(MessageInterface $message): bool
48+
{
49+
return $message instanceof SmsMessage;
50+
}
51+
52+
protected function doSend(MessageInterface $message): SentMessage
53+
{
54+
if (!$message instanceof SmsMessage) {
55+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
56+
}
57+
58+
$endpoint = sprintf('https://%s/sms/2/text/advanced', $this->getEndpoint());
59+
60+
$response = $this->client->request('POST', $endpoint, [
61+
'headers' => [
62+
'Authorization' => 'App '.$this->authToken,
63+
],
64+
'json' => [
65+
'messages' => [
66+
[
67+
'from' => $this->from,
68+
'destinations' => [
69+
[
70+
'to' => $message->getPhone(),
71+
],
72+
],
73+
'text' => $message->getSubject(),
74+
],
75+
],
76+
],
77+
]);
78+
79+
if (200 !== $response->getStatusCode()) {
80+
$content = $response->toArray(false);
81+
$errorMessage = $content['requestError']['serviceException']['messageId'] ?? '';
82+
$errorInfo = $content['requestError']['serviceException']['text'] ?? '';
83+
84+
throw new TransportException(sprintf('Unable to send the SMS: '.$errorMessage.' (%s).', $errorInfo), $response);
85+
}
86+
87+
return new SentMessage($message, (string) $this);
88+
}
89+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Infobip;
13+
14+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
16+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Notifier\Transport\Dsn;
18+
use Symfony\Component\Notifier\Transport\TransportInterface;
19+
20+
/**
21+
* @author Fabien Potencier <fabien@symfony.com>
22+
* @author Jérémy Romey <jeremy@free-agent.fr>
23+
*
24+
* @experimental in 5.2
25+
*/
26+
final class InfobipTransportFactory extends AbstractTransportFactory
27+
{
28+
/**
29+
* @return InfobipTransport
30+
*/
31+
public function create(Dsn $dsn): TransportInterface
32+
{
33+
$scheme = $dsn->getScheme();
34+
$authToken = $this->getUser($dsn);
35+
$from = $dsn->getOption('from');
36+
$host = $dsn->getHost();
37+
$port = $dsn->getPort();
38+
39+
if (!$from) {
40+
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn());
41+
}
42+
43+
if ('infobip' === $scheme) {
44+
return (new InfobipTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
45+
}
46+
47+
throw new UnsupportedSchemeException($dsn, 'infobip', $this->getSupportedSchemes());
48+
}
49+
50+
protected function getSupportedSchemes(): array
51+
{
52+
return ['infobip'];
53+
}
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019-2020 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Infobip Notifier
2+
================
3+
4+
Provides Infobip integration for Symfony Notifier.
5+
6+
DSN should be as follow:
7+
8+
```
9+
infobip://authtoken@infobiphost?from=0611223344
10+
```
11+
12+
`authtoken` and `infobiphost` are given by Infobip ; `from` is the sender.
13+
14+
Resources
15+
---------
16+
17+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
18+
* [Report issues](https://github.com/symfony/symfony/issues) and
19+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
20+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Infobip\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
16+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
17+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
18+
use Symfony\Component\Notifier\Transport\Dsn;
19+
20+
final class InfobipTransportFactoryTest extends TestCase
21+
{
22+
public function testCreateWithDsn(): void
23+
{
24+
$factory = new InfobipTransportFactory();
25+
26+
$dsn = 'infobip://authtoken@default?from=0611223344';
27+
$transport = $factory->create(Dsn::fromString($dsn));
28+
$transport->setHost('host.test');
29+
30+
$this->assertSame('infobip://host.test?from=0611223344', (string) $transport);
31+
}
32+
33+
public function testCreateWithNoFromThrowsMalformed(): void
34+
{
35+
$factory = new InfobipTransportFactory();
36+
37+
$this->expectException(IncompleteDsnException::class);
38+
39+
$dsnIncomplete = 'infobip://authtoken@default';
40+
$factory->create(Dsn::fromString($dsnIncomplete));
41+
}
42+
43+
public function testSupportsInfobipScheme(): void
44+
{
45+
$factory = new InfobipTransportFactory();
46+
47+
$dsn = 'infobip://authtoken@default?from=0611223344';
48+
$dsnUnsupported = 'unsupported://authtoken@default?from=0611223344';
49+
50+
$this->assertTrue($factory->supports(Dsn::fromString($dsn)));
51+
$this->assertFalse($factory->supports(Dsn::fromString($dsnUnsupported)));
52+
}
53+
54+
public function testNonInfobipSchemeThrows(): void
55+
{
56+
$factory = new InfobipTransportFactory();
57+
58+
$this->expectException(UnsupportedSchemeException::class);
59+
60+
$dsnUnsupported = 'unsupported://authtoken@default?from=0611223344';
61+
$factory->create(Dsn::fromString($dsnUnsupported));
62+
}
63+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Infobip\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransport;
16+
use Symfony\Component\Notifier\Exception\LogicException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Contracts\HttpClient\HttpClientInterface;
20+
21+
final class InfobipTransportTest extends TestCase
22+
{
23+
public function testToStringContainsProperties(): void
24+
{
25+
$transport = $this->getTransport();
26+
27+
$this->assertSame('infobip://host.test?from=0611223344', (string) $transport);
28+
}
29+
30+
public function testSupportsMessageInterface(): void
31+
{
32+
$transport = $this->getTransport();
33+
34+
$this->assertTrue($transport->supports(new SmsMessage('0611223344', 'Hello!')));
35+
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class), 'Hello!'));
36+
}
37+
38+
public function testSendNonSmsMessageThrowsException(): void
39+
{
40+
$transport = $this->getTransport();
41+
42+
$this->expectException(LogicException::class);
43+
44+
$transport->send($this->createMock(MessageInterface::class));
45+
}
46+
47+
private function getTransport(): InfobipTransport
48+
{
49+
return (new InfobipTransport(
50+
'authtoken',
51+
'0611223344',
52+
$this->createMock(HttpClientInterface::class)
53+
))->setHost('host.test');
54+
}
55+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "symfony/infobip-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony Infobip Notifier Bridge",
5+
"keywords": ["sms", "infobip", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Fabien Potencier",
11+
"email": "fabien@symfony.com"
12+
},
13+
{
14+
"name": "Jérémy Romey",
15+
"email": "jeremy@free-agent.fr"
16+
},
17+
{
18+
"name": "Symfony Community",
19+
"homepage": "https://symfony.com/contributors"
20+
}
21+
],
22+
"require": {
23+
"php": "^7.2.5",
24+
"symfony/http-client": "^4.3|^5.0",
25+
"symfony/notifier": "^5.2"
26+
},
27+
"autoload": {
28+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Infobip\\": "" },
29+
"exclude-from-classmap": [
30+
"/Tests/"
31+
]
32+
},
33+
"minimum-stability": "dev",
34+
"extra": {
35+
"branch-alias": {
36+
"dev-master": "5.2-dev"
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)