Skip to content

Commit e9c47a4

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

File tree

13 files changed

+365
-0
lines changed

13 files changed

+365
-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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\SmsMessage;
18+
use Symfony\Component\Notifier\Transport\AbstractTransport;
19+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
/**
23+
* @author Pierre Tondereau <pierre.tondereau@gmail.com>
24+
*
25+
* @experimental in 5.2
26+
*/
27+
final class SendinblueTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.sendinblue.com';
30+
31+
private $apiKey;
32+
private $sender;
33+
34+
public function __construct(string $apiKey, string $sender, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->apiKey = $apiKey;
37+
$this->sender = $sender;
38+
39+
parent::__construct($client, $dispatcher);
40+
}
41+
42+
public function __toString(): string
43+
{
44+
return sprintf('sendinblue://%s?sender=%s', $this->getEndpoint(), $this->sender);
45+
}
46+
47+
public function supports(MessageInterface $message): bool
48+
{
49+
return $message instanceof SmsMessage;
50+
}
51+
52+
protected function doSend(MessageInterface $message): void
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+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v3/transactionalSMS/sms', [
59+
'json' => [
60+
'sender' => $this->sender,
61+
'recipient' => $message->getPhone(),
62+
'content' => $message->getSubject(),
63+
],
64+
'headers' => [
65+
'api-key' => $this->apiKey,
66+
],
67+
]);
68+
69+
if (201 !== $response->getStatusCode()) {
70+
$error = $response->toArray(false);
71+
72+
throw new TransportException('Unable to send the SMS: '.$error['message'], $response);
73+
}
74+
}
75+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Pierre Tondereau <pierre.tondereau@protonmail.com>
21+
*
22+
* @experimental in 5.2
23+
*/
24+
final class SendinblueTransportFactory extends AbstractTransportFactory
25+
{
26+
/**
27+
* @return SendinblueTransport
28+
*/
29+
public function create(Dsn $dsn): TransportInterface
30+
{
31+
$scheme = $dsn->getScheme();
32+
$apiKey = $this->getUser($dsn);
33+
$sender = $dsn->getOption('sender');
34+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
35+
$port = $dsn->getPort();
36+
37+
if ('sendinblue' === $scheme) {
38+
return (new SendinblueTransport($apiKey, $sender, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
39+
}
40+
41+
throw new UnsupportedSchemeException($dsn, 'sendinblue', $this->getSupportedSchemes());
42+
}
43+
44+
protected function getSupportedSchemes(): array
45+
{
46+
return ['sendinblue'];
47+
}
48+
}
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: 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\Sendinblue\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransport;
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 SendinblueTransportTest extends TestCase
22+
{
23+
public function testToStringContainsProperties(): void
24+
{
25+
$transport = $this->initTransport();
26+
27+
$this->assertSame('sendinblue://host.test?sender=0611223344', (string) $transport);
28+
}
29+
30+
public function testSupportsMessageInterface(): void
31+
{
32+
$transport = $this->initTransport();
33+
34+
$this->assertTrue($transport->supports(new SmsMessage('0611223344', 'Hello!')));
35+
$this->assertFalse($transport->supports(new SmsMessage('0699887766', 'Hello!')));
36+
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class), 'Hello!'));
37+
}
38+
39+
public function testSendNonSmsMessageThrowsException(): void
40+
{
41+
$transport = $this->initTransport();
42+
43+
$this->expectException(LogicException::class);
44+
45+
$transport->send(new SmsMessage('0699887766', 'Hello!'));
46+
}
47+
48+
private function initTransport(): SendinblueTransport
49+
{
50+
return (new SendinblueTransport(
51+
'api-key', '0699887767', $this->createMock(HttpClientInterface::class)
52+
))->setHost('host.test');
53+
}
54+
}

0 commit comments

Comments
 (0)