-
-
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.
- Loading branch information
Samael tomas
committed
Apr 26, 2024
1 parent
65ccca0
commit 851b363
Showing
19 changed files
with
556 additions
and
3 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
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/Notifier/Bridge/Primotexto/.gitattributes
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 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.git* 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 |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/Notifier/Bridge/Primotexto/CHANGELOG.md
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. |
68 changes: 68 additions & 0 deletions
68
src/Symfony/Component/Notifier/Bridge/Primotexto/PrimotextoErrorCode.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,68 @@ | ||
<?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\Primotexto; | ||
|
||
enum PrimotextoErrorCode: int | ||
{ | ||
case NO_PHONE_NUMBER_PROVIDED = 10; | ||
case INVALID_PHONE_NUMBER_SYNTAX = 11; | ||
case NUMBER_BLACKLISTED_UNSUBSCRIBED = 12; | ||
case NUMBER_BLACKLISTED_USER_BOUNCE = 13; | ||
case NUMBER_BLACKLISTED_GLOBAL_BOUNCE = 14; | ||
case NUMBER_ALREADY_EXISTS = 15; | ||
case NO_MESSAGE_FOR_THIS_QUERY = 16; | ||
case TOO_MANY_FIELDS_SELECTED = 17; | ||
case FILE_TOO_LARGE = 18; | ||
case PHONE_NUMBER_IS_FIXED_LINE = 19; | ||
|
||
case INVALID_CHARACTERS_IN_SENDER = 20; | ||
case SENDER_TOO_SHORT = 21; | ||
case SENDER_TOO_LONG = 22; | ||
case FULL_NUMERIC_SENDER_NOT_ALLOWED = 23; | ||
|
||
case NO_MESSAGE_CONTENT_PROVIDED = 30; | ||
case INVALID_CHARACTERS_IN_MESSAGE = 31; | ||
case MESSAGE_CONTENT_TOO_LONG = 32; | ||
|
||
case INVALID_CAMPAIGN_NAME = 40; | ||
case INVALID_CAMPAIGN_PROGRAMMING_TIME = 41; | ||
case CAMPAIGN_CANNOT_BE_DELETED = 42; | ||
case CAMPAIGN_CANNOT_BE_CANCELLED = 43; | ||
case FREE_MODE_CAMPAIGN_LIMIT_REACHED = 44; | ||
case INVALID_CAMPAIGN_TAG = 45; | ||
case TAG_ALREADY_EXISTS = 46; | ||
case CAMPAIGN_NOT_FOUND = 47; | ||
case INVALID_CAMPAIGN_SEND_LIST = 48; | ||
|
||
case LIST_NOT_FOUND = 60; | ||
case INVALID_DATE_FORMAT = 61; | ||
case INVALID_SCHEDULED_DATE = 62; | ||
|
||
case API_ACCESS_DISABLED = 70; | ||
case INSUFFICIENT_CREDITS = 71; | ||
case AUTHENTICATION_FAILED = 72; | ||
case INVALID_JSON = 73; | ||
case CONTACTS_POST_LIMIT_REACHED = 74; | ||
case IDENTIFIERS_COLUMN_NOT_FOUND = 75; | ||
case QUOTA_EXCEEDED = 76; | ||
|
||
case COUNTRY_NOT_SUPPORTED = 90; | ||
case INTERNATIONAL_MODE_NEEDED = 91; | ||
case BLOCKED_ACCOUNT = 92; | ||
case USER_NOT_FOUND = 93; | ||
case USER_INFO_NOT_RETRIEVABLE = 94; | ||
case UNABLE_TO_CREATE_ACCOUNT = 95; | ||
case AUTO_INVALID_CAMPAIGN = 120; | ||
case AUTO_INVALID_CONTACT = 121; | ||
|
||
case UNKNOWN_ERROR = 1000; | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Symfony/Component/Notifier/Bridge/Primotexto/PrimotextoOptions.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,67 @@ | ||
<?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\Primotexto; | ||
|
||
use Symfony\Component\Notifier\Message\MessageOptionsInterface; | ||
|
||
/** | ||
* @author Samaël tomas <samael.tomas@gmail.com> | ||
*/ | ||
final class PrimotextoOptions implements MessageOptionsInterface | ||
{ | ||
private array $options; | ||
|
||
public function __construct(array $options = []) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
public function getRecipientId(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function campaignName(string $campaignName): static | ||
{ | ||
$this->options['campaignName'] = $campaignName; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function category(string $category): static | ||
{ | ||
$this->options['category'] = $category; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function date(int $timestamp): static | ||
{ | ||
$this->options['date'] = $timestamp; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->options; | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/Symfony/Component/Notifier/Bridge/Primotexto/PrimotextoTransport.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,97 @@ | ||
<?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\Primotexto; | ||
|
||
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 Samaël tomas <samael.tomas@gmail.com> | ||
*/ | ||
final class PrimotextoTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'api.primotexto.com'; | ||
|
||
private string $apiKey; | ||
private ?string $from; | ||
|
||
public function __construct( | ||
#[\SensitiveParameter] string $apiKey, | ||
?string $from = null, | ||
?HttpClientInterface $client = null, | ||
?EventDispatcherInterface $dispatcher = null, | ||
) { | ||
$this->apiKey = $apiKey; | ||
$this->from = $from; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof SmsMessage) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); | ||
} | ||
|
||
$options = $message->getOptions()?->toArray() ?? []; | ||
$options['from'] = $message->getFrom() ?: $this->from; | ||
$options['number'] = $message->getPhone(); | ||
$options['message'] = $message->getSubject(); | ||
|
||
$endpoint = sprintf('https://%s/v2/notification/messages/send', $this->getEndpoint()); | ||
$response = $this->client->request('POST', $endpoint, [ | ||
'headers' => [ | ||
'X-Primotexto-ApiKey' => $this->apiKey, | ||
'Content-Type' => 'application/json', | ||
], | ||
'json' => array_filter($options), | ||
]); | ||
|
||
try { | ||
$statusCode = $response->getStatusCode(); | ||
} catch (TransportExceptionInterface $e) { | ||
throw new TransportException('Could not reach the remote Primotexto server.', $response, 0, $e); | ||
} | ||
|
||
if (200 !== $statusCode) { | ||
$error = $response->toArray(false); | ||
|
||
$errorCodeValue = PrimotextoErrorCode::tryFrom((int) $error['code']) ?? PrimotextoErrorCode::UNKNOWN_ERROR; | ||
|
||
throw new TransportException(sprintf('Unable to send the SMS, error "%s" : "%s".', $error['code'], $errorCodeValue->name), $response); | ||
} | ||
|
||
$success = $response->toArray(false); | ||
|
||
$sentMessage = new SentMessage($message, (string) $this); | ||
$sentMessage->setMessageId($success['snapshotId']); | ||
|
||
return $sentMessage; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('primotexto://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : ''); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof PrimotextoOptions); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/Notifier/Bridge/Primotexto/PrimotextoTransportFactory.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,43 @@ | ||
<?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\Primotexto; | ||
|
||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
|
||
/** | ||
* @author Samaël tomas <samael.tomas@gmail.com> | ||
*/ | ||
final class PrimotextoTransportFactory extends AbstractTransportFactory | ||
{ | ||
public function create(Dsn $dsn): PrimotextoTransport | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if ('primotexto' !== $scheme) { | ||
throw new UnsupportedSchemeException($dsn, 'primotexto', $this->getSupportedSchemes()); | ||
} | ||
|
||
$apiKey = $this->getUser($dsn); | ||
$from = $dsn->getOption('from'); | ||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
return (new PrimotextoTransport($apiKey, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['primotexto']; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/Notifier/Bridge/Primotexto/README.md
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,50 @@ | ||
Primotexto Notifier | ||
================= | ||
|
||
Provides [Primotexto](https://www.primotexto.com/) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
PRIMOTEXTO_DSN=primotexto://APIKEY@default?from=FROM | ||
``` | ||
|
||
where: | ||
- `APIKEY` is your Primotexto API key | ||
- `FROM` is your sender name | ||
|
||
Adding Options to a Message | ||
--------------------------- | ||
|
||
With a Primotexto Message, you can use the `PrimotextoOptions` class to add | ||
[message options](https://www.primotexto.com/api/sms/notification.asp). | ||
|
||
```php | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Bridge\Primotexto\options; | ||
|
||
$sms = new SmsMessage('+1411111111', 'My message'); | ||
|
||
$options = (new PrimotextoOptions()) | ||
->campaignName('Code de confirmation') | ||
->category('codeConfirmation') | ||
->date(1398177000000) | ||
// ... | ||
; | ||
|
||
// Add the custom options to the sms message and send the message | ||
$sms->options($options); | ||
|
||
$texter->send($sms); | ||
``` | ||
|
||
Resources | ||
--------- | ||
|
||
* [Primotexto error codes](https://www.primotexto.com/api/plus/code_erreurs) | ||
|
||
* [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) |
Oops, something went wrong.