Skip to content

[Notifier] Add Sipgate bridge #57627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2801,6 +2801,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
NotifierBridge\RingCentral\RingCentralTransportFactory::class => 'notifier.transport_factory.ring-central',
NotifierBridge\RocketChat\RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
NotifierBridge\Sendberry\SendberryTransportFactory::class => 'notifier.transport_factory.sendberry',
NotifierBridge\Sipgate\SipgateTransportFactory::class => 'notifier.transport_factory.sipgate',
NotifierBridge\SimpleTextin\SimpleTextinTransportFactory::class => 'notifier.transport_factory.simple-textin',
NotifierBridge\Sevenio\SevenIoTransportFactory::class => 'notifier.transport_factory.sevenio',
NotifierBridge\Sinch\SinchTransportFactory::class => 'notifier.transport_factory.sinch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
'ring-central' => Bridge\RingCentral\RingCentralTransportFactory::class,
'sendberry' => Bridge\Sendberry\SendberryTransportFactory::class,
'sevenio' => Bridge\Sevenio\SevenIoTransportFactory::class,
'sipgate' => Bridge\Sipgate\SipgateTransportFactory::class,
'simple-textin' => Bridge\SimpleTextin\SimpleTextinTransportFactory::class,
'sinch' => Bridge\Sinch\SinchTransportFactory::class,
'sms-biuras' => Bridge\SmsBiuras\SmsBiurasTransportFactory::class,
Expand Down
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/Sipgate/.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/Sipgate/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/Sipgate/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.
24 changes: 24 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sipgate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Sipgate Notifier
================

Provides [Sipgate](https://www.sipgate.de) integration for Symfony Notifier.

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

```
SIPGATE_DSN=sipgate://TOKEN_ID:TOKEN@default?senderId=SENDER_ID
```

where:
- `TOKEN_ID` is your Sipgate API Token ID
- `TOKEN` is your Sipgate API TOKEN
- `SENDER_ID` is your Sipgate device ID (e.g. s1)

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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

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

namespace Symfony\Component\Notifier\Bridge\Sipgate;

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 Lukas Kaltenbach <lk@wikanet.de>
*/
final class SipgateTransport extends AbstractTransport
{
protected const HOST = 'api.sipgate.com';

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

public function __toString(): string
{
return sprintf('sipgate://%s?senderId=%s', $this->getEndpoint(), $this->senderId);
}

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage;
}

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

$endpoint = sprintf('https://%s/v2/sessions/sms', $this->getEndpoint());

$options = [];
$options['smsId'] = $this->senderId;
$options['message'] = $message->getSubject();
$options['recipient'] = $message->getPhone();

$response = $this->client->request('POST', $endpoint, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'auth_basic' => [$this->tokenId, $this->token],
'body' => json_encode($options),
]);

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

if (204 === $statusCode) {
$sentMessage = new SentMessage($message, (string) $this);

return $sentMessage;
} elseif (401 === $statusCode) {
throw new TransportException(sprintf('Unable to send SMS with Sipgate: Error code %d - tokenId or token is wrong.', $statusCode), $response);
} elseif (402 === $statusCode) {
throw new TransportException(sprintf('Unable to send SMS with Sipgate: Error code %d - insufficient funds.', $statusCode), $response);
} elseif (403 === $statusCode) {
throw new TransportException(sprintf('Unable to send SMS with Sipgate: Error code %d - no permisssion to use sms feature or password must be reset or senderId is wrong.', $statusCode), $response);
}
throw new TransportException(sprintf('Unable to send SMS with Sipgate: Error code %d.', $statusCode), $response);
}
}
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 <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Sipgate;

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

/**
* @author Lukas Kaltenbach <lk@wikanet.de>
*/
final class SipgateTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): SipgateTransport
{
if ('sipgate' !== $dsn->getScheme()) {
throw new UnsupportedSchemeException($dsn, 'sipgate', $this->getSupportedSchemes());
}

$tokenId = $this->getUser($dsn);
$token = $this->getPassword($dsn);
$senderId = $dsn->getRequiredOption('senderId');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

return (new SipgateTransport($tokenId, $token, $senderId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

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

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

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

use Symfony\Component\Notifier\Bridge\Sipgate\SipgateTransportFactory;
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;

class SipgateTransportFactoryTest extends TransportFactoryTestCase
{
public function createFactory(): SipgateTransportFactory
{
return new SipgateTransportFactory();
}

public static function createProvider(): iterable
{
yield [
'sipgate://host.test?senderId=s1',
'sipgate://tokenId:token@host.test?senderId=s1',
];
}

public static function supportsProvider(): iterable
{
yield [true, 'sipgate://tokenId:token@host.test?senderId=s1'];
yield [false, 'somethingElse://tokenId:token@host.test?senderId=s1'];
}

public static function unsupportedSchemeProvider(): iterable
{
yield ['somethingElse://tokenId:token@host.test?senderId=s1'];
yield ['somethingElse://tokenId:token@host.test']; // missing senderId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

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

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

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Notifier\Bridge\Sipgate\SipgateTransport;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

class SipgateTransportTest extends TransportTestCase
{
public static function createTransport(?HttpClientInterface $client = null): SipgateTransport
{
return new SipgateTransport('tokenid', 'token', 's1', $client ?? new MockHttpClient());
}

public static function toStringProvider(): iterable
{
yield ['sipgate://api.sipgate.com?senderId=s1', self::createTransport()];
}

public static function supportedMessagesProvider(): iterable
{
yield [new SmsMessage('+49123456789', 'Hallo!')];
}

public static function unsupportedMessagesProvider(): iterable
{
yield [new ChatMessage('Hallo!')];
yield [new DummyMessage()];
}

public function testSendSuccessfully()
{
$response = new MockResponse('', ['http_code' => 204]);

$client = new MockHttpClient($response);
$transport = $this->createTransport($client);

$sentMessage = $transport->send(new SmsMessage('+49123456789', 'Hallo!'));
$this->assertInstanceOf(SentMessage::class, $sentMessage);
}

/**
* @dataProvider errorProvider
*/
public function testExceptionIsThrownWhenSendFailed(int $statusCode, string $content, string $expectedExceptionMessage)
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn($statusCode);
$response->method('getContent')->willReturn($content);
$client = new MockHttpClient($response);
$transport = $this->createTransport($client);

$this->expectException(TransportException::class);
$this->expectExceptionMessage($expectedExceptionMessage);

$transport->send(new SmsMessage('+49123456789', 'Hallo!'));
}

public static function errorProvider(): iterable
{
yield [
401,
'',
'Unable to send SMS with Sipgate: Error code 401 - tokenId or token is wrong.',
];
yield [
402,
'',
'Unable to send SMS with Sipgate: Error code 402 - insufficient funds.',
];
yield [
403,
'',
'Unable to send SMS with Sipgate: Error code 403 - no permisssion to use sms feature or password must be reset or senderId is wrong.',
];
yield [
415,
'',
'Unable to send SMS with Sipgate: Error code 415.',
];
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sipgate/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "symfony/sipgate-notifier",
"type": "symfony-notifier-bridge",
"description": "Symfony Sipgate Notifier Bridge",
"keywords": ["sms", "sipgate", "notifier"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Lukas Kaltenbach",
"email": "lk@wikanet.de"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": ">=8.2",
"symfony/http-client": "^6.4|^7.0",
"symfony/notifier": "^7.2"
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Notifier\\Bridge\\Sipgate\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev"
}
Loading
Loading