Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4d6d4d

Browse files
committed
feature #44913 [Notifier] Add Orange SMS bridge (enigma972)
This PR was squashed before being merged into the 6.1 branch. Discussion ---------- [Notifier] Add Orange SMS bridge | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT Add Orange SMS bridge Previously [#44884](#44884) Commits ------- 1aebeaf [Notifier] Add Orange SMS bridge
2 parents 1255cff + 1aebeaf commit a4d6d4d

15 files changed

+395
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
140140
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
141141
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalTransportFactory;
142+
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransportFactory;
142143
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
143144
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
144145
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory as SendinblueNotifierTransportFactory;
@@ -2437,6 +2438,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24372438
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
24382439
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
24392440
OneSignalTransportFactory::class => 'notifier.transport_factory.one-signal',
2441+
OrangeSmsTransportFactory::class => 'notifier.transport_factory.orange-sms',
24402442
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovh-cloud',
24412443
RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
24422444
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
3838
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
3939
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalTransportFactory;
40+
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransportFactory;
4041
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
4142
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
4243
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
@@ -238,6 +239,10 @@
238239
->parent('notifier.transport_factory.abstract')
239240
->tag('texter.transport_factory')
240241

242+
->set('notifier.transport_factory.orange-sms', OrangeSmsTransportFactory::class)
243+
->parent('notifier.transport_factory.abstract')
244+
->tag('texter.transport_factory')
245+
241246
->set('notifier.transport_factory.expo', ExpoTransportFactory::class)
242247
->parent('notifier.transport_factory.abstract')
243248
->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.1
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: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\OrangeSms;
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\HttpClientInterface;
22+
23+
final class OrangeSmsTransport extends AbstractTransport
24+
{
25+
protected const HOST = 'api.orange.com';
26+
27+
private string $clientID;
28+
private string $clientSecret;
29+
private string $from;
30+
private ?string $senderName;
31+
32+
public function __construct(string $clientID, string $clientSecret, string $from, string $senderName = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
33+
{
34+
$this->clientID = $clientID;
35+
$this->clientSecret = $clientSecret;
36+
$this->from = $from;
37+
$this->senderName = $senderName;
38+
39+
parent::__construct($client, $dispatcher);
40+
}
41+
42+
public function __toString(): string
43+
{
44+
if (null !== $this->senderName) {
45+
return sprintf('orange-sms://%s?from=%s&sender_name=%s', $this->getEndpoint(), $this->from, $this->senderName);
46+
}
47+
48+
return sprintf('orange-sms://%s?from=%s', $this->getEndpoint(), $this->from);
49+
}
50+
51+
public function supports(MessageInterface $message): bool
52+
{
53+
return $message instanceof SmsMessage;
54+
}
55+
56+
public function doSend(MessageInterface $message): SentMessage
57+
{
58+
if (!$message instanceof SmsMessage) {
59+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
60+
}
61+
62+
$url = 'https://'.$this->getEndpoint().'/smsmessaging/v1/outbound/'.urlencode('tel:'.$this->from).'/requests';
63+
$headers = [
64+
'Authorization' => 'Bearer '.$this->getAccessToken(),
65+
'Content-Type' => 'application/json',
66+
];
67+
68+
$payload = [
69+
'outboundSMSMessageRequest' => [
70+
'address' => 'tel:'.$message->getPhone(),
71+
'senderAddress' => 'tel:'.$this->from,
72+
'outboundSMSTextMessage' => [
73+
'message' => $message->getSubject(),
74+
],
75+
],
76+
];
77+
78+
if (null !== $this->senderName) {
79+
$payload['outboundSMSMessageRequest']['senderName'] = urlencode($this->senderName);
80+
}
81+
82+
$response = $this->client->request('POST', $url, [
83+
'headers' => $headers,
84+
'json' => $payload,
85+
]);
86+
87+
if (201 !== $response->getStatusCode()) {
88+
$content = $response->toArray(false);
89+
$errorMessage = $content['requestError']['serviceException']['messageId'] ?? '';
90+
$errorInfo = $content['requestError']['serviceException']['text'] ?? '';
91+
92+
throw new TransportException(sprintf('Unable to send the SMS: "%s" (%s).', $errorMessage, $errorInfo), $response);
93+
}
94+
95+
return new SentMessage($message, (string) $this);
96+
}
97+
98+
private function getAccessToken(): string
99+
{
100+
$url = 'https://'.$this->getEndpoint().'/oauth/v3/token';
101+
$headers = [
102+
'Content-Type' => 'application/x-www-form-urlencoded',
103+
'Accept' => 'application/json',
104+
];
105+
$args = ['grant_type' => 'client_credentials'];
106+
107+
$response = $this->client->request('POST', $url, [
108+
'auth_basic' => [$this->clientID, $this->clientSecret],
109+
'headers' => $headers,
110+
'body' => $args,
111+
]);
112+
113+
if (200 !== $response->getStatusCode()) {
114+
throw new TransportException('Failed to get Orange access token .', $response);
115+
}
116+
117+
return $response->toArray()['access_token'];
118+
}
119+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\OrangeSms;
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 OrangeSmsTransportFactory extends AbstractTransportFactory
19+
{
20+
public function create(Dsn $dsn): OrangeSmsTransport
21+
{
22+
$scheme = $dsn->getScheme();
23+
24+
if ('orange-sms' !== $scheme) {
25+
throw new UnsupportedSchemeException($dsn, 'orange-sms', $this->getSupportedSchemes());
26+
}
27+
28+
$user = $this->getUser($dsn);
29+
$password = $this->getPassword($dsn);
30+
$from = $dsn->getRequiredOption('from');
31+
$senderName = $dsn->getOption('sender_name');
32+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
33+
$port = $dsn->getPort();
34+
35+
return (new OrangeSmsTransport($user, $password, $from, $senderName, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
36+
}
37+
38+
protected function getSupportedSchemes(): array
39+
{
40+
return ['orange-sms'];
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Orange Sms Notifier
2+
-------------------
3+
4+
Provides [Orange Sms](https://developer.orange.com/apis/sms) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
ORANGE_SMS_DSN=orange-sms://CLIENT_ID:CLIENT_SECRET@default?from=FROM&sender_name=SENDER_NAME
11+
```
12+
13+
where:
14+
15+
- `CLIENT_ID` is your Orange App client ID
16+
- `CLIENT_SECRET` is the Orange App client secret
17+
- `FROM` is the sender phone number
18+
- `SENDER_NAME` is the sender name
19+
20+
example:
21+
22+
```
23+
ORANGE_SMS_DSN=orange-sms://RbttXve8o2y3IglAqJXlXTzZywyyjqKo:iNpfgVeHusPEKrrp@default?from=+243000000&sender_name=platformXYZ
24+
```
25+
26+
See Orange Sms documentation at https://developer.orange.com/apis/sms-cd/api-reference
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\OrangeSms\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
final class OrangeSmsTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): OrangeSmsTransportFactory
20+
{
21+
return new OrangeSmsTransportFactory();
22+
}
23+
24+
public function createProvider(): iterable
25+
{
26+
yield [
27+
'orange-sms://host.test?from=FROM&sender_name=SENDER_NAME',
28+
'orange-sms://CLIENT_ID:[email protected]?from=FROM&sender_name=SENDER_NAME',
29+
];
30+
}
31+
32+
public function supportsProvider(): iterable
33+
{
34+
yield [true, 'orange-sms://CLIENT_ID:CLIENT_SECRET@default?from=FROM&sender_name=SENDER_NAME'];
35+
yield [false, 'somethingElse://CLIENT_ID:CLIENT_SECRET@default'];
36+
}
37+
38+
public function incompleteDsnProvider(): iterable
39+
{
40+
yield 'missing credentials' => ['orange-sms://default?from=FROM&sender_name=SENDER_NAME'];
41+
yield 'missing CLIENT_ID' => ['orange-sms://:CLIENT_SECRET@default?from=FROM&sender_name=SENDER_NAME'];
42+
yield 'missing CLIENT_SECRET' => ['orange-sms://CLIENT_ID:@default?from=FROM&sender_name=SENDER_NAME'];
43+
}
44+
45+
public function missingRequiredOptionProvider(): iterable
46+
{
47+
yield 'missing option: from' => ['orange-sms://CLIENT_ID:CLIENT_SECRET@default?sender_name=SENDER_NAME'];
48+
}
49+
50+
public function unsupportedSchemeProvider(): iterable
51+
{
52+
yield ['somethingElse://CLIENT_ID:CLIENT_SECRET@default?from=FROM&sender_name=SENDER_NAME'];
53+
yield ['somethingElse://CLIENT_ID:CLIENT_SECRET@host?sender_name=SENDER_NAME']; // missing "from" option
54+
}
55+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\OrangeSms\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransport;
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\Contracts\HttpClient\HttpClientInterface;
20+
21+
final class OrangeSmsTransportTest extends TransportTestCase
22+
{
23+
public function createTransport(HttpClientInterface $client = null): OrangeSmsTransport
24+
{
25+
return (new OrangeSmsTransport('CLIENT_ID', 'CLIENT_SECRET', 'FROM', 'SENDER_NAME', $client ?? $this->createMock(HttpClientInterface::class)))->setHost('host.test');
26+
}
27+
28+
public function toStringProvider(): iterable
29+
{
30+
yield ['orange-sms://host.test?from=FROM&sender_name=SENDER_NAME', $this->createTransport()];
31+
}
32+
33+
public function supportedMessagesProvider(): iterable
34+
{
35+
yield [new SmsMessage('+243899999999', 'Hello World!')];
36+
}
37+
38+
public function unsupportedMessagesProvider(): iterable
39+
{
40+
yield [new ChatMessage('Hello World!')];
41+
yield [$this->createMock(MessageInterface::class)];
42+
}
43+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "symfony/orange-sms-notifier",
3+
"type": "symfony-notifier-bridge",
4+
"description": "Symfony Orange Sms Notifier Bridge",
5+
"keywords": [ "sms", "orange-smsapi", "orange", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Fabien Potencier",
11+
"email": "[email protected]"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": ">=8.0.2",
20+
"symfony/http-client": "^5.4|^6.0",
21+
"symfony/notifier": "^5.4|^6.0"
22+
},
23+
"require-dev": {
24+
"symfony/uid": "^5.4|^6.0"
25+
},
26+
"autoload": {
27+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\OrangeSms\\": "" },
28+
"exclude-from-classmap": [
29+
"/Tests/"
30+
]
31+
},
32+
"minimum-stability": "dev"
33+
}

0 commit comments

Comments
 (0)