Skip to content

Commit 9b92699

Browse files
committed
Add Sweego Notifier bridge
1 parent 1066fdb commit 9b92699

21 files changed

+459
-0
lines changed

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

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

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

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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
11+
```
12+
13+
where:
14+
- `API_KEY` is your Sweego API key
15+
16+
Resources
17+
---------
18+
19+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
20+
* [Report issues](https://github.com/symfony/symfony/issues) and
21+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
22+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
?HttpClientInterface $client = null,
34+
?EventDispatcherInterface $dispatcher = null,
35+
) {
36+
parent::__construct($client, $dispatcher);
37+
}
38+
39+
public function __toString(): string
40+
{
41+
return \sprintf('sweego://%s', $this->getEndpoint());
42+
}
43+
44+
public function supports(MessageInterface $message): bool
45+
{
46+
return $message instanceof SmsMessage && null === $message->getOptions();
47+
}
48+
49+
protected function doSend(MessageInterface $message): SentMessage
50+
{
51+
if (!$message instanceof SmsMessage) {
52+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
53+
}
54+
55+
$endpoint = \sprintf('https://%s/send', $this->getEndpoint());
56+
$response = $this->client->request('POST', $endpoint, [
57+
'headers' => [
58+
'Api-Key' => $this->apiKey,
59+
],
60+
'json' => [
61+
'recipients' => [
62+
[
63+
'num' => $message->getPhone(),
64+
'region' => 'FR',
65+
],
66+
],
67+
'message-txt' => $message->getSubject(),
68+
'channel' => 'sms',
69+
'campaign-type' => 'transac',
70+
'provider' => 'sweego',
71+
],
72+
]);
73+
74+
try {
75+
$statusCode = $response->getStatusCode();
76+
} catch (TransportExceptionInterface $e) {
77+
throw new TransportException('Could not reach the remote Sweego server.', $response, 0, $e);
78+
}
79+
80+
if (200 !== $statusCode) {
81+
throw new TransportException('Unable to send the SMS.', $response);
82+
}
83+
84+
$success = $response->toArray(false);
85+
86+
$sentMessage = new SentMessage($message, (string) $this);
87+
$sentMessage->setMessageId(array_values($success['swg_uids'])[0]);
88+
89+
return $sentMessage;
90+
}
91+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
33+
$port = $dsn->getPort();
34+
35+
return (new SweegoTransport($apiKey, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
36+
}
37+
38+
protected function getSupportedSchemes(): array
39+
{
40+
return ['sweego'];
41+
}
42+
}

0 commit comments

Comments
 (0)