Skip to content

Commit 2ba041b

Browse files
committed
Add Sweego Notifier bridge
1 parent 24f9ef8 commit 2ba041b

21 files changed

+471
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2858,6 +2858,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
28582858
NotifierBridge\SmsSluzba\SmsSluzbaTransportFactory::class => 'notifier.transport_factory.sms-sluzba',
28592859
NotifierBridge\Smsense\SmsenseTransportFactory::class => 'notifier.transport_factory.smsense',
28602860
NotifierBridge\SpotHit\SpotHitTransportFactory::class => 'notifier.transport_factory.spot-hit',
2861+
NotifierBridge\Sweego\SweegoTransportFactory::class => 'notifier.transport_factory.sweego',
28612862
NotifierBridge\Telegram\TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
28622863
NotifierBridge\Telnyx\TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
28632864
NotifierBridge\Termii\TermiiTransportFactory::class => 'notifier.transport_factory.termii',
@@ -2925,6 +2926,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
29252926
$loader->load('notifier_webhook.php');
29262927

29272928
$webhookRequestParsers = [
2929+
NotifierBridge\Sweego\Webhook\SweegoRequestParser::class => 'notifier.webhook.request_parser.sweego',
29282930
NotifierBridge\Twilio\Webhook\TwilioRequestParser::class => 'notifier.webhook.request_parser.twilio',
29292931
NotifierBridge\Vonage\Webhook\VonageRequestParser::class => 'notifier.webhook.request_parser.vonage',
29302932
];

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
'smsense' => Bridge\Smsense\SmsenseTransportFactory::class,
108108
'smsmode' => Bridge\Smsmode\SmsmodeTransportFactory::class,
109109
'spot-hit' => Bridge\SpotHit\SpotHitTransportFactory::class,
110+
'sweego' => Bridge\Sweego\SweegoTransportFactory::class,
110111
'telnyx' => Bridge\Telnyx\TelnyxTransportFactory::class,
111112
'termii' => Bridge\Termii\TermiiTransportFactory::class,
112113
'turbo-sms' => Bridge\TurboSms\TurboSmsTransportFactory::class,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Component\Notifier\Bridge\Sweego\Webhook\SweegoRequestParser;
1415
use Symfony\Component\Notifier\Bridge\Twilio\Webhook\TwilioRequestParser;
1516
use Symfony\Component\Notifier\Bridge\Vonage\Webhook\VonageRequestParser;
1617

1718
return static function (ContainerConfigurator $container) {
1819
$container->services()
20+
->set('notifier.webhook.request_parser.sweego', SweegoRequestParser::class)
21+
->alias(SweegoRequestParser::class, 'notifier.webhook.request_parser.sweego')
22+
1923
->set('notifier.webhook.request_parser.twilio', TwilioRequestParser::class)
2024
->alias(TwilioRequestParser::class, 'notifier.webhook.request_parser.twilio')
2125

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Sweego Notifier
2+
===============
3+
4+
Provides [Sweego](https://www.sweego.io/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
SWEEGO_DSN=sweego://API_KEY@default?region=REGION&campaign_type=CAMPAIGN_TYPE
11+
```
12+
13+
where:
14+
- `API_KEY` is your Sweego API key
15+
- `REGION` is the region of the phone number (e.g. `FR`, ISO 3166-1 alpha-2 country code)
16+
- `CAMPAIGN_TYPE` is the type of the campaign (e.g. `transac`)
17+
18+
Resources
19+
---------
20+
21+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
22+
* [Report issues](https://github.com/symfony/symfony/issues) and
23+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
24+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Sweego;
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 Mathieu Santostefano <msantostefano@protonmail.com>
26+
*/
27+
final class SweegoTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.sweego.io';
30+
31+
public function __construct(
32+
#[\SensitiveParameter] private string $apiKey,
33+
private string $region,
34+
private string $campaignType,
35+
?HttpClientInterface $client = null,
36+
?EventDispatcherInterface $dispatcher = null,
37+
) {
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return \sprintf('sweego://%s?region=%s&campaign_type=%s', $this->getEndpoint(), $this->region, $this->campaignType);
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof SmsMessage && null === $message->getOptions();
49+
}
50+
51+
protected function doSend(MessageInterface $message): SentMessage
52+
{
53+
if (!$message instanceof SmsMessage) {
54+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
55+
}
56+
57+
$endpoint = \sprintf('https://%s/send', $this->getEndpoint());
58+
$response = $this->client->request('POST', $endpoint, [
59+
'headers' => [
60+
'Api-Key' => $this->apiKey,
61+
],
62+
'json' => [
63+
'recipients' => [
64+
[
65+
'num' => $message->getPhone(),
66+
'region' => $this->region,
67+
],
68+
],
69+
'message-txt' => $message->getSubject(),
70+
'channel' => 'sms',
71+
'campaign-type' => $this->campaignType,
72+
'provider' => 'sweego',
73+
],
74+
]);
75+
76+
try {
77+
$statusCode = $response->getStatusCode();
78+
} catch (TransportExceptionInterface $e) {
79+
throw new TransportException('Could not reach the remote Sweego server.', $response, 0, $e);
80+
}
81+
82+
if (200 !== $statusCode) {
83+
throw new TransportException('Unable to send the SMS.', $response);
84+
}
85+
86+
$success = $response->toArray(false);
87+
88+
$sentMessage = new SentMessage($message, (string) $this);
89+
$sentMessage->setMessageId(array_values($success['swg_uids'])[0]);
90+
91+
return $sentMessage;
92+
}
93+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Sweego;
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 Mathieu Santostefano <msantostefano@protonmail.com>
20+
*/
21+
final class SweegoTransportFactory extends AbstractTransportFactory
22+
{
23+
public function create(Dsn $dsn): SweegoTransport
24+
{
25+
$scheme = $dsn->getScheme();
26+
27+
if ('sweego' !== $scheme) {
28+
throw new UnsupportedSchemeException($dsn, 'sweego', $this->getSupportedSchemes());
29+
}
30+
31+
$apiKey = $this->getUser($dsn);
32+
$region = $dsn->getRequiredOption('region');
33+
$campaignType = $dsn->getRequiredOption('campaign_type');
34+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
35+
$port = $dsn->getPort();
36+
37+
return (new SweegoTransport($apiKey, $region, $campaignType, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
38+
}
39+
40+
protected function getSupportedSchemes(): array
41+
{
42+
return ['sweego'];
43+
}
44+
}

0 commit comments

Comments
 (0)