-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] Add Sendinblue notifier. #38298
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/Symfony/Component/Notifier/Bridge/Sendinblue/.gitattributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/Notifier/Bridge/Sendinblue/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.2.0 | ||
----- | ||
|
||
* Added the bridge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
26 changes: 26 additions & 0 deletions
26
src/Symfony/Component/Notifier/Bridge/Sendinblue/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Sendinblue Notifier | ||
=================== | ||
|
||
Provides Sendinblue integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
// .env file | ||
SENDINBLUE_DSN=sendinblue://API_KEY@default?sender=PHONE | ||
``` | ||
|
||
where: | ||
- `API_KEY` is your api key from your Sendinblue account | ||
- `PHONE` is your sender's phone number | ||
|
||
See more info at https://developers.sendinblue.com/reference#sendtransacsms | ||
|
||
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) |
83 changes: 83 additions & 0 deletions
83
src/Symfony/Component/Notifier/Bridge/Sendinblue/SendinblueTransport.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Sendinblue; | ||
|
||
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 Pierre Tondereau <[email protected]> | ||
* | ||
* @experimental in 5.2 | ||
*/ | ||
final class SendinblueTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'api.sendinblue.com'; | ||
|
||
private $apiKey; | ||
private $sender; | ||
|
||
public function __construct(string $apiKey, string $sender, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->apiKey = $apiKey; | ||
$this->sender = $sender; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('sendinblue://%s?sender=%s', $this->getEndpoint(), $this->sender); | ||
} | ||
|
||
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))); | ||
} | ||
|
||
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v3/transactionalSMS/sms', [ | ||
'json' => [ | ||
'sender' => $this->sender, | ||
'recipient' => $message->getPhone(), | ||
'content' => $message->getSubject(), | ||
], | ||
'headers' => [ | ||
'api-key' => $this->apiKey, | ||
], | ||
]); | ||
|
||
if (201 !== $response->getStatusCode()) { | ||
$error = $response->toArray(false); | ||
|
||
throw new TransportException('Unable to send the SMS: '.$error['message'], $response); | ||
} | ||
|
||
$success = $response->toArray(false); | ||
|
||
$message = new SentMessage($message, (string) $this); | ||
$message->setMessageId($success['messageId']); | ||
|
||
return $message; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Symfony/Component/Notifier/Bridge/Sendinblue/SendinblueTransportFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Sendinblue; | ||
|
||
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 Pierre Tondereau <[email protected]> | ||
* | ||
* @experimental in 5.2 | ||
*/ | ||
final class SendinblueTransportFactory extends AbstractTransportFactory | ||
{ | ||
/** | ||
* @return SendinblueTransport | ||
*/ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
if (!$sender = $dsn->getOption('sender')) { | ||
throw new IncompleteDsnException('Missing sender.', $dsn->getOriginalDsn()); | ||
} | ||
|
||
$scheme = $dsn->getScheme(); | ||
$apiKey = $this->getUser($dsn); | ||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
if ('sendinblue' === $scheme) { | ||
return (new SendinblueTransport($apiKey, $sender, $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
throw new UnsupportedSchemeException($dsn, 'sendinblue', $this->getSupportedSchemes()); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['sendinblue']; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Symfony/Component/Notifier/Bridge/Sendinblue/Tests/SendinblueTransportFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Sendinblue\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory; | ||
use Symfony\Component\Notifier\Exception\IncompleteDsnException; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
|
||
final class SendinblueTransportFactoryTest extends TestCase | ||
{ | ||
public function testCreateWithDsn(): void | ||
{ | ||
$factory = $this->initFactory(); | ||
|
||
$dsn = 'sendinblue://apiKey@default?sender=0611223344'; | ||
$transport = $factory->create(Dsn::fromString($dsn)); | ||
$transport->setHost('host.test'); | ||
|
||
$this->assertSame('sendinblue://host.test?sender=0611223344', (string) $transport); | ||
} | ||
|
||
public function testCreateWithNoPhoneThrowsMalformed(): void | ||
{ | ||
$factory = $this->initFactory(); | ||
|
||
$this->expectException(IncompleteDsnException::class); | ||
|
||
$dsnIncomplete = 'sendinblue://apiKey@default'; | ||
$factory->create(Dsn::fromString($dsnIncomplete)); | ||
} | ||
|
||
public function testSupportsSendinblueScheme(): void | ||
{ | ||
$factory = $this->initFactory(); | ||
|
||
$dsn = 'sendinblue://apiKey@default?sender=0611223344'; | ||
$dsnUnsupported = 'foobarmobile://apiKey@default?sender=0611223344'; | ||
|
||
$this->assertTrue($factory->supports(Dsn::fromString($dsn))); | ||
$this->assertFalse($factory->supports(Dsn::fromString($dsnUnsupported))); | ||
} | ||
|
||
private function initFactory(): SendinblueTransportFactory | ||
{ | ||
return new SendinblueTransportFactory(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/Notifier/Bridge/Sendinblue/Tests/SendinblueTransportTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Sendinblue\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransport; | ||
use Symfony\Component\Notifier\Exception\LogicException; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
final class SendinblueTransportTest extends TestCase | ||
{ | ||
public function testToStringContainsProperties(): void | ||
{ | ||
$transport = $this->initTransport(); | ||
|
||
$this->assertSame('sendinblue://host.test?sender=0611223344', (string) $transport); | ||
} | ||
|
||
public function testSupportsMessageInterface(): void | ||
{ | ||
$transport = $this->initTransport(); | ||
|
||
$this->assertTrue($transport->supports(new SmsMessage('0611223344', 'Hello!'))); | ||
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class), 'Hello!')); | ||
} | ||
|
||
public function testSendNonSmsMessageThrowsException(): void | ||
{ | ||
$transport = $this->initTransport(); | ||
|
||
$this->expectException(LogicException::class); | ||
$transport->send($this->createMock(MessageInterface::class)); | ||
} | ||
|
||
public function testSendWithErrorResponseThrows(): void | ||
{ | ||
$response = $this->createMock(ResponseInterface::class); | ||
$response->expects($this->exactly(2)) | ||
->method('getStatusCode') | ||
->willReturn(400); | ||
$response->expects($this->once()) | ||
->method('getContent') | ||
->willReturn(json_encode(['code' => 400, 'message' => 'bad request'])); | ||
|
||
$client = new MockHttpClient(static function () use ($response): ResponseInterface { | ||
return $response; | ||
}); | ||
|
||
$transport = $this->initTransport($client); | ||
|
||
$this->expectException(TransportException::class); | ||
$this->expectExceptionMessage('Unable to send the SMS: bad request'); | ||
$transport->send(new SmsMessage('phone', 'testMessage')); | ||
} | ||
|
||
private function initTransport(?HttpClientInterface $client = null): SendinblueTransport | ||
{ | ||
return (new SendinblueTransport( | ||
'api-key', '0611223344', $client ?: $this->createMock(HttpClientInterface::class) | ||
))->setHost('host.test'); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.