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 2ba041b

Browse filesBrowse files
committed
Add Sweego Notifier bridge
1 parent 24f9ef8 commit 2ba041b
Copy full SHA for 2ba041b

21 files changed

+471
-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
@@ -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

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.
+24Lines 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)
+93Lines 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+
}
+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;
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+
}
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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?region=FR&campaign_type=transac',
28+
'sweego://apiKey@host.test?region=FR&campaign_type=transac',
29+
];
30+
}
31+
32+
public static function missingRequiredOptionProvider(): iterable
33+
{
34+
yield 'missing option: region' => ['sweego://apiKey@default?campaign_type=transac'];
35+
yield 'missing option: campaign_type' => ['sweego://apiKey@default?region=FR'];
36+
}
37+
38+
public static function supportsProvider(): iterable
39+
{
40+
yield [true, 'sweego://apiKey@default'];
41+
yield [false, 'somethingElse://apiKey@default'];
42+
}
43+
44+
public static function unsupportedSchemeProvider(): iterable
45+
{
46+
yield ['somethingElse://apiKey@default'];
47+
}
48+
}
+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', 'FR', 'transac', $client ?? new MockHttpClient());
27+
}
28+
29+
public static function toStringProvider(): iterable
30+
{
31+
yield ['sweego://api.sweego.io?region=FR&campaign_type=transac', 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.