Skip to content

Commit f7c87b0

Browse files
committed
feature symfony#54747 [Notifier] Add Primotexto bridge (Samael tomas)
This PR was merged into the 7.2 branch. Discussion ---------- [Notifier] Add Primotexto bridge | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | none | License | MIT This PR adds a new bridge for [Primotexto](https://primotexto.com/). Based on documentation available here: https://www.primotexto.com/ <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Commits ------- 601e068 [Notifier] Add Primotexto bridge
2 parents 8b5320a + 601e068 commit f7c87b0

19 files changed

+541
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,6 +2792,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
27922792
NotifierBridge\OvhCloud\OvhCloudTransportFactory::class => 'notifier.transport_factory.ovh-cloud',
27932793
NotifierBridge\PagerDuty\PagerDutyTransportFactory::class => 'notifier.transport_factory.pager-duty',
27942794
NotifierBridge\Plivo\PlivoTransportFactory::class => 'notifier.transport_factory.plivo',
2795+
NotifierBridge\Primotexto\PrimotextoTransportFactory::class => 'notifier.transport_factory.primotexto',
27952796
NotifierBridge\Pushover\PushoverTransportFactory::class => 'notifier.transport_factory.pushover',
27962797
NotifierBridge\Pushy\PushyTransportFactory::class => 'notifier.transport_factory.pushy',
27972798
NotifierBridge\Redlink\RedlinkTransportFactory::class => 'notifier.transport_factory.redlink',

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
'orange-sms' => Bridge\OrangeSms\OrangeSmsTransportFactory::class,
8888
'ovh-cloud' => Bridge\OvhCloud\OvhCloudTransportFactory::class,
8989
'plivo' => Bridge\Plivo\PlivoTransportFactory::class,
90+
'primotexto' => Bridge\Primotexto\PrimotextoTransportFactory::class,
9091
'pushover' => Bridge\Pushover\PushoverTransportFactory::class,
9192
'pushy' => Bridge\Pushy\PushyTransportFactory::class,
9293
'redlink' => Bridge\Redlink\RedlinkTransportFactory::class,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.git* export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
7.2
5+
---
6+
7+
* Add the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2024-present Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Primotexto;
13+
14+
enum PrimotextoErrorCode: int
15+
{
16+
case NO_PHONE_NUMBER_PROVIDED = 10;
17+
case INVALID_PHONE_NUMBER_SYNTAX = 11;
18+
case NUMBER_BLACKLISTED_UNSUBSCRIBED = 12;
19+
case NUMBER_BLACKLISTED_USER_BOUNCE = 13;
20+
case NUMBER_BLACKLISTED_GLOBAL_BOUNCE = 14;
21+
case NUMBER_ALREADY_EXISTS = 15;
22+
case NO_MESSAGE_FOR_THIS_QUERY = 16;
23+
case TOO_MANY_FIELDS_SELECTED = 17;
24+
case FILE_TOO_LARGE = 18;
25+
case PHONE_NUMBER_IS_FIXED_LINE = 19;
26+
case INVALID_CHARACTERS_IN_SENDER = 20;
27+
case SENDER_TOO_SHORT = 21;
28+
case SENDER_TOO_LONG = 22;
29+
case FULL_NUMERIC_SENDER_NOT_ALLOWED = 23;
30+
case NO_MESSAGE_CONTENT_PROVIDED = 30;
31+
case INVALID_CHARACTERS_IN_MESSAGE = 31;
32+
case MESSAGE_CONTENT_TOO_LONG = 32;
33+
case INVALID_CAMPAIGN_NAME = 40;
34+
case INVALID_CAMPAIGN_PROGRAMMING_TIME = 41;
35+
case CAMPAIGN_CANNOT_BE_DELETED = 42;
36+
case CAMPAIGN_CANNOT_BE_CANCELLED = 43;
37+
case FREE_MODE_CAMPAIGN_LIMIT_REACHED = 44;
38+
case INVALID_CAMPAIGN_TAG = 45;
39+
case TAG_ALREADY_EXISTS = 46;
40+
case CAMPAIGN_NOT_FOUND = 47;
41+
case INVALID_CAMPAIGN_SEND_LIST = 48;
42+
case LIST_NOT_FOUND = 60;
43+
case INVALID_DATE_FORMAT = 61;
44+
case INVALID_SCHEDULED_DATE = 62;
45+
case API_ACCESS_DISABLED = 70;
46+
case INSUFFICIENT_CREDITS = 71;
47+
case AUTHENTICATION_FAILED = 72;
48+
case INVALID_JSON = 73;
49+
case CONTACTS_POST_LIMIT_REACHED = 74;
50+
case IDENTIFIERS_COLUMN_NOT_FOUND = 75;
51+
case QUOTA_EXCEEDED = 76;
52+
case COUNTRY_NOT_SUPPORTED = 90;
53+
case INTERNATIONAL_MODE_NEEDED = 91;
54+
case BLOCKED_ACCOUNT = 92;
55+
case USER_NOT_FOUND = 93;
56+
case USER_INFO_NOT_RETRIEVABLE = 94;
57+
case UNABLE_TO_CREATE_ACCOUNT = 95;
58+
case AUTO_INVALID_CAMPAIGN = 120;
59+
case AUTO_INVALID_CONTACT = 121;
60+
case UNKNOWN_ERROR = 1000;
61+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Primotexto;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Samaël Tomas <[email protected]>
18+
*/
19+
final class PrimotextoOptions implements MessageOptionsInterface
20+
{
21+
public function __construct(
22+
private array $options = [],
23+
) {
24+
}
25+
26+
public function getRecipientId(): ?string
27+
{
28+
return null;
29+
}
30+
31+
/**
32+
* @return $this
33+
*/
34+
public function campaignName(string $campaignName): static
35+
{
36+
$this->options['campaignName'] = $campaignName;
37+
38+
return $this;
39+
}
40+
41+
/**
42+
* @return $this
43+
*/
44+
public function category(string $category): static
45+
{
46+
$this->options['category'] = $category;
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* Planning campaign for a specific date.
53+
*
54+
* @return $this
55+
*/
56+
public function campaignDate(int $timestamp): static
57+
{
58+
$this->options['date'] = $timestamp;
59+
60+
return $this;
61+
}
62+
63+
public function toArray(): array
64+
{
65+
return $this->options;
66+
}
67+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Primotexto;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SentMessage;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Samaël Tomas <[email protected]>
26+
*/
27+
final class PrimotextoTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.primotexto.com';
30+
31+
public function __construct(
32+
#[\SensitiveParameter]
33+
private string $apiKey,
34+
private ?string $from = null,
35+
?HttpClientInterface $client = null,
36+
?EventDispatcherInterface $dispatcher = null,
37+
) {
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
protected function doSend(MessageInterface $message): SentMessage
42+
{
43+
if (!$message instanceof SmsMessage) {
44+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
45+
}
46+
47+
$options = $message->getOptions()?->toArray() ?? [];
48+
$options['from'] = $message->getFrom() ?: $this->from;
49+
$options['number'] = $message->getPhone();
50+
$options['message'] = $message->getSubject();
51+
52+
$endpoint = sprintf('https://%s/v2/notification/messages/send', $this->getEndpoint());
53+
$response = $this->client->request('POST', $endpoint, [
54+
'headers' => [
55+
'X-Primotexto-ApiKey' => $this->apiKey,
56+
'Content-Type' => 'application/json',
57+
],
58+
'json' => array_filter($options),
59+
]);
60+
61+
try {
62+
$statusCode = $response->getStatusCode();
63+
} catch (TransportExceptionInterface $e) {
64+
throw new TransportException('Could not reach the remote Primotexto server.', $response, 0, $e);
65+
}
66+
67+
if (200 !== $statusCode) {
68+
$error = $response->toArray(false);
69+
70+
$errorCodeValue = PrimotextoErrorCode::tryFrom((int) $error['code']) ?? PrimotextoErrorCode::UNKNOWN_ERROR;
71+
72+
throw new TransportException(sprintf('Unable to send the SMS, error "%s" : "%s".', $error['code'], $errorCodeValue->name), $response);
73+
}
74+
75+
$success = $response->toArray(false);
76+
77+
$sentMessage = new SentMessage($message, (string) $this);
78+
$sentMessage->setMessageId($success['snapshotId']);
79+
80+
return $sentMessage;
81+
}
82+
83+
public function __toString(): string
84+
{
85+
return sprintf('primotexto://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : '');
86+
}
87+
88+
public function supports(MessageInterface $message): bool
89+
{
90+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof PrimotextoOptions);
91+
}
92+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Primotexto;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
18+
/**
19+
* @author Samaël Tomas <[email protected]>
20+
*/
21+
final class PrimotextoTransportFactory extends AbstractTransportFactory
22+
{
23+
public function create(Dsn $dsn): PrimotextoTransport
24+
{
25+
$scheme = $dsn->getScheme();
26+
27+
if ('primotexto' !== $scheme) {
28+
throw new UnsupportedSchemeException($dsn, 'primotexto', $this->getSupportedSchemes());
29+
}
30+
31+
$apiKey = $this->getUser($dsn);
32+
$from = $dsn->getOption('from');
33+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34+
$port = $dsn->getPort();
35+
36+
return (new PrimotextoTransport($apiKey, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
37+
}
38+
39+
protected function getSupportedSchemes(): array
40+
{
41+
return ['primotexto'];
42+
}
43+
}

0 commit comments

Comments
 (0)