Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4de21f4

Browse filesBrowse files
committed
Add Sweego Notifier bridge
1 parent cc11de0 commit 4de21f4
Copy full SHA for 4de21f4

21 files changed

+459
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,6 +2846,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
28462846
NotifierBridge\SmsSluzba\SmsSluzbaTransportFactory::class => 'notifier.transport_factory.sms-sluzba',
28472847
NotifierBridge\Smsense\SmsenseTransportFactory::class => 'notifier.transport_factory.smsense',
28482848
NotifierBridge\SpotHit\SpotHitTransportFactory::class => 'notifier.transport_factory.spot-hit',
2849+
NotifierBridge\Sweego\SweegoTransportFactory::class => 'notifier.transport_factory.sweego',
28492850
NotifierBridge\Telegram\TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
28502851
NotifierBridge\Telnyx\TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
28512852
NotifierBridge\Termii\TermiiTransportFactory::class => 'notifier.transport_factory.termii',
@@ -2913,6 +2914,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
29132914
$loader->load('notifier_webhook.php');
29142915

29152916
$webhookRequestParsers = [
2917+
NotifierBridge\Sweego\Webhook\SweegoRequestParser::class => 'notifier.webhook.request_parser.sweego',
29162918
NotifierBridge\Twilio\Webhook\TwilioRequestParser::class => 'notifier.webhook.request_parser.twilio',
29172919
NotifierBridge\Vonage\Webhook\VonageRequestParser::class => 'notifier.webhook.request_parser.vonage',
29182920
];

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_webhook.php
+4Lines 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

+3Lines 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
+3Lines 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
+7Lines 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
+19Lines 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.
+22Lines 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)
+91Lines 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+
}
+42Lines 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+
}
+42Lines 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\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Sweego\SweegoTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
final class SweegoTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): SweegoTransportFactory
20+
{
21+
return new SweegoTransportFactory();
22+
}
23+
24+
public static function createProvider(): iterable
25+
{
26+
yield [
27+
'sweego://host.test',
28+
'sweego://apiKey@host.test',
29+
];
30+
}
31+
32+
public static function supportsProvider(): iterable
33+
{
34+
yield [true, 'sweego://apiKey@default'];
35+
yield [false, 'somethingElse://apiKey@default'];
36+
}
37+
38+
public static function unsupportedSchemeProvider(): iterable
39+
{
40+
yield ['somethingElse://apiKey@default'];
41+
}
42+
}
+44Lines 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\Tests;
13+
14+
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\Sweego\SweegoTransport;
16+
use Symfony\Component\Notifier\Message\ChatMessage;
17+
use Symfony\Component\Notifier\Message\SmsMessage;
18+
use Symfony\Component\Notifier\Test\TransportTestCase;
19+
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
final class SweegoTransportTest extends TransportTestCase
23+
{
24+
public static function createTransport(?HttpClientInterface $client = null, string $from = 'from'): SweegoTransport
25+
{
26+
return new SweegoTransport('apiKey', $client ?? new MockHttpClient());
27+
}
28+
29+
public static function toStringProvider(): iterable
30+
{
31+
yield ['sweego://api.sweego.io', self::createTransport()];
32+
}
33+
34+
public static function supportedMessagesProvider(): iterable
35+
{
36+
yield [new SmsMessage('0611223344', 'Hello!')];
37+
}
38+
39+
public static function unsupportedMessagesProvider(): iterable
40+
{
41+
yield [new ChatMessage('Hello!')];
42+
yield [new DummyMessage()];
43+
}
44+
}
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"event_type": "sms_sent",
3+
"timestamp": "2024-09-02T17:25:43",
4+
"swg_uid": "03-f237cd16-a013-4e35-a279-c9eaa994e82b",
5+
"event_id": "a5ccc627-6e43-4012-bb29-f1bfe3a3d13e",
6+
"channel": "sms",
7+
"client_id": "de1bbe6f-4103-47b1-8f69-94856aaba3fc",
8+
"country_code": "FR",
9+
"phone_number": "0033612345678",
10+
"sender_id": "38082",
11+
"sms_type": "transactional",
12+
"sms_price": 0.04,
13+
"campaign_id": null,
14+
"test_mode": false,
15+
"send_date": "2024-09-02T15:25:40.577165",
16+
"mobile_network_code": 1,
17+
"mobile_country_code": 208
18+
}
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Sms\SmsEvent;
4+
5+
$wh = new SmsEvent(SmsEvent::DELIVERED, '03-f237cd16-a013-4e35-a279-c9eaa994e82b', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR));
6+
$wh->setRecipientPhone('0033612345678');
7+
8+
return $wh;
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Tests\Webhook;
13+
14+
use Symfony\Component\Notifier\Bridge\Sweego\Webhook\SweegoRequestParser;
15+
use Symfony\Component\Webhook\Client\RequestParserInterface;
16+
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase;
17+
18+
class SweegoRequestParserTest extends AbstractRequestParserTestCase
19+
{
20+
protected function createRequestParser(): RequestParserInterface
21+
{
22+
return new SweegoRequestParser();
23+
}
24+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.