8000 [Notifier] Add RingCentral bridge · symfony/symfony@c67274c · GitHub
[go: up one dir, main page]

Skip to content

Commit c67274c

Browse files
gnito-orgfabpot
authored andcommitted
[Notifier] Add RingCentral bridge
1 parent a855734 commit c67274c

17 files changed

+550
-2
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\RingCentral\RingCentralTransportFactory;
162163
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
163164
use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransportFactory;
164165
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory as SendinblueNotifierTransportFactory;
@@ -2583,6 +2584,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25832584
OneSignalTransportFactory::class => 'notifier.transport_factory.one-signal',
25842585
OrangeSmsTransportFactory::class => 'notifier.transport_factory.orange-sms',
25852586
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovh-cloud',
2587+
RingCentralTransportFactory::class => 'notifier.transport_factory.ring-central',
25862588
RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
25872589
SendberryTransportFactory::class => 'notifier.transport_factory.sendberry',
25882590
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',

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

Lines changed: 7 additions & 2 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\RingCentral\RingCentralTransportFactory;
4849
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
4950
use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransportFactory;
5051
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
@@ -295,12 +296,16 @@
295296
->tag('chatter.transport_factory')
296297

297298
->set('notifier.transport_factory.chatwork', ChatworkTransportFactory::class)
298-
->parent('notifier.transport_factory.abstract')
299-
->tag('chatter.transport_factory')
299+
->parent('notifier.transport_factory.abstract')
300+
->tag('chatter.transport_factory')
300301

301302
->set('notifier.transport_factory.termii', TermiiTransportFactory::class)
302303
->parent('notifier.transport_factory.abstract')
303304
->tag('texter.transport_factory')
304305

306+
->set('notifier.transport_factory.ring-central', RingCentralTransportFactory::class)
307+
->parent('notifier.transport_factory.abstract')
308+
->tag('texter.transport_factory')
309+
305310
;
306311
};
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, cop 10000 y, 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+
Ring Central Notifier
2+
=====================
3+
4+
Provides [Ring Central](https://www.ringcentral.com) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
RINGCENTRAL_DSN=ringcentral://API_TOKEN@default?from=FROM
11+
```
12+
13+
where:
14+
15+
- `API_TOKEN` is your Ring Central OAuth 2 token
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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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\RingCentral;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class RingCentralOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getCountryCallingCode(): ?string
29+
{
30+
return $this->options['country_calling_code'] ?? null;
31+
}
32+
33+
public function getCountryId(): ?string
34+
{
35+
return $this->options['country_id'] ?? null;
36+
}
37+
38+
public function getCountryIsoCode(): ?string
39+
{
40+
return $this->options['country_iso_code'] ?? null;
41+
}
42+
43+
public function getCountryName(): ?string
44+
{
45+
return $this->options['country_name'] ?? null;
46+
}
47+
48+
public function getCountryUri(): ?string
49+
{
50+
return $this->options['country_uri'] ?? null;
51+
}
52+
53+
public function getFrom(): ?string
54+
{
55+
return $this->options['from'] ?? null;
56+
}
57+
58+
public function getRecipientId(): ?string
59+
{
60+
return $this->options['recipient_id'] ?? null;
61+
}
62+
63+
public function setCountryCallingCode(string $countryCallingCode): self
64+
{
65+
$this->options['country_calling_code'] = $countryCallingCode;
66+
67+
return $this;
68+
}
69+
70+
public function setCountryId(string $countryId): self
71+
{
72+
$this->options['country_id'] = $countryId;
73+
74+
return $this;
75+
}
76+
77+
public function setCountryIsoCode(string $countryIsoCode): self
78+
{
79+
$this->options['country_iso_code'] = $countryIsoCode;
80+
81+
return $this;
82+
}
83+
84+
public function setCountryName(string $countryName): self
85+
{
86+
$this->options['country_name'] = $countryName;
87+
88+
return $this;
89+
}
90+
91+
public function setCountryUri(string $countryUri): self
92+
{
93+
$this->options['country_uri'] = $countryUri;
94+
95+
return $this;
96+
}
97+
98+
public function setFrom(string $from): self
99+
{
100+
$this->options['from'] = $from;
101+
102+
return $this;
103+
}
104+
105+
public function setRecipientId(string $id): self
106+
{
107+
$this->options['recipient_id'] = $id;
108+
109+
return $this;
110+
}
111+
112+
public function toArray(): array
113+
{
114+
return $this->options;
115+
}
116+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\RingCentral;
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 RingCentralTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'platform.ringcentral.com';
31+
32+
public function __construct(
33+
#[\SensitiveParameter] private readonly string $apiToken,
34+
private readonly string $from,
35+
HttpClientInterface $client = null,
36+
EventDispatcherInterface $dispatcher = null
37+
) {
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return sprintf('ringcentral://%s?from=%s', $this->getEndpoint(), $this->from);
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof SmsMessage;
49+
}
50+
51+
protected function doSend(MessageInterface $message): SentMessage
52+
{
53+
if (!$message instanceof SmsMessage) {
54+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
55+
}
56+
57+
$opts = $message->getOptions();
58+
$options = $opts ? $opts->toArray() : [];
59+
$options['text'] = $message->getSubject();
60+
$options['from']['phoneNumber'] = $options['from'] ?? $this->from;
61+
$options['to'][]['phoneNumber'] = $message->getPhone();
62+
63+
if (isset($options['country_id'])) {
64+
$options['country']['id'] = $options['country_id'] ?? null;
65+
$options['country']['uri'] = $options['country_uri'] ?? null;
66+
$options['country']['name'] = $options['country_name'] ?? null;
67+
$options['country']['isoCode'] = $options['country_iso_code'] ?? null;
68+
$options['country']['callingCode'] = $options['country_calling_code'] ?? null;
69+
unset($options['country_id'], $options['country_uri'], $options['country_name'], $options['country_iso_code'], $options['country_calling_code']);
70+
}
71+
72+
if (!preg_match('/^\+[1-9]\d{1,14}$/', $options['from']['phoneNumber'])) {
73+
throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number. Phone number must be in E.164 format.', $this->from));
74+
}
75+
76+
$endpoint = sprintf('https://%s/restapi/v1.0/account/~/extension/~/sms', $this->getEndpoint());
77+
$response = $this->client->request('POST', $endpoint, ['auth_bearer' => $this->apiToken, 'json' => array_filter($options)]);
78+
79+
try {
80+
$statusCode = $response->getStatusCode();
81+
} catch (TransportExceptionInterface $e) {
82+
throw new TransportException('Could not reach the remote Ring Central server.', $response, 0, $e);
83+
}
84+
85+
if (200 !== $statusCode) {
86+
$error = $response->toArray(false);
87+
throw new TransportException(sprintf('Unable to send the SMS - "%s".', $error['message'] ?? $error['error_description'] ?? $error['description'] ?? 'unknown failure'), $response);
88+
}
89+
90+
$success = $response->toArray(false);
91+
$sentMessage = new SentMessage($message, (string) $this);
92+
$sentMessage->setMessageId($success['id']);
93+
94+
return $sentMessage;
95+
}
96+
}
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 <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\RingCentral;
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 RingCentralTransportFactory extends AbstractTransportFactory
22+
{
23+
private const TRANSPORT_SCHEME = 'ringcentral';
24+
25+
public function create(Dsn $dsn): RingCentralTransport
26+
{
27+
$scheme = $dsn->getScheme();
28+
if (self::TRANSPORT_SCHEME !== $scheme) {
29+
throw new UnsupportedSchemeException($dsn, self::TRANSPORT_SCHEME, $this->getSupportedSchemes());
30+
}
31+
$apiKey = $this->getUser($dsn);
32+
$from = $dsn->getRequiredOption('from');
33+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34+
$port = $dsn->getPort();
35+
36+
return (new RingCentralTransport($apiKey, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
37+
}
38+
39+
protected function getSupportedSchemes(): array
40+
{
41+
return [self::TRANSPORT_SCHEME];
42+
}
43+
}

0 commit comments

Comments
 (0)
0