-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] Add Esendex bridge #36573
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
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 |
98 changes: 98 additions & 0 deletions
98
src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransport.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,98 @@ | ||
<?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\Esendex; | ||
|
||
use Symfony\Component\HttpClient\Exception\JsonException; | ||
use Symfony\Component\HttpClient\Exception\TransportException as HttpClientTransportException; | ||
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; | ||
|
||
/** | ||
* @experimental in 5.2 | ||
*/ | ||
final class EsendexTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'api.esendex.com'; | ||
|
||
private $token; | ||
private $accountReference; | ||
private $from; | ||
|
||
public function __construct(string $token, string $accountReference, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->token = $token; | ||
$this->accountReference = $accountReference; | ||
$this->from = $from; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('esendex://%s', $this->getEndpoint()); | ||
} | ||
|
||
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))); | ||
} | ||
|
||
$messageData = [ | ||
'to' => $message->getPhone(), | ||
'body' => $message->getSubject(), | ||
]; | ||
if (null !== $this->from) { | ||
$messageData['from'] = $this->from; | ||
} | ||
|
||
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v1.0/messagedispatcher', [ | ||
'auth_basic' => $this->token, | ||
'json' => [ | ||
'accountreference' => $this->accountReference, | ||
'messages' => [$messageData], | ||
], | ||
]); | ||
|
||
if (200 === $response->getStatusCode()) { | ||
return new SentMessage($message, (string) $this); | ||
} | ||
|
||
$message = sprintf('Unable to send the SMS: error %d.', $response->getStatusCode()); | ||
|
||
try { | ||
$result = $response->toArray(false); | ||
if (!empty($result['errors'])) { | ||
$error = $result['errors'][0]; | ||
|
||
$message .= sprintf(' Details from Esendex: %s: "%s".', $error['code'], $error['description']); | ||
} | ||
} catch (HttpClientTransportException $e) { | ||
// Catching this exception is useful to keep compatibility, with symfony/http-client < 4.4.10 | ||
// See https://github.com/symfony/symfony/pull/37065 | ||
} catch (JsonException $e) { | ||
} | ||
|
||
throw new TransportException($message, $response); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransportFactory.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,47 @@ | ||
<?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\Esendex; | ||
|
||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
|
||
/** | ||
* @experimental in 5.2 | ||
*/ | ||
final class EsendexTransportFactory extends AbstractTransportFactory | ||
{ | ||
/** | ||
* @return EsendexTransport | ||
*/ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
$token = $this->getUser($dsn).':'.$this->getPassword($dsn); | ||
$accountReference = $dsn->getOption('accountreference'); | ||
$from = $dsn->getOption('from'); | ||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
if ('esendex' === $scheme) { | ||
return (new EsendexTransport($token, $accountReference, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
throw new UnsupportedSchemeException($dsn, 'esendex', $this->getSupportedSchemes()); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['esendex']; | ||
} | ||
} |
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. |
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,28 @@ | ||
Esendex Notifier | ||
================ | ||
|
||
Provides Esendex integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
// .env file | ||
ESENDEX_DSN='esendex://EMAIL:PASSWORD@default?accountreference=ACCOUNT_REFERENCE&from=FROM' | ||
``` | ||
|
||
where: | ||
- `EMAIL` is your Esendex account email | ||
- `PASSWORD` is the Esendex API password | ||
- `ACCOUNT_REFERENCE` is the Esendex account reference that the messages should be sent from. | ||
- `FROM` is the alphanumeric originator for the message to appear to originate from. | ||
|
||
See Esendex documentation at https://developers.esendex.com/api-reference#smsapis | ||
|
||
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) |
88 changes: 88 additions & 0 deletions
88
src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportTest.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,88 @@ | ||
<?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\Esendex\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransport; | ||
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 EsendexTransportTest extends TestCase | ||
{ | ||
public function testToString(): void | ||
{ | ||
$transport = new EsendexTransport('testToken', 'accountReference', 'from', $this->createMock(HttpClientInterface::class)); | ||
$transport->setHost('testHost'); | ||
|
||
$this->assertSame(sprintf('esendex://%s', 'testHost'), (string) $transport); | ||
} | ||
|
||
public function testSupportsSmsMessage(): void | ||
{ | ||
$transport = new EsendexTransport('testToken', 'accountReference', 'from', $this->createMock(HttpClientInterface::class)); | ||
|
||
$this->assertTrue($transport->supports(new SmsMessage('phone', 'testSmsMessage'))); | ||
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class))); | ||
} | ||
|
||
public function testSendNonSmsMessageThrows(): void | ||
{ | ||
$transport = new EsendexTransport('testToken', 'accountReference', 'from', $this->createMock(HttpClientInterface::class)); | ||
|
||
$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(500); | ||
|
||
$client = new MockHttpClient(static function () use ($response): ResponseInterface { | ||
return $response; | ||
}); | ||
|
||
$transport = new EsendexTransport('testToken', 'accountReference', 'from', $client); | ||
|
||
$this->expectException(TransportException::class); | ||
$this->expectExceptionMessage('Unable to send the SMS: error 500.'); | ||
$transport->send(new SmsMessage('phone', 'testMessage')); | ||
} | ||
|
||
public function testSendWithErrorResponseContainingDetailsThrows(): void | ||
{ | ||
$response = $this->createMock(ResponseInterface::class); | ||
$response->expects($this->exactly(2)) | ||
->method('getStatusCode') | ||
->willReturn(500); | ||
$response->expects($this->once()) | ||
->method('getContent') | ||
->willReturn(json_encode(['errors' => [['code' => 'accountreference_invalid', 'description' => 'Invalid Account Reference EX0000000']]])); | ||
|
||
$client = new MockHttpClient(static function () use ($response): ResponseInterface { | ||
return $response; | ||
}); | ||
|
||
$transport = new EsendexTransport('testToken', 'accountReference', 'from', $client); | ||
|
||
$this->expectException(TransportException::class); | ||
$this->expectExceptionMessage('Unable to send the SMS: error 500. Details from Esendex: accountreference_invalid: "Invalid Account Reference EX0000000".'); | ||
$transport->send(new SmsMessage('phone', 'testMessage')); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Symfony/Component/Notifier/Bridge/Esendex/composer.json
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,35 @@ | ||
{ | ||
"name": "symfony/esendex-notifier", | ||
"type": "symfony-bridge", | ||
"description": "Symfony Esendex Notifier Bridge", | ||
"keywords": ["sms", "esendex", "notifier"], | ||
"homepage": "https://symfony.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Fabien Potencier", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Symfony Community", | ||
"homepage": "https://symfony.com/contributors" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.2.5", | ||
"symfony/http-client": "^4.4|^5.0", | ||
"symfony/notifier": "^5.2" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Esendex\\": "" }, | ||
"exclude-from-classmap": [ | ||
"/Tests/" | ||
] | ||
}, | ||
"minimum-stability": "dev", | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "5.2-dev" | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Notifier/Bridge/Esendex/phpunit.xml.dist
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,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd" | ||
backupGlobals="false" | ||
colors="true" | ||
bootstrap="vendor/autoload.php" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-1" /> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite name="Symfony Esendex Notifier Bridge Test Suite"> | ||
<directory>./Tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./</directory> | ||
<exclude> | ||
<directory>./Resources</directory> | ||
<directory>./Tests</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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.