Skip to content

Commit 7459295

Browse files
committed
Add Sendinblue notifier.
1 parent 713f30e commit 7459295

File tree

13 files changed

+400
-0
lines changed

13 files changed

+400
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22172217
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
22182218
SmsapiTransportFactory::class => 'notifier.transport_factory.smsapi',
22192219
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
2220+
SendinblueTransportFactory::class => 'notifier.transport_factory.sendinblue',
22202221
];
22212222

22222223
foreach ($classToServices as $class => $service) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
2323
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
2424
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
25+
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
2526
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
2627
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
2728
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
@@ -105,6 +106,10 @@
105106
->parent('notifier.transport_factory.abstract')
106107
->tag('texter.transport_factory')
107108

109+
->set('notifier.transport_factory.sendinblue', SendinblueTransportFactory::class)
110+
->parent('notifier.transport_factory.abstract')
111+
->tag('texter.transport_factory')
112+
108113
->set('notifier.transport_factory.null', NullTransportFactory::class)
109114
->parent('notifier.transport_factory.abstract')
110115
->tag('chatter.transport_factory')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.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: 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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Sendinblue Notifier
2+
==============
3+
4+
Provides Sendinblue integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
// .env file
11+
SENDINBLUE_MOBILE_DSN=sendinblue://API_KEY@default?sender=PHONE
12+
```
13+
14+
where:
15+
- `API_KEY` is your api key from your Sendinblue account
16+
- `PHONE` is your sender's phone number
17+
18+
See more info at https://developers.sendinblue.com/reference#sendtransacsms
19+
20+
Resources
21+
---------
22+
23+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
24+
* [Report issues](https://github.com/symfony/symfony/issues) and
25+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
26+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Sendinblue;
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 Pierre Tondereau <pierre.tondereau@gmail.com>
25+
*
26+
* @experimental in 5.2
27+
*/
28+
final class SendinblueTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'api.sendinblue.com';
31+
32+
private $apiKey;
33+
private $sender;
34+
35+
public function __construct(string $apiKey, string $sender, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->apiKey = $apiKey;
38+
$this->sender = $sender;
39+
40+
parent::__construct($client, $dispatcher);
41+
}
42+
43+
public function __toString(): string
44+
{
45+
return sprintf('sendinblue://%s?sender=%s', $this->getEndpoint(), $this->sender);
46+
}
47+
48+
public function supports(MessageInterface $message): bool
49+
{
50+
return $message instanceof SmsMessage;
51+
}
52+
53+
protected function doSend(MessageInterface $message): SentMessage
54+
{
55+
if (!$message instanceof SmsMessage) {
56+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
57+
}
58+
59+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v3/transactionalSMS/sms', [
60+
'json' => [
61+
'sender' => $this->sender,
62+
'recipient' => $message->getPhone(),
63+
'content' => $message->getSubject(),
64+
],
65+
'headers' => [
66+
'api-key' => $this->apiKey,
67+
],
68+
]);
69+
70+
if (201 !== $response->getStatusCode()) {
71+
$error = $response->toArray(false);
72+
73+
throw new TransportException('Unable to send the SMS: '.$error['message'], $response);
74+
}
75+
76+
$success = $response->toArray(false);
77+
78+
$message = new SentMessage($message, (string) $this);
79+
$message->setMessageId($success['messageId']);
80+
81+
return $message;
82+
}
83+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Sendinblue;
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 Pierre Tondereau <pierre.tondereau@protonmail.com>
22+
*
23+
* @experimental in 5.2
24+
*/
25+
final class SendinblueTransportFactory extends AbstractTransportFactory
26+
{
27+
/**
28+
* @return SendinblueTransport
29+
*/
30+
public function create(Dsn $dsn): TransportInterface
31+
{
32+
$scheme = $dsn->getScheme();
33+
$apiKey = $this->getUser($dsn);
34+
$sender = $dsn->getOption('sender');
35+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
36+
$port = $dsn->getPort();
37+
38+
if (!$sender) {
39+
throw new IncompleteDsnException('Missing sender.', $dsn->getOriginalDsn());
40+
}
41+
42+
if ('sendinblue' === $scheme) {
43+
return (new SendinblueTransport($apiKey, $sender, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
44+
}
45+
46+
throw new UnsupportedSchemeException($dsn, 'sendinblue', $this->getSupportedSchemes());
47+
}
48+
49+
protected function getSupportedSchemes(): array
50+
{
51+
return ['sendinblue'];
52+
}
53+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Sendinblue\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
16+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
17+
use Symfony\Component\Notifier\Transport\Dsn;
18+
19+
final class SendinblueTransportFactoryTest extends TestCase
20+
{
21+
public function testCreateWithDsn(): void
22+
{
23+
$factory = $this->initFactory();
24+
25+
$dsn = 'sendinblue://apiKey@default?sender=0611223344';
26+
$transport = $factory->create(Dsn::fromString($dsn));
27+
$transport->setHost('host.test');
28+
29+
$this->assertSame('sendinblue://host.test?sender=0611223344', (string) $transport);
30+
}
31+
32+
public function testCreateWithNoPhoneThrowsMalformed(): void
33+
{
34+
$factory = $this->initFactory();
35+
36+
$this->expectException(IncompleteDsnException::class);
37+
38+
$dsnIncomplete = 'sendinblue://apiKey@default';
39+
$factory->create(Dsn::fromString($dsnIncomplete));
40+
}
41+
42+
public function testSupportsSendinblueScheme(): void
43+
{
44+
$factory = $this->initFactory();
45+
46+
$dsn = 'sendinblue://apiKey@default?sender=0611223344';
47+
$dsnUnsupported = 'foobarmobile://apiKey@default?sender=0611223344';
48+
49+
$this->assertTrue($factory->supports(Dsn::fromString($dsn)));
50+
$this->assertFalse($factory->supports(Dsn::fromString($dsnUnsupported)));
51+
}
52+
53+
private function initFactory(): SendinblueTransportFactory
54+
{
55+
return new SendinblueTransportFactory();
56+
}
57+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Sendinblue\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\MockHttpClient;
16+
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransport;
17+
use Symfony\Component\Notifier\Exception\LogicException;
18+
use Symfony\Component\Notifier\Exception\TransportException;
19+
use Symfony\Component\Notifier\Message\MessageInterface;
20+
use Symfony\Component\Notifier\Message\SmsMessage;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
use Symfony\Contracts\HttpClient\ResponseInterface;
23+
24+
final class SendinblueTransportTest extends TestCase
25+
{
26+
public function testToStringContainsProperties(): void
27+
{
28+
$transport = $this->initTransport();
29+
30+
$this->assertSame('sendinblue://host.test?sender=0611223344', (string) $transport);
31+
}
32+
33+
public function testSupportsMessageInterface(): void
34+
{
35+
$transport = $this->initTransport();
36+
37+
$this->assertTrue($transport->supports(new SmsMessage('0611223344', 'Hello!')));
38+
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class), 'Hello!'));
39+
}
40+
41+
public function testSendNonSmsMessageThrowsException(): void
42+
{
43+
$transport = $this->initTransport();
44+
45+
$this->expectException(LogicException::class);
46+
$transport->send($this->createMock(MessageInterface::class));
47+
}
48+
49+
public function testSendWithErrorResponseThrows(): void
50+
{
51+
$response = $this->createMock(ResponseInterface::class);
52+
$response->expects($this->once())
53+
->method('getStatusCode')
54+
->willReturn(400);
55+
$response->expects($this->once())
56+
->method('getContent')
57+
->willReturn(json_encode(['code' => 400, 'message' => 'bad request']));
58+
59+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
60+
return $response;
61+
});
62+
63+
$transport = $this->initTransport($client);
64+
65+
$this->expectException(TransportException::class);
66+
$this->expectExceptionMessage('Unable to send the SMS: bad request');
67+
$transport->send(new SmsMessage('phone', 'testMessage'));
68+
}
69+
70+
private function initTransport(?HttpClientInterface $client = null): SendinblueTransport
71+
{
72+
return (new SendinblueTransport(
73+
'api-key', '0611223344', $client ?: $this->createMock(HttpClientInterface::class)
74+
))->setHost('host.test');
75+
}
76+
}

0 commit comments

Comments
 (0)