10000 feature #44271 [Notifier] add Vonage bridge to replace the Nexmo one … · symfony/symfony@b5d3f56 · GitHub
[go: up one dir, main page]

Skip to content

Commit b5d3f56

Browse files
committed
feature #44271 [Notifier] add Vonage bridge to replace the Nexmo one (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [Notifier] add Vonage bridge to replace the Nexmo one | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | - | License | MIT | Doc PR | - @SecondeJK from Vonage reached us on Slack to tell us that Nexmo has been bought by Vonage 2 years ago. So here we are, deprecating the nexmo bridge in favor of the vonage one. Commits ------- a3802e2 [Notifier] add Vonage bridge to replace the Nexmo one
2 parents 0ac7aa2 + a3802e2 commit b5d3f56

22 files changed

+385
-13
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
158158
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransport;
159159
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
160+
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
160161
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
161162
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
162163
use Symfony\Component\Notifier\Notifier;
@@ -2540,6 +2541,7 @@ private function registerNotifierConfiguration(array $config, Contain 1E79 erBuilder $
25402541
TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
25412542
TurboSmsTransport::class => 'notifier.transport_factory.turbo-sms',
25422543
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
2544+
VonageTransportFactory::class => 'notifier.transport_factory.vonage',
25432545
YunpianTransportFactory::class => 'notifier.transport_factory.yunpian',
25442546
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
25452547
];

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
5353
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransportFactory;
5454
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
55+
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
5556
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
5657
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
5758
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
@@ -109,6 +110,11 @@
109110
->set('notifier.transport_factory.nexmo', NexmoTransportFactory::class)
110111
->parent('notifier.transport_factory.abstract')
111112
->tag('texter.transport_factory')
113+
->deprecate('symfony/framework-bundle', '5.4', 'The "%service_id% service is deprecated, use "notifier.transport_factory.vonage" instead.')
114+
115+
->set('notifier.transport_factory.vonage', VonageTransportFactory::class)
116+
->parent('notifier.transport_factory.abstract')
117+
->tag('texter.transport_factory')
112118

113119
->set('notifier.transport_factory.rocket-chat', RocketChatTransportFactory::class)
114120
->parent('notifier.transport_factory.abstract')

src/Symfony/Component/Notifier/Bridge/Nexmo/NexmoTransport.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Nexmo;
1313

14+
trigger_deprecation('symfony/nexmo-notifier', '5.4', 'The "symfony/nexmo-notifier" package is deprecated, use "symfony/vonage-notifier" instead.');
15+
1416
use Symfony\Component\Notifier\Exception\TransportException;
1517
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
1618
use Symfony\Component\Notifier\Message\MessageInterface;
@@ -23,6 +25,8 @@
2325

2426
/**
2527
* @author Fabien Potencier <fabien@symfony.com>
28+
*
29+
* @deprecated since Symfony 5.4, use the Vonage bridge instead.
2630
*/
2731
final class NexmoTransport extends AbstractTransport
2832
{

src/Symfony/Component/Notifier/Bridge/Nexmo/NexmoTransportFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
/**
2020
* @author Fabien Potencier <fabien@symfony.com>
21+
*
22+
* @deprecated since Symfony 5.4, use the Vonage bridge instead.
2123
*/
2224
final class NexmoTransportFactory extends AbstractTransportFactory
2325
{

src/Symfony/Component/Notifier/Bridge/Nexmo/README.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
Nexmo Notifier
22
==============
33

4-
Provides [Nexmo](https://nexmo.com) integration for Symfony Notifier.
5-
6-
DSN example
7-
-----------
8-
9-
```
10-
NEXMO_DSN=nexmo://KEY:SECRET@default?from=FROM
11-
```
12-
13-
where:
14-
- `KEY` is your Nexmo key
15-
- `SECRET` is your Nexmo secret
16-
- `FROM` is your sender
4+
The Nexmo bridge is deprecated, use the Vonage bridge instead.
175

186
Resources
197
---------

src/Symfony/Component/Notifier/Bridge/Nexmo/Tests/NexmoTransportFactoryTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
1616
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
1717

18+
/**
19+
* @group legacy
20+
*/
1821
final class NexmoTransportFactoryTest extends TransportFactoryTestCase
1922
{
2023
/**

src/Symfony/Component/Notifier/Bridge/Nexmo/Tests/NexmoTransportTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use Symfony\Component\Notifier\Transport\TransportInterface;
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
2121

22+
/**
23+
* @group legacy
24+
*/
2225
final class NexmoTransportTest extends TransportTestCase
2326
{
2427
/**

src/Symfony/Component/Notifier/Bridge/Nexmo/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=7.2.5",
20+
"symfony/deprecation-contracts": "^2.1|^3",
2021
"symfony/http-client": "^4.3|^5.0|^6.0",
2122
"symfony/notifier": "^5.3|^6.0"
2223
},
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+
5.4
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) 2021 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+
Vonage Notifier
2+
===============
3+
4+
Provides [Vonage](https://vonage.com) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
VONAGE_DSN=vonage://KEY:SECRET@default?from=FROM
11+
```
12+
13+
where:
14+
- `KEY` is your Vonage key
15+
- `SECRET` is your Vonage secret
16+
- `FROM` is your sender
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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Vonage\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
final class VonageTransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
/**
21+
* @return VonageTransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
24+
{
25+
return new VonageTransportFactory();
26+
}
27+
28+
public function createProvider(): iterable
29+
{
30+
yield [
31+
'vonage://host.test?from=0611223344',
32+
'vonage://apiKey:apiSecret@host.test?from=0611223344',
33+
];
34+
}
35+
36+
public function supportsProvider(): iterable
37+
{
38+
yield [true, 'vonage://apiKey:apiSecret@default?from=0611223344'];
39+
yield [false, 'somethingElse://apiKey:apiSecret@default?from=0611223344'];
40+
}
41+
42+
public function missingRequiredOptionProvider(): iterable
43+
{
44+
yield 'missing option: from' => ['vonage://apiKey:apiSecret@default'];
45+
}
46+
47+
public function unsupportedSchemeProvider(): iterable
48+
{
49+
yield ['somethingElse://apiKey:apiSecret@default?from=0611223344'];
50+
yield ['somethingElse://apiKey:apiSecret@default']; // missing "from" option
51+
}
52+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Vonage\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransport;
15+
use Symfony\Component\Notifier\Message\ChatMessage;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SmsMessage;
18+
use Symfony\Component\Notifier\Test\TransportTestCase;
19+
use Symfony\Component\Notifier\Transport\TransportInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
final class VonageTransportTest extends TransportTestCase
23+
{
24+
/**
25+
* @return VonageTransport
26+
*/
27+
public function createTransport(HttpClientInterface $client = null): TransportInterface
28+
{
29+
return new VonageTransport('apiKey', 'apiSecret', 'sender', $client ?? $this->createMock(HttpClientInterface::class));
30+
}
31+
32+
public function toStringProvider(): iterable
33+
{
34+
yield ['vonage://rest.nexmo.com?from=sender', $this->createTransport()];
35+
}
36+
37+
public function supportedMessagesProvider(): iterable
38+
{
39+
yield [new SmsMessage('0611223344', 'Hello!')];
40+
}
41+
42+
public function unsupportedMessagesProvider(): iterable
43+
{
44+
yield [new ChatMessage('Hello!')];
45+
yield [$this->createMock(MessageInterface::class)];
46+
}
47+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Vonage;
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 Fabien Potencier <fabien@symfony.com>
26+
*/
27+
final class VonageTransport extends AbstractTransport
28+
{
29+
// see https://developer.vonage.com/messaging/sms/overview
30+
protected const HOST = 'rest.nexmo.com';
31+
32+
private $apiKey;
33+
private $apiSecret;
34+
private $from;
35+
36+
public function __construct(string $apiKey, string $apiSecret, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
37+
{
38+
$this->apiKey = $apiKey;
39+
$this->apiSecret = $apiSecret;
40+
$this->from = $from;
41+
42+
parent::__construct($client, $dispatcher);
43+
}
44+
45+
public function __toString(): string
46+
{
47+
return sprintf('vonage://%s?from=%s', $this->getEndpoint(), $this->from);
48+
}
49+
50+
public function supports(MessageInterface $message): bool
51+
{
52+
return $message instanceof SmsMessage;
53+
}
54+
55+
protected function doSend(MessageInterface $message): SentMessage
56+
{
57+
if (!$message instanceof SmsMessage) {
58+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
59+
}
60+
61+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/sms/json', [
62+
'body' => [
63+
'from' => $this->from,
64+
'to' => $message->getPhone(),
65+
'text' => $message->getSubject(),
66+
'api_key' => $this->apiKey,
67+
'api_secret' => $this->apiSecret,
68+
],
69+
]);
70+
71+
try {
72+
$result = $response->toArray(false);
73+
} catch (TransportExceptionInterface $e) {
74+
throw new TransportException('Could not reach the remote Vonage server.', $response, 0, $e);
75+
}
76+
77+
foreach ($result['messages'] as $msg) {
78+
if ($msg['status'] ?? false) {
79+
throw new TransportException('Unable to send the SMS: '.$msg['error-text'].sprintf(' (code %s).', $msg['status']), $response);
80+
}
81+
}
82+
83+
$success = $response->toArray(false);
84+
85+
$sentMessage = new SentMessage($message, (string) $this);
86+
$sentMessage->setMessageId($success['messages'][0]['message-id']);
87+
88+
return $sentMessage;
89+
}
90+
}

0 commit comments

Comments
 (0)
0