8000 feature #48399 [Notifier] Add iSendPro bridge (leblanc-simon) · symfony/symfony@f17a4a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit f17a4a6

Browse files
committed
feature #48399 [Notifier] Add iSendPro bridge (leblanc-simon)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Notifier] Add iSendPro bridge | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#17450 |Recipe | symfony/recipes#1147 This PR add the notifier bridge for [Isendpro](https://www.isendpro.com/) (SMS). Replace the #48253 (I failed to target new branch with Git, sorry...) Commits ------- 695d5eb [Notifier] Add iSendPro bridge
2 parents 97db974 + 695d5eb commit f17a4a6

16 files changed

+492
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
145145
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
146146
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
147+
use Symfony\Component\Notifier\Bridge\Isendpro\IsendproTransportFactory;
147148
use Symfony\Component\Notifier\Bridge\KazInfoTeh\KazInfoTehTransportFactory;
148149
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory;
149150
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
@@ -2566,6 +2567,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25662567
GoogleChatTransportFactory::class => 'notifier.transport_factory.google-chat',
25672568
InfobipTransportFactory::class => 'notifier.transport_factory.infobip',
25682569
IqsmsTransportFactory::class => 'notifier.transport_factory.iqsms',
2570+
IsendproTransportFactory::class => 'notifier.transport_factory.isendpro',
25692571
KazInfoTehTransportFactory::class => 'notifier.transport_factory.kaz-info-teh',
25702572
LightSmsTransportFactory::class => 'notifier.transport_factory.light-sms',
25712573
LinkedInTransportFactory::class => 'notifier.transport_factory.linked-in',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
3131
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
3232
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
33+
use Symfony\Component\Notifier\Bridge\Isendpro\IsendproTransportFactory;
3334
use Symfony\Component\Notifier\Bridge\KazInfoTeh\KazInfoTehTransportFactory;
3435
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory;
3536
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
@@ -154,6 +155,10 @@
154155
->parent('notifier.transport_factory.abstract')
155156
->tag('texter.transport_factory')
156157

158+
->set('notifier.transport_factory.isendpro', IsendproTransportFactory::class)
159+
->parent('notifier.transport_factory.abstract')
160+
->tag('texter.transport_factory')
161+
157162
->set('notifier.transport_factory.mobyt', MobytTransportFactory::class)
158163
->parent('notifier.transport_factory.abstract')
159164
->tag('texter.transport_factory')
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: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\Isendpro;
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\DecodingExceptionInterface;
22+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
23+
use Symfony\Contracts\HttpClient\HttpClientInterface;
24+
25+
final class IsendproTransport extends AbstractTransport
26+
{
27+
protected const HOST = 'apirest.isendpro.com';
28+
29+
public function __construct(
30+
#[\SensitiveParameter] private string $keyid,
31+
private ?string $from = null,
32+
private bool $noStop = false,
33+
private bool $sandbox = false,
34+
HttpClientInterface $client = null,
35+
EventDispatcherInterface $dispatcher = null
36+
) {
37+
parent::__construct($client, $dispatcher);
38+
}
39+
40+
public function __toString(): string
41+
{
42+
if (null === $this->from) {
43+
return sprintf('isendpro://%s?no_stop=%d&sandbox=%d', $this->getEndpoint(), (int) $this->noStop, (int) $this->sandbox);
44+
}
45+
46+
return sprintf('isendpro://%s?from=%s&no_stop=%d&sandbox=%d', $this->getEndpoint(), $this->from, (int) $this->noStop, (int) $this->sandbox);
47+
}
48+
49+
public function supports(MessageInterface $message): bool
50+
{
51+
return $message instanceof SmsMessage;
52+
}
53+
54+
protected function doSend(MessageInterface $message): SentMessage
55+
{
56+
if (!$message instanceof SmsMessage) {
57+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
58+
}
59+
60+
$messageId = bin2hex(random_bytes(7));
61+
62+
$messageData = [
63+
'keyid' => $this->keyid,
64+
'num' => $message->getPhone(),
65+
'sms' => $message->getSubject(),
66+
'sandbox' => (int) $this->sandbox,
67+
'tracker' => $messageId,
68+
];
69+
70+
if ($this->noStop) {
71+
$messageData['nostop'] = '1';
72+
}
73+
74+
if ('' !== $message->getFrom()) {
75+
$messageData['emetteur'] = $message->getFrom();
76+
} elseif (null !== $this->from) {
77+
$messageData['emetteur'] = $this->from;
78+
}
79+
80+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/cgi-bin/sms', [
81+
'headers' => [
82+
'Accept' => 'application/json',
83+
'Cache-Control' => 'no-cache',
84+
],
85+
'json' => $messageData,
86+
]);
87+
88+
try {
89+
$statusCode = $response->getStatusCode();
90+
$result = $response->toArray(false);
91+
$details = $result['etat']['etat'][0] ?? [];
92+
$detailsCode = (int) ($details['code'] ?? -1); // -1 is not a valid error code on iSendPro. But if code doesn't exist, it's a very strange error (not possible normally)
93+
94+
if (200 === $statusCode && 0 === $detailsCode) {
95+
$sentMessage = new SentMessage($message, (string) $this);
96+
$sentMessage->setMessageId($messageId);
97+
98+
return $sentMessage;
99+
}
100+
101+
$errorMessage = sprintf('Unable to send the SMS: error %d.', $statusCode);
102+
$detailsMessage = $details['message'] ?? null;
103+
104+
if ($detailsMessage) {
105+
$errorMessage .= sprintf(' Details from iSendPro: %s: "%s".', $detailsCode, $detailsMessage);
106+
}
107+
} catch (TransportExceptionInterface $e) {
108+
throw new TransportException('Could not reach the remote iSendPro server.', $response, 0, $e);
109+
} catch (DecodingExceptionInterface $e) {
110+
$errorMessage = sprintf('Unable to send the SMS: error %d. %s', $statusCode, $e->getMessage());
111+
}
112+
113+
throw new TransportException($errorMessage, $response);
114+
}
115+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Isendpro;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
18+
final class IsendproTransportFactory extends AbstractTransportFactory
19+
{
20+
public function create(Dsn $dsn): IsendproTransport
21+
{
22+
if ('isendpro' !== $dsn->getScheme()) {
23+
throw new UnsupportedSchemeException($dsn, 'isendpro', $this->getSupportedSchemes());
24+
}
25+
26+
$keyid = $this->getUser($dsn);
27+
$from = $dsn->getOption('from', null);
28+
$noStop = filter_var($dsn->getOption('no_stop', false), \FILTER_VALIDATE_BOOLEAN);
29+
$sandbox = filter_var($dsn->getOption('sandbox', false), \FILTER_VALIDATE_BOOLEAN);
30+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
31+
$port = $dsn->getPort();
32+
33+
return (new IsendproTransport($keyid, $from, $noStop, $sandbox, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
34+
}
35+
36+
protected function getSupportedSchemes(): array
37+
{
38+
return ['isendpro'];
39+
}
40+
}
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
iSendPro Notifier
2+
=================
3+
4+
Provides [iSendPro](https://www.isendpro.com/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
ISENDPRO_DSN=isendpro://ACCOUNT_KEY_ID@default?from=FROM&no_stop=NO_STOP&sandbox=SANDBOX
11+
```
12+
13+
where:
14+
- `ACCOUNT_KEY_ID` is your iSendPro API Key ID
15+
- `FROM` is the alphanumeric originator for the message to appear to originate from (optional)
16+
- `NO_STOP` setting this parameter to "1" (default "0") allows removing "STOP clause" at the end of the message for non-commercial use (optional)
17+
- `SANDBOX` setting this parameter to "1" (default "0") allows to use the notifier in sandbox mode (optional)
18+
19+
See iSendPro documentation at https://www.isendpro.com/docs/#prerequis
20+
21+
Resources
22+
---------
23+
24+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
25+
* [Report issues](https://github.com/symfony/symfony/issues) and
26+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
27+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\Isendpro\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Isendpro\IsendproTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
final class IsendproTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): IsendproTransportFactory
20+
{
21+
return new IsendproTransportFactory();
22+
}
23+
24+
public function createProvider(): iterable
25+
{
26+
yield [
27+
'isendpro://host.test?no_stop=0&sandbox=0',
28+
'isendpro://account_key_id@host.test',
29+
];
30+
31+
yield [
32+
'isendpro://host.test?from=FROM&no_stop=0&sandbox=0',
33+
'isendpro://account_key_id@host.test?from=FROM',
34+
];
35+
36+
yield [
37+
'isendpro://host.test?from=FROM&no_stop=0&sandbox=0',
38+
'isendpro://account_key_id@host.test?from=FROM&no_stop=0&sandbox=0',
39+
];
40+
41+
yield [
42+
'isendpro://host.test?from=FROM&no_stop=0&sandbox=0',
43+
'isendpro://account_key_id@host.test?from=FROM&no_stop=false&sandbox=0',
44+
];
45+
46+
yield [
47+
'isendpro://host.test?from=FROM&no_stop=1&sandbox=0',
48+
'isendpro://account_key_id@host.test?from=FROM&no_stop=1&sandbox=0',
49+
];
50+
51+
yield [
52+
'isendpro://host.test?from=FROM&no_stop=1&sandbox=1',
53+
'isendpro://account_key_id@host.test?from=FROM&no_stop=1&sandbox=true',
54+
];
55+
}
56+
57+
public function supportsProvider(): iterable
58+
{
59+
yield [true, 'isendpro://account_key_id@host?from=FROM'];
60+
yield [false, 'somethingElse://account_key_id@default'];
61+
}
62+
63+
public function incompleteDsnProvider(): iterable
64+
{
65+
yield 'missing credentials' => ['isendpro://host?from=FROM'];
66+
}
67+
68+
public function missingRequiredOptionProvider(): iterable
69+
{
70+
yield 'missing option: account_key_id' => ['isendpro://default'];
71+
}
72+
73+
public function unsupportedSchemeProvider(): iterable
74+
{
75+
yield ['somethingElse://account_key_id@default'];
76+
}
77+
78+
/**
79+
* @dataProvider missingRequiredOptionProvider
80+
*/
81+
public function testMissingRequiredOptionException(string $dsn, string $message = null)
82+
{
83+
$this->markTestIncomplete('The only required option is account key id, matched by incompleteDsnProvider');
84+
}
85+
}

0 commit comments

Comments
 (0)
0