-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] Add Smsmode bridge #48496
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,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
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 |
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 | ||
========= | ||
|
||
6.3 | ||
--- | ||
|
||
* Add 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) 2022-present 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,24 @@ | ||
Smsmode Notifier | ||
================ | ||
|
||
Provides [Smsmode](https://www.smsmode.com/) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
SMSMODE_DSN=smsmode://API_KEY@default?from=FROM | ||
``` | ||
|
||
where: | ||
|
||
- `API_KEY` is your Smsmode API key | ||
- `FROM` is your sender ID | ||
|
||
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) |
85 changes: 85 additions & 0 deletions
85
src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeOptions.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,85 @@ | ||
<?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\Smsmode; | ||
|
||
use Symfony\Component\Notifier\Message\MessageOptionsInterface; | ||
|
||
/** | ||
* @author gnito-org <https://github.com/gnito-org> | ||
*/ | ||
final class SmsmodeOptions implements MessageOptionsInterface | ||
gnito-org marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private array $options; | ||
|
||
public function __construct(array $options = []) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
public function getFrom(): ?string | ||
{ | ||
return $this->options['from'] ?? null; | ||
} | ||
|
||
public function getRecipientId(): ?string | ||
{ | ||
return $this->options['recipient_id'] ?? null; | ||
} | ||
|
||
public function getRefClient(): ?string | ||
{ | ||
return $this->options['ref_client'] ?? null; | ||
} | ||
|
||
public function getSentDate(): ?string | ||
{ | ||
return $this->options['sent_date'] ?? null; | ||
} | ||
|
||
public function setFrom(string $from): self | ||
{ | ||
$this->options['from'] = $from; | ||
|
||
return $this; | ||
} | ||
|
||
public function setRecipientId(string $id): self | ||
{ | ||
$this->options['recipient_id'] = $id; | ||
|
||
return $this; | ||
} | ||
|
||
public function setRefClient(string $refClient): self | ||
{ | ||
$this->options['ref_client'] = $refClient; | ||
|
||
return $this; | ||
} | ||
|
||
public function setSentDate(string $sentDate): self | ||
{ | ||
$this->options['sent_date'] = $sentDate; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$options = $this->options; | ||
if (isset($options['recipient_id'])) { | ||
unset($options['recipient_id']); | ||
} | ||
|
||
return $options; | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeTransport.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,113 @@ | ||
<?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\Smsmode; | ||
|
||
use Symfony\Component\Notifier\Exception\InvalidArgumentException; | ||
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\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author gnito-org <https://github.com/gnito-org> | ||
*/ | ||
final class SmsmodeTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'rest.smsmode.com'; | ||
|
||
public function __construct( | ||
#[\SensitiveParameter] private readonly string $apiKey, | ||
private readonly ?string $from = null, | ||
HttpClientInterface $client = null, | ||
EventDispatcherInterface $dispatcher = null | ||
) { | ||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
$queryParameters = []; | ||
if ($this->from) { | ||
$queryParameters['from'] = $this->from; | ||
} | ||
|
||
return sprintf('smsmode://%s', $this->getEndpoint()).($queryParameters ? '?'.http_build_query($queryParameters) : null); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof SmsmodeOptions); | ||
} | ||
|
||
/** | ||
* https://dev.smsmode.com/sms/v1/message. | ||
*/ | ||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof SmsMessage) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); | ||
} | ||
|
||
$endpoint = sprintf('https://%s/sms/v1/messages', $this->getEndpoint()); | ||
|
||
$opts = $message->getOptions(); | ||
$options = $opts ? $opts->toArray() : []; | ||
$options['body']['text'] = $message->getSubject(); | ||
$options['recipient']['to'] = $message->getPhone(); | ||
|
||
if (!isset($options['from'])) { | ||
$options['from'] = $this->from; | ||
} | ||
|
||
if (!preg_match('/^[a-zA-Z0-9\s]{1,11}$/', $options['from'] ?? '')) { | ||
throw new InvalidArgumentException(sprintf('The "From" value "%s" is not a valid sender ID.', $this->from)); | ||
} | ||
|
||
if (isset($options['sent_date'])) { | ||
$options['sentDate'] = $options['sent_date']; | ||
unset($options['sent_date']); | ||
} | ||
|
||
if (isset($options['ref_client'])) { | ||
$options['refClient'] = $options['ref_client']; | ||
unset($options['ref_client']); | ||
} | ||
|
||
$response = $this->client->request('POST', $endpoint, [ | ||
'headers' => ['X-Api-Key' => $this->apiKey], | ||
'json' => array_filter($options), | ||
]); | ||
|
||
try { | ||
$statusCode = $response->getStatusCode(); | ||
} catch (TransportExceptionInterface $e) { | ||
throw new TransportException('Could not reach the remote Smsmode server.', $response, 0, $e); | ||
} | ||
|
||
if (201 !== $statusCode) { | ||
$error = $response->getContent(false); | ||
throw new TransportException(sprintf('Unable to send the SMS - "%s".', $error ?: 'unknown failure'), $response); | ||
} | ||
|
||
$success = $response->toArray(false); | ||
|
||
$sentMessage = new SentMessage($message, (string) $this); | ||
$sentMessage->setMessageId($success['messageId']); | ||
|
||
return $sentMessage; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeTransportFactory.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,45 @@ | ||
<?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\Smsmode; | ||
|
||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
|
||
/** | ||
* @author gnito-org <https://github.com/gnito-org> | ||
*/ | ||
final class SmsmodeTransportFactory extends AbstractTransportFactory | ||
{ | ||
private const TRANSPORT_SCHEME = 'smsmode'; | ||
|
||
public function create(Dsn $dsn): SmsmodeTransport | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if (self::TRANSPORT_SCHEME !== $scheme) { | ||
throw new UnsupportedSchemeException($dsn, self::TRANSPORT_SCHEME, $this->getSupportedSchemes()); | ||
} | ||
|
||
$apiKey = $this->getUser($dsn); | ||
$from = $dsn->getRequiredOption('from'); | ||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
return (new SmsmodeTransport($apiKey, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return [self::TRANSPORT_SCHEME]; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeOptionsTest.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,29 @@ | ||
<?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\Smsmode\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Notifier\Bridge\Smsmode\SmsmodeOptions; | ||
|
||
class SmsmodeOptionsTest extends TestCase | ||
{ | ||
public function testSmsmodeOptions() | ||
{ | ||
$smsmodeOptions = (new SmsmodeOptions())->setFrom('test_from')->setRecipientId('test_recipient')->setRefClient('test_ref_client')->setSentDate('test_sent_date'); | ||
|
||
self::assertSame([ | ||
'from' => 'test_from', | ||
'ref_client' => 'test_ref_client', | ||
'sent_date' => 'test_sent_date', | ||
], $smsmodeOptions->toArray()); | ||
} | ||
} |
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.