Skip to content

Commit

Permalink
Add Sweego Notifier bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
welcoMattic committed Sep 19, 2024
1 parent cc11de0 commit 2d58734
Show file tree
Hide file tree
Showing 22 changed files with 552 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2846,6 +2846,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
NotifierBridge\SmsSluzba\SmsSluzbaTransportFactory::class => 'notifier.transport_factory.sms-sluzba',
NotifierBridge\Smsense\SmsenseTransportFactory::class => 'notifier.transport_factory.smsense',
NotifierBridge\SpotHit\SpotHitTransportFactory::class => 'notifier.transport_factory.spot-hit',
NotifierBridge\Sweego\SweegoTransportFactory::class => 'notifier.transport_factory.sweego',
NotifierBridge\Telegram\TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
NotifierBridge\Telnyx\TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
NotifierBridge\Termii\TermiiTransportFactory::class => 'notifier.transport_factory.termii',
Expand Down Expand Up @@ -2913,6 +2914,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
$loader->load('notifier_webhook.php');

$webhookRequestParsers = [
NotifierBridge\Sweego\Webhook\SweegoRequestParser::class => 'notifier.webhook.request_parser.sweego',
NotifierBridge\Twilio\Webhook\TwilioRequestParser::class => 'notifier.webhook.request_parser.twilio',
NotifierBridge\Vonage\Webhook\VonageRequestParser::class => 'notifier.webhook.request_parser.vonage',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
'smsense' => Bridge\Smsense\SmsenseTransportFactory::class,
'smsmode' => Bridge\Smsmode\SmsmodeTransportFactory::class,
'spot-hit' => Bridge\SpotHit\SpotHitTransportFactory::class,
'sweego' => Bridge\Sweego\SweegoTransportFactory::class,
'telnyx' => Bridge\Telnyx\TelnyxTransportFactory::class,
'termii' => Bridge\Termii\TermiiTransportFactory::class,
'turbo-sms' => Bridge\TurboSms\TurboSmsTransportFactory::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\Notifier\Bridge\Sweego\Webhook\SweegoRequestParser;
use Symfony\Component\Notifier\Bridge\Twilio\Webhook\TwilioRequestParser;
use Symfony\Component\Notifier\Bridge\Vonage\Webhook\VonageRequestParser;

return static function (ContainerConfigurator $container) {
$container->services()
->set('notifier.webhook.request_parser.sweego', SweegoRequestParser::class)
->alias(SweegoRequestParser::class, 'notifier.webhook.request_parser.sweego')

->set('notifier.webhook.request_parser.twilio', TwilioRequestParser::class)
->alias(TwilioRequestParser::class, 'notifier.webhook.request_parser.twilio')

Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sweego/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.git* export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sweego/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sweego/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.2
-----

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sweego/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024-present Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
22 changes: 22 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sweego/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Sweego Notifier
===============

Provides [Sweego](https://www.sweego.io/) integration for Symfony Notifier.

DSN example
-----------

```
SWEEGO_DSN=sweego://API_KEY@default
```

where:
- `API_KEY` is your Sweego API key

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
91 changes: 91 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sweego/SweegoTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Sweego;

use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Mathieu Santostefano <[email protected]>
*/
final class SweegoTransport extends AbstractTransport
{
protected const HOST = 'api.sweego.io';

public function __construct(
#[\SensitiveParameter] private string $apiKey,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
) {
parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return \sprintf('sweego://%s', $this->getEndpoint());
}

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage && null === $message->getOptions();
}

protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
}

$endpoint = \sprintf('https://%s/send', $this->getEndpoint());
$response = $this->client->request('POST', $endpoint, [
'headers' => [
'Api-Key' => $this->apiKey,
],
'json' => [
'recipients' => [
[
'num' => $message->getPhone(),
'region' => 'FR',
],
],
'message-txt' => $message->getSubject(),
'channel' => 'sms',
'campaign-type' => 'transac',
'provider' => 'sweego',
],
]);

try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Sweego server.', $response, 0, $e);
}

if (200 !== $statusCode) {
throw new TransportException('Unable to send the SMS.', $response);
}

$success = $response->toArray(false);

$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId(array_values($success['swg_uids'])[0]);

return $sentMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Sweego;

use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Mathieu Santostefano <[email protected]>
*/
final class SweegoTransport extends AbstractTransport
{
protected const HOST = 'api.sweego.io';

public function __construct(
#[\SensitiveParameter] private string $apiKey,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
) {
parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return \sprintf('sweego://%s', $this->getEndpoint());
}

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage && null === $message->getOptions();
}

protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
}

$endpoint = \sprintf('https://%s/send', $this->getEndpoint());
$response = $this->client->request('POST', $endpoint, [
'headers' => [
'Api-Key' => $this->apiKey,
],
'json' => [
'recipients' => [
[
'num' => $message->getPhone(),
'region' => 'FR',
],
],
'message-txt' => $message->getSubject(),
'channel' => 'sms',
'campaign-type' => 'transac',
'provider' => 'sweego',
],
]);

try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Sweego server.', $response, 0, $e);
}

if (200 !== $statusCode) {
throw new TransportException('Unable to send the SMS', $response);
}

$success = $response->toArray(false);

$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId(array_values($success['swg_uids'])[0]);

return $sentMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Sweego;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;

/**
* @author Mathieu Santostefano <[email protected]>
*/
final class SweegoTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): SweegoTransport
{
$scheme = $dsn->getScheme();

if ('sweego' !== $scheme) {
throw new UnsupportedSchemeException($dsn, 'sweego', $this->getSupportedSchemes());
}

$apiKey = $this->getUser($dsn);
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

return (new SweegoTransport($apiKey, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

protected function getSupportedSchemes(): array
{
return ['sweego'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Sweego\Tests;

use Symfony\Component\Notifier\Bridge\Sweego\SweegoTransportFactory;
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;

final class SweegoTransportFactoryTest extends TransportFactoryTestCase
{
public function createFactory(): SweegoTransportFactory
{
return new SweegoTransportFactory();
}

public static function createProvider(): iterable
{
yield [
'sweego://host.test',
'sweego://[email protected]',
];
}

public static function supportsProvider(): iterable
{
yield [true, 'sweego://apiKey@default'];
yield [false, 'somethingElse://apiKey@default'];
}

public static function unsupportedSchemeProvider(): iterable
{
yield ['somethingElse://apiKey@default'];
}
}
Loading

0 comments on commit 2d58734

Please sign in to comment.