-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] Add MessageMedia Bridge #41375
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
[Notifier] Add MessageMedia Bridge
- Loading branch information
commit dd3da8fda7037d62c115cfd903e2cc2001c02a33
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/MessageMedia/.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 |
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/Notifier/Bridge/MessageMedia/.gitignore
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,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/Notifier/Bridge/MessageMedia/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.4 | ||
--- | ||
|
||
* Add the bridge |
19 changes: 19 additions & 0 deletions
19
src/Symfony/Component/Notifier/Bridge/MessageMedia/LICENSE
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) 2021 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. |
101 changes: 101 additions & 0 deletions
101
src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaTransport.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,101 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\MessageMedia; | ||
|
||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; | ||
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\Exception\DecodingExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Adrian Nguyen <vuphuong87@gmail.com> | ||
*/ | ||
final class MessageMediaTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'api.messagemedia.com'; | ||
|
||
private $apiKey; | ||
private $apiSecret; | ||
private $from; | ||
|
||
public function __construct(string $apiKey, string $apiSecret, string $from = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->apiKey = $apiKey; | ||
$this->apiSecret = $apiSecret; | ||
$this->from = $from; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
if (null !== $this->from) { | ||
return sprintf('messagemedia://%s?from=%s', $this->getEndpoint(), $this->from); | ||
} | ||
|
||
return sprintf('messagemedia://%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 UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); | ||
} | ||
|
||
$endpoint = sprintf('https://%s/v1/messages', $this->getEndpoint()); | ||
$response = $this->client->request( | ||
'POST', | ||
$endpoint, | ||
[ | ||
'auth_basic' => $this->apiKey.':'.$this->apiSecret, | ||
'json' => [ | ||
'messages' => [ | ||
[ | ||
'destination_number' => $message->getPhone(), | ||
'source_number' => $this->from, | ||
'content' => $message->getSubject(), | ||
], | ||
], | ||
], | ||
] | ||
); | ||
|
||
if (202 === $response->getStatusCode()) { | ||
$result = $response->toArray(false)['messages'][0]; | ||
$sentMessage = new SentMessage($message, (string) $this); | ||
$sentMessage->setMessageId($result['message_id']); | ||
|
||
return $sentMessage; | ||
} | ||
|
||
try { | ||
$error = $response->toArray(false); | ||
|
||
$errorMessage = $error['details'][0] ?? ($error['message'] ?? 'Unknown reason'); | ||
} catch (DecodingExceptionInterface | TransportExceptionInterface $e) { | ||
$errorMessage = 'Unknown reason'; | ||
} | ||
|
||
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $errorMessage), $response); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaTransportFactory.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,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\MessageMedia; | ||
|
||
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 Adrian Nguyen <vuphuong87@gmail.com> | ||
*/ | ||
final class MessageMediaTransportFactory extends AbstractTransportFactory | ||
{ | ||
/** | ||
* @return MessageMediaTransport | ||
*/ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if ('messagemedia' !== $scheme) { | ||
throw new UnsupportedSchemeException($dsn, 'messagemedia', $this->getSupportedSchemes()); | ||
} | ||
|
||
$apiKey = $this->getUser($dsn); | ||
$apiSecret = $this->getPassword($dsn); | ||
$from = $dsn->getOption('from'); | ||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
return (new MessageMediaTransport($apiKey, $apiSecret, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['messagemedia']; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Notifier/Bridge/MessageMedia/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,25 @@ | ||
MessageMedia Notifier | ||
================= | ||
|
||
Provides [MessageMedia](https://messagemedia.com/) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
MESSAGEMEDIA_DSN=messagemedia://API_KEY:API_SECRET@default?from=FROM | ||
``` | ||
|
||
where: | ||
- `API_KEY` is your API key | ||
- `API_SECRET` is your API secret | ||
- `FROM` is your registered sender ID (optional). Accepted values: 3-15 letters, could be alpha tag, shortcode or international phone number. | ||
When phone number starts with a `+` sign, it needs to be url encoded in the DSN | ||
|
||
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) |
51 changes: 51 additions & 0 deletions
51
...Symfony/Component/Notifier/Bridge/MessageMedia/Tests/MessageMediaTransportFactoryTest.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,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\MessageMedia\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaTransportFactory; | ||
use Symfony\Component\Notifier\Test\TransportFactoryTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportFactoryInterface; | ||
|
||
final class MessageMediaTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
/** | ||
* @return MessageMediaTransportFactory | ||
*/ | ||
public function createFactory(): TransportFactoryInterface | ||
{ | ||
return new MessageMediaTransportFactory(); | ||
} | ||
|
||
public function createProvider(): iterable | ||
{ | ||
yield [ | ||
'messagemedia://host.test', | ||
'messagemedia://apiKey:apiSecret@host.test', | ||
]; | ||
|
||
yield [ | ||
'messagemedia://host.test?from=TEST', | ||
'messagemedia://apiKey:apiSecret@host.test?from=TEST', | ||
]; | ||
} | ||
|
||
public function supportsProvider(): iterable | ||
{ | ||
yield [true, 'messagemedia://apiKey:apiSecret@default']; | ||
yield [false, 'somethingElse://apiKey:apiSecret@default']; | ||
} | ||
|
||
public function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield ['somethingElse://apiKey:apiSecret@default']; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/Symfony/Component/Notifier/Bridge/MessageMedia/Tests/MessageMediaTransportTest.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,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\MessageMedia\Tests; | ||
|
||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaTransport; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Exception\TransportExceptionInterface; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Test\TransportTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
final class MessageMediaTransportTest extends TransportTestCase | ||
{ | ||
/** | ||
* @return MessageMediaTransport | ||
*/ | ||
public function createTransport(HttpClientInterface $client = null, string $from = null): TransportInterface | ||
{ | ||
return new MessageMediaTransport('apiKey', 'apiSecret', $from, $client ?? $this->createMock(HttpClientInterface::class)); | ||
} | ||
|
||
public function toStringProvider(): iterable | ||
{ | ||
yield ['messagemedia://api.messagemedia.com', $this->createTransport()]; | ||
yield ['messagemedia://api.messagemedia.com?from=TEST', $this->createTransport(null, 'TEST')]; | ||
} | ||
|
||
public function supportedMessagesProvider(): iterable | ||
{ | ||
yield [new SmsMessage('0491570156', 'Hello!')]; | ||
} | ||
|
||
public function unsupportedMessagesProvider(): iterable | ||
{ | ||
yield [new ChatMessage('Hello!')]; | ||
yield [$this->createMock(MessageInterface::class)]; | ||
} | ||
|
||
/** | ||
* @dataProvider exceptionIsThrownWhenHttpSendFailedProvider | ||
* | ||
* @throws TransportExceptionInterface | ||
*/ | ||
public function testExceptionIsThrownWhenHttpSendFailed(int $statusCode, string $content, string $expectedExceptionMessage) | ||
{ | ||
$response = $this->createMock(ResponseInterface::class); | ||
$response->method('getStatusCode') | ||
->willReturn($statusCode); | ||
$response->method('getContent') | ||
->willReturn($content); | ||
|
||
$client = new MockHttpClient($response); | ||
|
||
$transport = new MessageMediaTransport('apiKey', 'apiSecret', null, $client); | ||
$this->expectException(TransportException::class); | ||
$this->expectExceptionMessage($expectedExceptionMessage); | ||
|
||
$transport->send(new SmsMessage('+61491570156', 'Hello!')); | ||
} | ||
|
||
public function exceptionIsThrownWhenHttpSendFailedProvider(): iterable | ||
{ | ||
yield [503, '', 'Unable to send the SMS: "Unknown reason".']; | ||
yield [500, '{"details": ["Something went wrong."]}', 'Unable to send the SMS: "Something went wrong.".']; | ||
yield [403, '{"message": "Forbidden."}', 'Unable to send the SMS: "Forbidden.']; | ||
yield [401, '{"Unauthenticated"}', 'Unable to send the SMS: "Unknown reason".']; | ||
} | ||
} |
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.