Skip to content

Commit 69a4a0f

Browse files
Lukas Kaltenbachfabpot
authored andcommitted
[Notifier] Add Sipgate bridge
1 parent f5dbe04 commit 69a4a0f

File tree

16 files changed

+405
-0
lines changed

16 files changed

+405
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
28012801
NotifierBridge\RingCentral\RingCentralTransportFactory::class => 'notifier.transport_factory.ring-central',
28022802
NotifierBridge\RocketChat\RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
28032803
NotifierBridge\Sendberry\SendberryTransportFactory::class => 'notifier.transport_factory.sendberry',
2804+
NotifierBridge\Sipgate\SipgateTransportFactory::class => 'notifier.transport_factory.sipgate',
28042805
NotifierBridge\SimpleTextin\SimpleTextinTransportFactory::class => 'notifier.transport_factory.simple-textin',
28052806
NotifierBridge\Sevenio\SevenIoTransportFactory::class => 'notifier.transport_factory.sevenio',
28062807
NotifierBridge\Sinch\SinchTransportFactory::class => 'notifier.transport_factory.sinch',

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
'ring-central' => Bridge\RingCentral\RingCentralTransportFactory::class,
9494
'sendberry' => Bridge\Sendberry\SendberryTransportFactory::class,
9595
'sevenio' => Bridge\Sevenio\SevenIoTransportFactory::class,
96+
'sipgate' => Bridge\Sipgate\SipgateTransportFactory::class,
9697
'simple-textin' => Bridge\SimpleTextin\SimpleTextinTransportFactory::class,
9798
'sinch' => Bridge\Sinch\SinchTransportFactory::class,
9899
'sms-biuras' => Bridge\SmsBiuras\SmsBiurasTransportFactory::class,
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Sipgate Notifier
2+
================
3+
4+
Provides [Sipgate](https://www.sipgate.de) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
SIPGATE_DSN=sipgate://TOKEN_ID:TOKEN@default?senderId=SENDER_ID
11+
```
12+
13+
where:
14+
- `TOKEN_ID` is your Sipgate API Token ID
15+
- `TOKEN` is your Sipgate API TOKEN
16+
- `SENDER_ID` is your Sipgate device ID (e.g. s1)
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)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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\Sipgate;
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 Lukas Kaltenbach <lk@wikanet.de>
26+
*/
27+
final class SipgateTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.sipgate.com';
30+
31+
public function __construct(
32+
private string $tokenId,
33+
#[\SensitiveParameter] private string $token,
34+
private ?string $senderId = null,
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('sipgate://%s?senderId=%s', $this->getEndpoint(), $this->senderId);
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof SmsMessage;
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/v2/sessions/sms', $this->getEndpoint());
58+
59+
$options = [];
60+
$options['smsId'] = $this->senderId;
61+
$options['message'] = $message->getSubject();
62+
$options['recipient'] = $message->getPhone();
63+
64+
$response = $this->client->request('POST', $endpoint, [
65+
'headers' => [
66+
'Accept' => 'application/json',
67+
'Content-Type' => 'application/json',
68+
],
69+
'auth_basic' => [$this->tokenId, $this->token],
70+
'body' => json_encode($options),
71+
]);
72+
73+
try {
74+
$statusCode = $response->getStatusCode();
75+
} catch (TransportExceptionInterface $e) {
76+
throw new TransportException('Could not reach the remote Sipgate server.', $response, 0, $e);
77+
}
78+
79+
if (204 === $statusCode) {
80+
$sentMessage = new SentMessage($message, (string) $this);
81+
82+
return $sentMessage;
83+
} elseif (401 === $statusCode) {
84+
throw new TransportException(sprintf('Unable to send SMS with Sipgate: Error code %d - tokenId or token is wrong.', $statusCode), $response);
85+
} elseif (402 === $statusCode) {
86+
throw new TransportException(sprintf('Unable to send SMS with Sipgate: Error code %d - insufficient funds.', $statusCode), $response);
87+
} elseif (403 === $statusCode) {
88+
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);
89+
}
90+
throw new TransportException(sprintf('Unable to send SMS with Sipgate: Error code %d.', $statusCode), $response);
91+
}
92+
}
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\Sipgate;
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 Lukas Kaltenbach <lk@wikanet.de>
20+
*/
21+
final class SipgateTransportFactory extends AbstractTransportFactory
22+
{
23+
public function create(Dsn $dsn): SipgateTransport
24+
{
25+
if ('sipgate' !== $dsn->getScheme()) {
26+
throw new UnsupportedSchemeException($dsn, 'sipgate', $this->getSupportedSchemes());
27+
}
28+
29+
$tokenId = $this->getUser($dsn);
30+
$token = $this->getPassword($dsn);
31+
$senderId = $dsn->getRequiredOption('senderId');
32+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
33+
$port = $dsn->getPort();
34+
35+
return (new SipgateTransport($tokenId, $token, $senderId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
36+
}
37+
38+
protected function getSupportedSchemes(): array
39+
{
40+
return ['sipgate'];
41+
}
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Sipgate\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Sipgate\SipgateTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
class SipgateTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): SipgateTransportFactory
20+
{
21+
return new SipgateTransportFactory();
22+
}
23+
24+
public static function createProvider(): iterable
25+
{
26+
yield [
27+
'sipgate://host.test?senderId=s1',
28+
'sipgate://tokenId:token@host.test?senderId=s1',
29+
];
30+
}
31+
32+
public static function supportsProvider(): iterable
33+
{
34+
yield [true, 'sipgate://tokenId:token@host.test?senderId=s1'];
35+
yield [false, 'somethingElse://tokenId:token@host.test?senderId=s1'];
36+
}
37+
38+
public static function unsupportedSchemeProvider(): iterable
39+
{
40+
yield ['somethingElse://tokenId:token@host.test?senderId=s1'];
41+
yield ['somethingElse://tokenId:token@host.test']; // missing senderId
42+
}
43+
}

0 commit comments

Comments
 (0)