Skip to content

Commit

Permalink
[Notifier] Add Primotexto bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Samael tomas committed Apr 26, 2024
1 parent 65ccca0 commit 4af85a9
Show file tree
Hide file tree
Showing 19 changed files with 550 additions and 3 deletions.
Expand Up @@ -2185,9 +2185,9 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
->setFactory([new Reference('messenger.transport_factory'), 'createTransport'])
->setArguments([$transport['dsn'], $transport['options'] + ['transport_name' => $name], new Reference($serializerId)])
->addTag('messenger.receiver', [
'alias' => $name,
'is_failure_transport' => \in_array($name, $failureTransports, true),
]
'alias' => $name,
'is_failure_transport' => \in_array($name, $failureTransports, true),
]
)
;
$container->setDefinition($transportId = 'messenger.transport.'.$name, $transportDefinition);
Expand Down Expand Up @@ -2783,6 +2783,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
NotifierBridge\OvhCloud\OvhCloudTransportFactory::class => 'notifier.transport_factory.ovh-cloud',
NotifierBridge\PagerDuty\PagerDutyTransportFactory::class => 'notifier.transport_factory.pager-duty',
NotifierBridge\Plivo\PlivoTransportFactory::class => 'notifier.transport_factory.plivo',
NotifierBridge\Primotexto\PrimotextoTransportFactory::class => 'notifier.transport_factory.primotexto',
NotifierBridge\Pushover\PushoverTransportFactory::class => 'notifier.transport_factory.pushover',
NotifierBridge\Pushy\PushyTransportFactory::class => 'notifier.transport_factory.pushy',
NotifierBridge\Redlink\RedlinkTransportFactory::class => 'notifier.transport_factory.redlink',
Expand Down
Expand Up @@ -87,6 +87,7 @@
'orange-sms' => Bridge\OrangeSms\OrangeSmsTransportFactory::class,
'ovh-cloud' => Bridge\OvhCloud\OvhCloudTransportFactory::class,
'plivo' => Bridge\Plivo\PlivoTransportFactory::class,
'primotexto' => Bridge\Primotexto\PrimotextoTransportFactory::class,
'pushover' => Bridge\Pushover\PushoverTransportFactory::class,
'pushy' => Bridge\Pushy\PushyTransportFactory::class,
'redlink' => Bridge\Redlink\RedlinkTransportFactory::class,
Expand Down
@@ -0,0 +1,3 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.git* export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Primotexto/.gitignore
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Primotexto/CHANGELOG.md
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.2
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Primotexto/LICENSE
@@ -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.
@@ -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;
}
@@ -0,0 +1,66 @@
<?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
{
public function __construct(private array $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;
}

/**
* Planning campaign for a specific date.
*
* @return $this
*/
public function campaignDate(int $timestamp): static
{
$this->options['date'] = $timestamp;

return $this;
}

public function toArray(): array
{
return $this->options;
}
}
@@ -0,0 +1,92 @@
<?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';

public function __construct(
#[\SensitiveParameter]
private string $apiKey,
private ?string $from = null,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
) {
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);
}
}
@@ -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 src/Symfony/Component/Notifier/Bridge/Primotexto/README.md
@@ -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)

0 comments on commit 4af85a9

Please sign in to comment.