8000 [Notifier] Add Bandwidth bridge · symfony/symfony@604fd9f · GitHub
[go: up one dir, main page]

Skip to content

Commit 604fd9f

Browse files
gnito-orgfabpot
authored andcommitted
[Notifier] Add Bandwidth bridge
1 parent 69f46f2 commit 604fd9f

17 files changed

+597
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
use Symfony\Component\Mime\MimeTypes;
128128
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
129129
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
130+
use Symfony\Component\Notifier\Bridge\Bandwidth\BandwidthTransportFactory;
130131
use Symfony\Component\Notifier\Bridge\Chatwork\ChatworkTransportFactory;
131132
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
132133
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
@@ -2548,6 +2549,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25482549
$classToServices = [
25492550
AllMySmsTransportFactory::class => 'notifier.transport_factory.all-my-sms',
25502551
AmazonSnsTransportFactory::class => 'notifier.transport_factory.amazon-sns',
2552+
BandwidthTransportFactory::class => 'notifier.transport_factory.bandwidth',
25512553
ChatworkTransportFactory::class => 'notifier.transport_factory.chatwork',
25522554
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
25532555
ContactEveryoneTransportFactory::class => 'notifier.transport_factory.contact-everyone',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
1515
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
16+
use Symfony\Component\Notifier\Bridge\Bandwidth\BandwidthTransportFactory;
1617
use Symfony\Component\Notifier\Bridge\Chatwork\ChatworkTransportFactory;
1718
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
1819
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
@@ -287,5 +288,9 @@
287288
->parent('notifier.transport_factory.abstract')
288289
->tag('chatter.transport_factory')
289290

291+
->set('notifier.transport_factory.bandwidth', BandwidthTransportFactory::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: 140 additions & 0 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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\Bandwidth;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class BandwidthOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getAccountId(): ?string
29+
{
30+
return $this->options['account_id'] ?? null;
31+
}
32+
33+
public function getApplicationId(): ?string
34+
{
35+
return $this->options['application_id'] ?? null;
36+
}
37+
38+
public function getExpiration(): ?string
39+
{
40+
return $this->options['expiration'] ?? null;
41+
}
42+
43+
public function getFrom(): ?string
44+
{
45+
return $this->options['from'] ?? null;
46+
}
47+
48+
public function getMedia(): ?array
49+
{
50+
return $this->options['media'] ?? null;
51+
}
52+
53+
public function getPriority(): ?string
54+
{
55+
return $this->options['priority'] ?? null;
56+
}
57+
58+
public function getRecipientId(): ?string
59+
{
60+
return $this->options['recipient_id'] ?? null;
61+
}
62+
63+
public function getTag(): ?string
64+
{
65+
return $this->options['tag'] ?? null;
66+
}
67+
68+
public function getTo(): ?array
69+
{
70+
return $this->options['to'] ?? null;
71+
}
72+
73+
public function setAccountId(string $accountId): self
74+
{
75+
$this->options['account_id'] = $accountId;
76+
77+
return $this;
78+
}
79+
80+
public function setApplicationId(string $applicationId): self
81+
{
82+
$this->options['application_id'] = $applicationId;
83+
84+
return $this;
85+
}
86+
87+
public function setExpiration(string $expiration): self
88+
{
89+
$this->options['expiration'] = $expiration;
90+
91+
return $this;
92+
}
93+
94+
public function setFrom(string $from): self
95+
{
96+
$this->options['from'] = $from;
97+
98+
return $this;
99+
}
100+
101+
public function setMedia(array $media): self
102+
{
103+
$this->options['media'] = $media;
104+
105+
return $this;
106+
}
107+
108+
public function setPriority(string $priority): self
109+
{
110+
$this->options['priority'] = $priority;
111+
112+
return $this;
113+
}
114+
115+
public function setRecipientId(string $id): self
116+
{
117+
$this->options['recipient_id'] = $id;
118+
119+
return $this;
120+
}
121+
122+
public function setTag(string $tag): self
123+
{
124+
$this->options['tag'] = $tag;
125+
126+
return $this;
127+
}
128+
129+
public function setTo(array $to): self
130+
{
131+
$this->options['to'] = $to;
132+
133+
return $this;
134+
}
135+
136+
public function toArray(): array
137+
{
138+
return $this->options;
139+
}
140+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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\Bandwidth;
13+
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 BandwidthTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'messaging.bandwidth.com';
31+
32+
public function __construct(
33+
private readonly string $username,
34+
#[\SensitiveParameter] private readonly string $password,
35+
private readonly string $from,
36+
private readonly string $accountId,
37+
private readonly string $applicationId,
38+
private readonly ?string $priority,
39+
HttpClientInterface $client = null,
40+
EventDispatcherInterface $dispatcher = null
41+
) {
42+
parent::__construct($client, $dispatcher);
43+
}
44+
45+
public function __toString(): string
46+
{
47+
return sprintf('bandwidth://%s?from=%s&account_id=%s&application_id=%s', $this->getEndpoint(), $this->from, $this->accountId, $this->applicationId).($this->priority ? sprintf('&priority=%s', $this->priority) : null);
48+
}
49+
50+
public function supports(MessageInterface $message): bool
51+
{
52+
return $message instanceof SmsMessage;
53+
}
54+
55+
/**
56+
* https://dev.bandwidth.com/apis/messaging/#tag/Messages/operation/createMessage.
57+
*/
58+
protected function doSend(MessageInterface $message): SentMessage
59+
{
60+
if (!$message instanceof SmsMessage) {
61+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
62+
}
63+
$opts = $message->getOptions();
64+
$options = $opts ? $opts->toArray() : [];
65+
$options['text'] = $message->getSubject();
66+
$options['from'] = $options['from'] ?? $this->from;
67+
$options['to'] = $options['to'] ?? [$message->getPhone()];
68+
$options['account_id'] = $options['account_id'] ?? $this->accountId;
69+
$options['applicationId'] = $options['application_id'] ?? $this->applicationId;
70+
unset($options['application_id']);
71+
72+
if (!isset($options['priority']) && $this->priority) {
73+
$options['priority'] = $this->priority;
74+
}
75+
76+
if (!preg_match('/^\+[1-9]\d{1,14}$/', $this->from)) {
77+
throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number. The number must be in E.164 format.', $this->from));
78+
}
79+
80+
if (!preg_match('/^\+[1-9]\d{1,14}$/', $message->getPhone())) {
81+
throw new InvalidArgumentException(sprintf('The "To" number "%s" is not a valid phone number. The number must be in E.164 format.', $message->getPhone()));
82+
}
83+
$endpoint = sprintf('https://%s/api/v2/users/%s/messages', $this->getEndpoint(), $options['account_id']);
84+
unset($options['accountId']);
85+
86+
$response = $this->client->request('POST', $endpoint, [
87+
'auth_basic' => $this->username.':'.$this->password,
88+
'json' => array_filter($options),
89+
]);
90+
91+
try {
92+
$statusCode = $response->getStatusCode();
93+
} catch (TransportExceptionInterface $e) {
94+
throw new TransportException('Could not reach the remote Bandwidth server.', $response, 0, $e);
95+
}
96+
97+
if (202 !== $statusCode) {
98+
$error = $response->toArray(false);
99+
throw new TransportException(sprintf('Unable to send the SMS - "%s" - "%s".', $error['type'], $error['description']), $response);
100+
}
101+
102+
$success = $response->toArray(false);
103+
$sentMessage = new SentMessage($message, (string) $this);
104+
$sentMessage->setMessageId($success['id']);
105+
106+
return $sentMessage;
107+
}
108+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Bandwidth;
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 gnito-org <https://github.com/gnito-org>
20+
*/
21+
final class BandwidthTransportFactory extends AbstractTransportFactory
22+
{
23+
private const TRANSPORT_SCHEME = 'bandwidth';
24+
25+
public function create(Dsn $dsn): BandwidthTransport
26+
{
27+
$scheme = $dsn->getScheme();
28+
29+
if (self::TRANSPORT_SCHEME !== $scheme) {
30+
throw new UnsupportedSchemeException($dsn, self::TRANSPORT_SCHEME, $this->getSupportedSchemes());
31+
}
32+
33+
$username = $this->getUser($dsn);
34+
$password = $this->getPassword($dsn);
35+
$from = $dsn->getRequiredOption('from');
36+
$accountId = $dsn->getRequiredOption('account_id');
37+
$applicationId = $dsn->getRequiredOption('application_id');
38+
$priority = $dsn->getOption('priority');
39+
< 741A span class=pl-c1>$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
40+
$port = $dsn->getPort();
41+
42+
return (new BandwidthTransport($username, $password, $from, $accountId, $applicationId, $priority, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
43+
}
44+
45+
protected function getSupportedSchemes(): array
46+
{
47+
return [self::TRANSPORT_SCHEME];
48+
}
49+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
====== F438 ===
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) 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Bandwidth Notifier
2+
==================
3+
4+
Provides [Bandwidth](https://www.bandwidth.com) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
BANDWIDTH_DSN=bandwidth://USERNAME:PASSWORD@default?from=FROM&account_id=ACCOUNT_ID&application_id=APPLICATION_ID&priority=PRIORITY
11+
```
12+
13+
where:
14+
15+
- `USERNAME` is your Bandwidth username
16+
- `PASSWORD` is your Bandwidth password
17+
- `FROM` is your sender
18+
- `ACCOUNT_ID` is your account ID
19+
- `APPLICATION_ID` is your application ID
20+
- `PRIORITY` is your priority (optional)
21+
22+
Resources
23+
---------
24+
25+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
26+
* [Report issues](https://github.com/symfony/symfony/issues) and
27+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
28+
in the [main Symfony repository](https://github.com/symfony/symfony)

0 commit comments

Comments
 (0)
0