8000 feature #48394 [Notifier] Add Plivo bridge (gnito-org) · symfony/symfony@3751b28 · GitHub
[go: up one dir, main page]

Skip to content
< 10000 div class="d-none">

Commit 3751b28

Browse files
committed
feature #48394 [Notifier] Add Plivo bridge (gnito-org)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Notifier] Add Plivo bridge | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#17484 | Recipe | symfony/recipes#1154 This PR add the [Plivo](https://www.plivo.com) Notifier bridge (SMS). <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- 72e8416 [Notifier] Add Plivo bridge
2 parents 81df54d + 72e8416 commit 3751b28

File tree

17 files changed

+588
-0
lines changed

17 files changed

+588
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalTransportFactory;
160160
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransportFactory;
161161
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
162+
use Symfony\Component\Notifier\Bridge\Plivo\PlivoTransportFactory;
162163
use Symfony\Component\Notifier\Bridge\RingCentral\RingCentralTransportFactory;
163164
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
164165
use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransportFactory;
@@ -2584,6 +2585,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25842585
OneSignalTransportFactory::class => 'notifier.transport_factory.one-signal',
25852586
OrangeSmsTransportFactory::class => 'notifier.transport_factory.orange-sms',
25862587
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovh-cloud',
2588+
PlivoTransportFactory::class => 'notifier.transport_factory.plivo',
25872589
RingCentralTransportFactory::class => 'notifier.transport_factory.ring-central',
25882590
RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
25892591
SendberryTransportFactory::class => 'notifier.transport_factory.sendberry',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalTransportFactory;
4646
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransportFactory;
4747
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
48+
use Symfony\Component\Notifier\Bridge\Plivo\PlivoTransportFactory;
4849
use Symfony\Component\Notifier\Bridge\RingCentral\RingCentralTransportFactory;
4950
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
5051
use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransportFactory;
@@ -307,5 +308,9 @@
307308
->parent('notifier.transport_factory.abstract')
308309
->tag('texter.transport_factory')
309310

311+
->set('notifier.transport_factory.plivo', PlivoTransportFactory::class)
312+
->parent('notifier.transport_factory.abstract')
313+
->tag('texter.transport_factory')
314+
310315
;
311316
};
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) 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: 140 additions & 0 deletions
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\Plivo;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class PlivoOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getLog(): ?bool
29+
{
30+
return $this->options['log'] ?? null;
31+
}
32+
33+
public function getMediaUrls(): ?string
34+
{
35+
return $this->options['media_urls'] ?? null;
36+
}
37+
38+
public function getMethod(): ?string
39+
{
40+
return $this->options['method'] ?? null;
41+
}
42+
43+
public function getPowerpackUuid(): ?string
44+
{
45+
return $this->options['powerpack_uuid'] ?? null;
46+
}
47+
48+
public function getRecipientId(): ?string
49+
{
50+
return $this->options['recipient_id'] ?? null;
51+
}
52+
53+
public function getSrc(): ?string
54+
{
55+
return $this->options['src'] ?? null;
56+
}
57+
58+
public function getTrackable(): ?bool
59+
{
60+
return $this->options['trackable'] ?? null;
61+
}
62+
63+
public function getType(): ?string
64+
{
65+
return $this->options['type'] ?? null;
66+
}
67+
68+
public function getUrl(): ?string
69+
{
70+
return $this->options['url'] ?? null;
71+
}
72+
73+
public function setLog(bool $log): self
74+
{
75+
$this->options['log'] = $log;
76+
77+
return $this;
78+
}
79+
80+
public function setMediaUrls(string $mediaUrls): self
81+
{
82+
$this->options['media_urls'] = $mediaUrls;
83+
84+
return $this;
85+
}
86+
87+
public function setMethod(string $method): self
88+
{
89+
$this->options['method'] = $method;
90+
91+
return $this;
92+
}
93+
94+
public function setPowerpackUuid(string $powerpackUuid): self
95+
{
96+
$this->options['powerpack_uuid'] = $powerpackUuid;
97+
98+
return $this;
99+
}
100+
101+
public function setRecipientId(string $id): self
102+
{
103+
$this->options['recipient_id'] = $id;
104+
105+
return $this;
106+
}
107+
108+
public function setSrc(string $src): self
109+
{
110+
$this->options['src'] = $src;
111+
112+
return $this;
113+
}
114+
115+
public function setTrackable(bool $trackable): self
116+
{
117+
$this->options['trackable'] = $trackable;
118+
119+
return $this;
120+
}
121+
122+
public function setType(string $type): self
123+
{
124+
$this->options['type'] = $type;
125+
126+
return $this;
127+
}
128+
129+
public function setUrl(string $url): self
130+
{
131+
$this->options['url'] = $url;
132+
133+
return $this;
134+
}
135+
136+
public function toArray(): array
137+
{
138+
return $this->options;
139+
}
140+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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\Plivo;
13+
14+
use Symfony\Component\HttpClient\Exception\JsonException;
15+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
16+
use Symfony\Component\Notifier\Exception\TransportException;
17+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
18+
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Component\Notifier\Message\SentMessage;
20+
use Symfony\Component\Notifier\Message\SmsMessage;
21+
use Symfony\Component\Notifier\Transport\AbstractTransport;
22+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
23+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
24+
use Symfony\Contracts\HttpClient\HttpClientInterface;
25+
26+
/**
27+
* @author gnito-org <https://github.com/gnito-org>
28+
*/
29+
final class PlivoTransport extends AbstractTransport
30+
{
31+
protected const HOST = 'api.plivo.com';
32+
33+
public function __construct(
34+
private readonly string $authId,
35+
#[\SensitiveParameter] private readonly string $authToken,
36+
private readonly string $from,
37+
HttpClientInterface $client = null,
38+
EventDispatcherInterface $dispatcher = null
39+
) {
40+
parent::__construct($client, $dispatcher);
41+
}
42+
43+
public function __toString(): string
44+
{
45+
return sprintf('plivo://%s?from=%s', $this->getEndpoint(), $this->from);
46+
}
47+
48+
public function supports(MessageInterface $message): bool
49+
{
50+
return $message instanceof SmsMessage;
51+
}
52+
53+
protected function doSend(MessageInterface $message): SentMessage
54+
{
55+
if (!$message instanceof SmsMessage) {
56+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
57+
}
58+
59+
$opts = $message->getOptions();
60+
$options = $opts ? $opts->toArray() : [];
61+
$options['text'] = $message->getSubject();
62+
$options['src'] = $options['src'] ?? $this->from;
63+
$options['dst'] = $options['dst'] ?? $message->getPhone();
64+
65+
if (!preg_match('/^[a-zA-Z0-9\s]{2,11}$/', $options['src']) && !preg_match('/^\+?[1-9]\d{1,14}$/', $options['src'])) {
66+
throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID. Phone number must contain only numbers and optional + character.', $this->from));
67+
}
68+
69+
$endpoint = sprintf('https://%s/v1/Account/%s/Message/', $this->getEndpoint(), $this->authId);
70+
$response = $this->client->request('POST', $endpoint, ['auth_basic' => $this->authId.':'.$this->authToken, 'json' => array_filter($options)]);
71+
72+
try {
73+
$statusCode = $response->getStatusCode();
74+
} catch (TransportExceptionInterface $e) {
75+
throw new TransportException('Could not reach the remote Plivo server.', $response, 0, $e);
76+
}
77+
78+
if (202 !== $statusCode) {
79+
try {
80+
$error = $response->toArray(false);
81+
} catch (JsonException) {
82+
$error['error'] = $response->getContent(false);
83+
}
84+
throw new TransportException(sprintf('Unable to send the SMS - status code: "%s": "%s".', $statusCode, $error['error'] ?? 'unknown error'), $response);
85+
}
86+
87+
$success = $response->toArray(false);
88+
$sentMessage = new SentMessage($message, (string) $this);
89+
$sentMessage->setMessageId($success['message_uuid'][0]);
90+
91+
return $sentMessage;
92+
}
93+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Plivo;
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 PlivoTransportFactory extends AbstractTransportFactory
22+
{
23+
private const TRANSPORT_SCHEME = 'plivo';
24+
25+
public function create(Dsn $dsn): PlivoTransport
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+
$authId = $this->getUser($dsn);
34+
$authToken = $this->getPassword($dsn);
35+
$from = $dsn->getRequiredOption('from');
36+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
37+
$port = $dsn->getPort();
38+
39+
return (new PlivoTransport($authId, $authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
40+
}
41+
42+
protected function getSupportedSchemes(): array
43+
{
44+
return [self::TRANSPORT_SCHEME];
45+
}
46+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Plivo Notifier
2+
==============
3+
4+
Provides [Plivo](https://www.plivo.com) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
PLIVO_DSN=plivo://AUTH_ID:AUTH_TOKEN@default?from=FROM
11+
```
12+
13+
where:
14+
15+
- `AUTH_ID` is your Plivo Auth ID
16+
- `AUTH_TOKEN` is your Plivo Auth Token
17+
- `FROM` is your sender
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)

0 commit comments

Comments
 (0)
0