Skip to content

Commit 788c74e

Browse files
committed
[Notifier] Add Termii bridge
1 parent 69f46f2 commit 788c74e

File tree

17 files changed

+582
-0
lines changed

17 files changed

+582
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
use Symfony\Component\Notifier\Bridge\SpotHit\SpotHitTransportFactory;
172172
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
173173
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
174+
use Symfony\Component\Notifier\Bridge\Termii\TermiiTransportFactory;
174175
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransport;
175176
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
176177
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
@@ -2592,6 +2593,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25922593
SpotHitTransportFactory::class => 'notifier.transport_factory.spot-hit',
25932594
TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
25942595
TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
2596+
TermiiTransportFactory::class => 'notifier.transport_factory.termii',
25952597
TurboSmsTransport::class => 'notifier.transport_factory.turbo-sms',
25962598
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
25972599
VonageTransportFactory::class => 'notifier.transport_factory.vonage',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
use Symfony\Component\Notifier\Bridge\SpotHit\SpotHitTransportFactory;
5858
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
5959
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
60+
use Symfony\Component\Notifier\Bridge\Termii\TermiiTransportFactory;
6061
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransportFactory;
6162
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
6263
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
@@ -287,5 +288,9 @@
287288
->parent('notifier.transport_factory.abstract')
288289
->tag('chatter.transport_factory')
289290

291+
->set('notifier.transport_factory.termii', TermiiTransportFactory::class)
292+
->parent('notifier.transport_factory.abstract')
293+
->tag('texter.transport_factory')
294+
290295
;
291296
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore 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+
6.3
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) 2019-2022 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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Termii Notifier
2+
=============
3+
4+
Provides [Termii](https://www.termii.com) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
TERMII_DSN=termii://API_KEY@default?from=FROM&channel=CHANNEL
11+
```
12+
13+
where:
14+
15+
- `API_KEY` is your Termii API key
16+
- `FROM` is your sender
17+
- `CHANNEL` is your channel (generic, dnd, whatsapp)
18+
19+
Resources
20+
---------
21+
22+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
23+
* [Report issues](https://github.com/symfony/symfony/issues) and
24+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
25+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <fabien@symfony.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Symfony\Component\Notifier\Bridge\Termii;
12+
13+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
14+
15+
/**
16+
* @author gnito-org <https://github.com/gnito-org>
17+
*/
18+
final class TermiiOptions implements MessageOptionsInterface
19+
{
20+
private array $options;
21+
22+
public function __construct(array $options = [])
23+
{
24+
$this->options = $options;
25+
}
26+
27+
public function getChannel(): ?string
28+
{
29+
return $this->options['channel'] ?? null;
30+
}
31+
32+
public function getFrom(): ?string
33+
{
34+
return $this->options['from'] ?? null;
35+
}
36+
37+
public function getMediaCaption(): ?string
38+
{
39+
return $this->options['media_caption'] ?? null;
40+
}
41+
42+
public function getMediaUrl(): ?string
43+
{
44+
return $this->options['media_url'] ?? null;
45+
}
46+
47+
public function getRecipientId(): ?string
48+
{
49+
return $this->options['recipient_id'] ?? null;
50+
}
51+
52+
public function getType(): ?string
53+
{
54+
return $this->options['type'] ?? null;
55+
}
56+
57+
public function setChannel(string $channel): self
58+
{
59+
$this->options['channel'] = $channel;
60+
return $this;
61+
}
62+
63+
public function setFrom(string $from): self
64+
{
65+
$this->options['from'] = $from;
66+
return $this;
67+
}
68+
69+
public function setMediaCaption(string $mediaCaption): self
70+
{
71+
$this->options['media_caption'] = $mediaCaption;
72+
return $this;
73+
}
74+
75+
public function setMediaUrl(string $mediaUrl): self
76+
{
77+
$this->options['media_url'] = $mediaUrl;
78+
return $this;
79+
}
80+
81+
public function setRecipientId(string $id): self
82+
{
83+
$this->options['recipient_id'] = $id;
84+
return $this;
85+
}
86+
87+
public function setType(string $type): self
88+
{
89+
$this->options['type'] = $type;
90+
return $this;
91+
}
92+
93+
public function toArray(): array
94+
{
95+
return $this->options;
96+
}
97+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <fabien@symfony.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Symfony\Component\Notifier\Bridge\Termii;
12+
13+
use Symfony\Component\HttpClient\Exception\JsonException;
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Message\SmsMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
23+
use Symfony\Contracts\HttpClient\HttpClientInterface;
24+
25+
/**
26+
* @author gnito-org <https://github.com/gnito-org>
27+
*/
28+
final class TermiiTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'api.ng.termii.com';
31+
32+
public function __construct(
33+
#[\SensitiveParameter] private readonly string $apiKey,
34+
private readonly string $from,
35+
private readonly string $channel,
36+
HttpClientInterface $client = null,
37+
EventDispatcherInterface $dispatcher = null
38+
) {
39+
parent::__construct($client, $dispatcher);
40+
}
41+
42+
public function __toString(): string
43+
{
44+
return sprintf('termii://%s?from=%s&channel=%s', $this->getEndpoint(), $this->from, $this->channel);
45+
}
46+
47+
public function supports(MessageInterface $message): bool
48+
{
49+
return $message instanceof SmsMessage;
50+
}
51+
52+
protected function doSend(MessageInterface $message): SentMessage
53+
{
54+
if (!$message instanceof SmsMessage) {
55+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
56+
}
57+
$opts = $message->getOptions();
58+
$options = $opts ? $opts->toArray() : [];
59+
$options['api_key'] = $this->apiKey;
60+
$options['sms'] = $message->getSubject();
61+
$options['from'] = $options['from'] ?? $this->from;
62+
$options['to'] = $message->getPhone();
63+
$options['channel'] = $options['channel'] ?? $this->channel;
64+
$options['type'] = $options['type'] ?? 'plain';
65+
if (isset($options['media_url'])) {
66+
$options['media']['url'] = $options['media_url'] ?? null;
67+
$options['media']['caption'] = $options['media_caption'] ?? null;
68+
unset($options['media_url'], $options['media_caption']);
69+
}
70+
if (!preg_match('/^[a-zA-Z0-9\s]{3,11}$/', $options['from']) && !preg_match('/^\+?[1-9]\d{1,14}$/', $options['from'])) {
71+
throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $this->from));
72+
}
73+
$endpoint = sprintf('https://%s/api/sms/send', $this->getEndpoint());
74+
$response = $this->client->request(
75+
'POST',
76+
$endpoint,
77+
[
78+
'json' => array_filter($options),
79+
]
80+
);
81+
try {
82+
$statusCode = $response->getStatusCode();
83+
} catch (TransportExceptionInterface $e) {
84+
throw new TransportException('Could not reach the remote Termii server.', $response, 0, $e);
85+
}
86+
if (200 !== $statusCode) {
87+
try {
88+
$error = $response->toArray(false);
89+
} catch (JsonException) {
90+
$error['message'] = $response->getContent(false);
91+
}
92+
throw new TransportException(sprintf('Unable to send the SMS - status code: %s: %s', $statusCode, $error['message'] ?? 'unknown error'), $response);
93+
}
94+
$success = $response->toArray(false);
95+
$sentMessage = new SentMessage($message, (string) $this);
96+
$sentMessage->setMessageId($success['message_id']);
97+
98+
return $sentMessage;
99+
}
100+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <fabien@symfony.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Symfony\Component\Notifier\Bridge\Termii;
12+
13+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
14+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
15+
use Symfony\Component\Notifier\Transport\Dsn;
16+
17+
/**
18+
* @author gnito-org <https://github.com/gnito-org>
19+
*/
20+
final class TermiiTransportFactory extends AbstractTransportFactory
21+
{
22+
private const TRANSPORT_SCHEME = 'termii';
23+
24+
public function create(Dsn $dsn): TermiiTransport
25+
{
26+
$scheme = $dsn->getScheme();
27+
if (self::TRANSPORT_SCHEME !== $scheme) {
28+
throw new UnsupportedSchemeException($dsn, self::TRANSPORT_SCHEME, $this->getSupportedSchemes());
29+
}
30+
$apiKey = $this->getUser($dsn);
31+
$from = $dsn->getRequiredOption('from');
32+
$channel = $dsn->getRequiredOption('channel');
33+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34+
$port = $dsn->getPort();
35+
36+
return (new TermiiTransport($apiKey, $from, $channel, $this->client, $this->dispatcher))
37+
->setHost($host)
38+
->setPort($port)
39+
;
40+
}
41+
42+
protected function getSupportedSchemes(): array
43+
{
44+
return [self::TRANSPORT_SCHEME];
45+
}
46+
}

0 commit comments

Comments
 (0)