-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #54356 [Notifier] LOX24 SMS bridge (alebedev80)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [Notifier] LOX24 SMS bridge | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT |Doc PR| symfony/symfony-docs#19692 |Recipe PR| symfony/recipes#1300 Add LOX24 SMS Gateway bridge to Symfony Notifier. A Germany based SMS Gateway. Commits ------- b1a25ae [Notifier] LOX24 SMS bridge
- Loading branch information
Showing
22 changed files
with
1,186 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 | ||
========= | ||
|
||
7.1 | ||
--- | ||
|
||
* Add the bridge |
This file contains 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) 2024-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. |
88 changes: 88 additions & 0 deletions
88
src/Symfony/Component/Notifier/Bridge/Lox24/Lox24Options.php
This file contains 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 <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\Lox24; | ||
|
||
use Symfony\Component\Notifier\Message\MessageOptionsInterface; | ||
|
||
/** | ||
* @author Andrei Lebedev <andrew.lebedev@gmail.com> | ||
*/ | ||
final class Lox24Options implements MessageOptionsInterface | ||
{ | ||
public function __construct( | ||
private array $options = [], | ||
) { | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function getRecipientId(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* DateTime object of SMS the delivery time. | ||
* If Null or not set, the message will be sent immediately. | ||
*/ | ||
public function deliveryAt(?\DateTimeInterface $deliveryAt): self | ||
{ | ||
$this->options['delivery_at'] = $deliveryAt ? $deliveryAt->getTimestamp() : 0; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* The language of the voice message. | ||
* If set 'auto', the automatic language detection by message text will be used. | ||
*/ | ||
public function voiceLanguage(VoiceLanguage $language): self | ||
{ | ||
if (VoiceLanguage::Auto === $language) { | ||
unset($this->options['voice_lang']); | ||
} else { | ||
$this->options['voice_lang'] = $language->value; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* If True deletes the message from the LOX24 database after delivery. | ||
*/ | ||
public function deleteTextAfterSending(bool $deleteText): self | ||
{ | ||
$this->options['delete_text'] = $deleteText; | ||
|
||
return $this; | ||
} | ||
|
||
public function type(Type $type): self | ||
{ | ||
$this->options['type'] = $type->value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* String which will be sent back to your endpoint. It can be usable to pass your system message id. | ||
*/ | ||
public function callbackData(?string $data): self | ||
{ | ||
$this->options['callback_data'] = $data; | ||
|
||
return $this; | ||
} | ||
} |
190 changes: 190 additions & 0 deletions
190
src/Symfony/Component/Notifier/Bridge/Lox24/Lox24Transport.php
This file contains 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,190 @@ | ||
<?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\Lox24; | ||
|
||
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\ClientExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Andrei Lebedev <andrew.lebedev@gmail.com> | ||
*/ | ||
final class Lox24Transport extends AbstractTransport | ||
{ | ||
protected const HOST = 'api.lox24.eu'; | ||
|
||
public function __construct( | ||
private readonly string $user, | ||
#[\SensitiveParameter] private readonly string $token, | ||
private readonly string $from, | ||
private readonly array $options = [], | ||
?HttpClientInterface $client = null, | ||
?EventDispatcherInterface $dispatcher = null, | ||
) { | ||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
$params = [ | ||
'from' => $this->from, | ||
...$this->options, | ||
]; | ||
|
||
$query = $params ? '?'.http_build_query($params) : ''; | ||
|
||
return sprintf('lox24://%s%s', $this->getEndpoint(), $query); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage | ||
&& (null === $message->getOptions() || $message->getOptions() instanceof Lox24Options); | ||
} | ||
|
||
/** | ||
* @throws RedirectionExceptionInterface | ||
* @throws DecodingExceptionInterface | ||
* @throws ClientExceptionInterface | ||
* @throws TransportExceptionInterface | ||
* @throws ServerExceptionInterface | ||
*/ | ||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$this->supports($message)) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); | ||
} | ||
|
||
$from = $message->getFrom() ?: $this->from; | ||
|
||
if (!$this->isFromValid($from)) { | ||
throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $from)); | ||
} | ||
|
||
$body = [ | ||
'sender_id' => $from, | ||
'phone' => $message->getPhone(), | ||
'text' => $message->getSubject(), | ||
]; | ||
|
||
$options = $message->getOptions()?->toArray() ?? []; | ||
$body = $this->setIsTextDeleted($body, $options); | ||
$body = $this->setCallbackData($body, $options); | ||
$body = $this->setDeliveryAt($body, $options); | ||
$body = $this->setServiceCode($body, $options); | ||
$body = $this->setVoiceLang($body, $options); | ||
|
||
$response = $this->client->request('POST', sprintf('https://%s/sms', $this->getEndpoint()), [ | ||
'headers' => [ | ||
'X-LOX24-AUTH-TOKEN' => sprintf('%s:%s', $this->user, $this->token), | ||
'Accept' => 'application/json', | ||
'Content-Type' => 'application/json', | ||
'User-Agent' => 'LOX24 Symfony Notifier', | ||
], | ||
'body' => $body, | ||
]); | ||
|
||
try { | ||
$statusCode = $response->getStatusCode(); | ||
} catch (TransportExceptionInterface $e) { | ||
throw new TransportException('Could not reach the remote LOX24 server.', $response, 0, $e); | ||
} | ||
|
||
if (201 !== $statusCode) { | ||
$error = $response->toArray(false); | ||
|
||
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['detail']), $response); | ||
} | ||
|
||
$success = $response->toArray(false); | ||
|
||
$sentMessage = new SentMessage($message, (string) $this); | ||
$sentMessage->setMessageId($success['uuid']); | ||
|
||
return $sentMessage; | ||
} | ||
|
||
private function isFromValid(string $from): bool | ||
{ | ||
return preg_match('/^[.\-a-zA-Z0-9_ ]{2,11}$/', $from) || preg_match('/^\+[1-9]\d{1,14}$/', $from); | ||
} | ||
|
||
private function setIsTextDeleted(array $body, array $options): array | ||
{ | ||
$body['is_text_deleted'] = (bool) ($options['delete_text'] ?? false); | ||
|
||
return $body; | ||
} | ||
|
||
private function setCallbackData(array $body, array $options): array | ||
{ | ||
if (!empty($options['callback_data'])) { | ||
$body['callback_data'] = $options['callback_data']; | ||
} | ||
|
||
return $body; | ||
} | ||
|
||
private function setDeliveryAt(array $body, array $options): array | ||
{ | ||
$body['delivery_at'] = max((int) ($options['delivery_at'] ?? 0), 0); | ||
|
||
return $body; | ||
} | ||
|
||
private function setServiceCode(array $body, array $options): array | ||
{ | ||
$code = $options['type'] ?? Type::Sms->value; | ||
|
||
try { | ||
$type = Type::from((string) $code); | ||
} catch (\ValueError) { | ||
throw new InvalidArgumentException(sprintf('Invalid type: "%s".', $code)); | ||
} | ||
|
||
$body['service_code'] = $type->getServiceCode(); | ||
|
||
return $body; | ||
} | ||
|
||
private function setVoiceLang(array $body, array $options): array | ||
{ | ||
$voiceLang = $options['voice_lang'] ?? null; | ||
if ($voiceLang) { | ||
$voiceLang = strtoupper($voiceLang); | ||
try { | ||
$lang = VoiceLanguage::from($voiceLang); | ||
} catch (\ValueError) { | ||
$allowed = implode(', ', array_map(static fn ($case) => $case->value, VoiceLanguage::cases())); | ||
$str = 'The "voice_lang" option "%s" is not a valid language. Allowed languages are: %s.'; | ||
throw new InvalidArgumentException(sprintf($str, $voiceLang, $allowed)); | ||
} | ||
|
||
if (VoiceLanguage::Auto !== $lang) { | ||
$body['voice_lang'] = $lang->value; | ||
} | ||
} | ||
|
||
return $body; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Symfony/Component/Notifier/Bridge/Lox24/Lox24TransportFactory.php
This file contains 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,53 @@ | ||
<?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\Lox24; | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
|
||
/** | ||
* @author Andrei Lebedev <andrew.lebedev@gmail.com> | ||
*/ | ||
final class Lox24TransportFactory extends AbstractTransportFactory | ||
{ | ||
public function create(Dsn $dsn): Lox24Transport | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if (!\in_array($scheme, $this->getSupportedSchemes(), true)) { | ||
throw new UnsupportedSchemeException($dsn, $scheme, $this->getSupportedSchemes()); | ||
} | ||
|
||
$user = $this->getUser($dsn); | ||
$token = $this->getPassword($dsn); | ||
$from = $dsn->getRequiredOption('from'); | ||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
return (new Lox24Transport($user, $token, $from, $dsn->getOptions(), $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['lox24']; | ||
} | ||
} |
Oops, something went wrong.