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

Skip to content

Commit 89b2981

Browse files
franckranaivoFranck Rodolphe RANAIVO-HARISOA
authored andcommitted
[Notifier] Add Contact Everyone Bridge
1 parent 123b165 commit 89b2981

File tree

16 files changed

+405
-0
lines changed

16 files changed

+405
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
124124
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
125125
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
126+
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
126127
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
127128
use Symfony\Component\Notifier\Bridge\Engagespot\EngagespotTransportFactory;
128129
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
@@ -2523,6 +2524,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25232524
AllMySmsTransportFactory::class => 'notifier.transport_factory.all-my-sms',
25242525
AmazonSnsTransportFactory::class => 'notifier.transport_factory.amazon-sns',
25252526
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
2527+
ContactEveryoneTransportFactory::class => 'notifier.transport_factory.contact-everyone',
25262528
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
25272529
EngagespotTransportFactory::class => 'notifier.transport_factory.engagespot',
25282530
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
1515
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
1616
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
17+
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
1718
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
1819
use Symfony\Component\Notifier\Bridge\Engagespot\EngagespotTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
@@ -197,6 +198,10 @@
197198
->parent('notifier.transport_factory.abstract')
198199
->tag('texter.transport_factory')
199200

201+
->set('notifier.transport_factory.contact-everyone', ContactEveryoneTransportFactory::class)
202+
->parent('notifier.transport_factory.abstract')
203+
->tag('texter.transport_factory')
204+
200205
->set('notifier.transport_factory.amazon-sns', AmazonSnsTransportFactory::class)
201206
->parent('notifier.transport_factory.abstract')
202207
->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.2
5+
---
6+
7+
* Add the bridge
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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\ContactEveryone;
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 Franck Ranaivo-Harisoa <[email protected]>
26+
*/
27+
final class ContactEveryoneTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'contact-everyone.orange-business.com';
30+
31+
private string $token;
32+
private ?string $diffusionName;
33+
private ?string $category;
34+
35+
public function __construct(string $token, ?string $diffusionName, ?string $category, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->token = $token;
38+
$this->diffusionName = $diffusionName;
39+
$this->category = $category;
40+
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
$dsn = sprintf('contact-everyone://%s', $this->getEndpoint());
47+
48+
if (null !== $this->diffusionName) {
49+
$dsn .= sprintf('?diffusionname=%s', $this->diffusionName);
50+
}
51+
52+
if (null !== $this->category) {
53+
$dsn .= sprintf('%scategory=%s', (null === $this->diffusionName) ? '?' : '&', $this->category);
54+
}
55+
56+
return $dsn;
57+
}
58+
59+
public function supports(MessageInterface $message): bool
60+
{
61+
return $message instanceof SmsMessage;
62+
}
63+
64+
protected function doSend(MessageInterface $message): SentMessage
65+
{
66+
if (!$message instanceof SmsMessage) {
67+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
68+
}
69+
70+
$endpoint = sprintf('https://%s/api/light/diffusions/sms', self::HOST);
71+
$response = $this->client->request('POST', $endpoint, [
72+
'query' => [
73+
'xcharset' => 'true',
74+
'token' => $this->token,
75+
'to' => $message->getPhone(),
76+
'msg' => $message->getSubject(),
77+
],
78+
]);
79+
80+
try {
81+
$statusCode = $response->getStatusCode();
82+
} catch (TransportExceptionInterface $e) {
83+
throw new TransportException('Could not reach the remote Contact Everyone server.', $response, 0, $e);
84+
}
85+
86+
if (200 !== $statusCode) {
87+
$error = $response->toArray(false);
88+
throw new TransportException(sprintf('Unable to send the Contact Everyone message with following error: "%s". For further details, please check this logId: "%s".', $error['message'], $error['logId']), $response);
89+
}
90+
91+
$result = $response->getContent(false);
92+
93+
$sentMessage = new SentMessage($message, (string) $this);
94+
$sentMessage->setMessageId($result ?? '');
95+
96+
return $sentMessage;
97+
}
98+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\ContactEveryone;
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 Franck Ranaivo-Harisoa <[email protected]>
20+
*/
21+
final class ContactEveryoneTransportFactory extends AbstractTransportFactory
22+
{
23+
public function create(Dsn $dsn): ContactEveryoneTransport
24+
{
25+
if ('contact-everyone' !== $dsn->getScheme()) {
26+
throw new UnsupportedSchemeException($dsn, 'contact-everyone', $this->getSupportedSchemes());
27+
}
28+
29+
$token = $this->getUser($dsn);
30+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
31+
$diffusionName = $dsn->getOption('diffusionname');
32+
$category = $dsn->getOption('category');
33+
34+
return (new ContactEveryoneTransport($token, $diffusionName, $category, $this->client, $this->dispatcher))->setHost($host)->setPort($dsn->getPort());
35+
}
36+
37+
protected function getSupportedSchemes(): array
38+
{
39+
return ['contact-everyone'];
40+
}
41+
}
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Contact Everyone Notifier
2+
=========================
3+
4+
Provides [Contact everyone](https://www.orange-business.com/fr/produits/contact-everyone) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
CONTACT_EVERYONE_DSN=contact-everyone://TOKEN@default?&diffusionname=DIFFUSION_NAME&category=CATEGORY
11+
```
12+
13+
where:
14+
- `TOKEN` is your Contact Everyone api token
15+
- `DIFFUSION_NAME` (optional) allows you to define the label of the diffusion that will be displayed in the event logs.
16+
- `CATEGORY` (optional) allows you to define the label of the category that will be displayed in the event logs.
17+
18+
This bridge uses the light version of Contact Everyone api.
19+
20+
See your account info at https://contact-everyone.orange-business.com/#/login
21+
22+
Resources
23+
---------
24+
25+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
26+
* [Report issues](https://github.com/symfony/symfony/issues) and
27+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
28+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\ContactEveryone\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
final class ContactEveryoneTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): ContactEveryoneTransportFactory
20+
{
21+
return new ContactEveryoneTransportFactory();
22+
}
23+
24+
public function createProvider(): iterable
25+
{
26+
yield [
27+
'contact-everyone://host.test',
28+
'contact-everyone://[email protected]',
29+
];
30+
31+
yield [
32+
'contact-everyone://host.test?diffusionname=Symfony',
33+
'contact-everyone://[email protected]?diffusionname=Symfony',
34+
];
35+
36+
yield [
37+
'contact-everyone://host.test?category=Symfony',
38+
'contact-everyone://[email protected]?category=Symfony',
39+
];
40+
41+
yield [
42+
'contact-everyone://host.test?diffusionname=Symfony&category=Symfony',
43+
'contact-everyone://[email protected]?diffusionname=Symfony&category=Symfony',
44+
];
45+
}
46+
47+
public function supportsProvider(): iterable
48+
{
49+
yield [true, 'contact-everyone://token@default'];
50+
yield [false, 'somethingElse://token@default'];
51+
}
52+
53+
public function incompleteDsnProvider(): iterable
54+
{
55+
yield 'missing token' => ['contact-everyone://default'];
56+
}
57+
58+
public function unsupportedSchemeProvider(): iterable
59+
{
60+
yield ['somethingElse://token@default'];
61+
}
62+
}

0 commit comments

Comments
 (0)