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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
use Symfony\Component\Notifier\Bridge\Engagespot\EngagespotTransportFactory;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
Expand Down Expand Up @@ -2523,6 +2524,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
AllMySmsTransportFactory::class => 'notifier.transport_factory.all-my-sms',
AmazonSnsTransportFactory::class => 'notifier.transport_factory.amazon-sns',
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
ContactEveryoneTransportFactory::class => 'notifier.transport_factory.contact-everyone',
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
EngagespotTransportFactory::class => 'notifier.transport_factory.engagespot',
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
use Symfony\Component\Notifier\Bridge\Engagespot\EngagespotTransportFactory;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
Expand Down Expand Up @@ -197,6 +198,10 @@
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.contact-everyone', ContactEveryoneTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.amazon-sns', AmazonSnsTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

6.2
---

* Add the bridge
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\ContactEveryone;

use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Franck Ranaivo-Harisoa <[email protected]>
*/
final class ContactEveryoneTransport extends AbstractTransport
{
protected const HOST = 'contact-everyone.orange-business.com';

private string $token;
private ?string $diffusionName;
private ?string $category;

public function __construct(string $token, ?string $diffusionName, ?string $category, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->token = $token;
$this->diffusionName = $diffusionName;
$this->category = $category;

parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
$dsn = sprintf('contact-everyone://%s', $this->getEndpoint());

if ($this->diffusionName) {
$dsn .= sprintf('?diffusionname=%s', $this->diffusionName);
}

if ($this->category) {
$dsn .= sprintf('%scategory=%s', (null === $this->diffusionName) ? '?' : '&', $this->category);
}

return $dsn;
}

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage;
}

protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
}

$endpoint = sprintf('https://%s/api/light/diffusions/sms', self::HOST);
$response = $this->client->request('POST', $endpoint, [
'query' => [
'xcharset' => 'true',
'token' => $this->token,
'to' => $message->getPhone(),
'msg' => $message->getSubject(),
],
]);

try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Contact Everyone server.', $response, 0, $e);
}

if (200 !== $statusCode) {
$error = $response->toArray(false);
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);
}

$result = $response->getContent(false);

$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($result ?? '');

return $sentMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\ContactEveryone;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;

/**
* @author Franck Ranaivo-Harisoa <[email protected]>
*/
final class ContactEveryoneTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): ContactEveryoneTransport
{
if ('contact-everyone' !== $dsn->getScheme()) {
throw new UnsupportedSchemeException($dsn, 'contact-everyone', $this->getSupportedSchemes());
}

$token = $this->getUser($dsn);
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$diffusionName = $dsn->getOption('diffusionname');
$category = $dsn->getOption('category');

return (new ContactEveryoneTransport($token, $diffusionName, $category, $this->client, $this->dispatcher))->setHost($host)->setPort($dsn->getPort());
}

protected function getSupportedSchemes(): array
{
return ['contact-everyone'];
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/ContactEveryone/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2022 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
28 changes: 28 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/ContactEveryone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Contact Everyone Notifier
=========================

Provides [Contact everyone](https://www.orange-business.com/fr/produits/contact-everyone) integration for Symfony Notifier.

DSN example
-----------

```
CONTACT_EVERYONE_DSN=contact-everyone://TOKEN@default?&diffusionname=DIFFUSION_NAME&category=CATEGORY
```

where:
- `TOKEN` is your Contact Everyone API token
- `DIFFUSION_NAME` (optional) allows you to define the label of the diffusion that will be displayed in the event logs.
- `CATEGORY` (optional) allows you to define the label of the category that will be displayed in the event logs.

This bridge uses the light version of Contact Everyone API.

See your account info at https://contact-everyone.orange-business.com/#/login

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\ContactEveryone\Tests;

use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;

final class ContactEveryoneTransportFactoryTest extends TransportFactoryTestCase
{
public function createFactory(): ContactEveryoneTransportFactory
{
return new ContactEveryoneTransportFactory();
}

public function createProvider(): iterable
{
yield [
'contact-everyone://host.test',
'contact-everyone://[email protected]',
];

yield [
'contact-everyone://host.test?diffusionname=Symfony',
'contact-everyone://[email protected]?diffusionname=Symfony',
];

yield [
'contact-everyone://host.test?category=Symfony',
'contact-everyone://[email protected]?category=Symfony',
];

yield [
'contact-everyone://host.test?diffusionname=Symfony&category=Symfony',
'contact-everyone://[email protected]?diffusionname=Symfony&category=Symfony',
];
}

public function supportsProvider(): iterable
{
yield [true, 'contact-everyone://token@default'];
yield [false, 'somethingElse://token@default'];
}

public function incompleteDsnProvider(): iterable
{
yield 'missing token' => ['contact-everyone://default'];
}

public function unsupportedSchemeProvider(): iterable
{
yield ['somethingElse://token@default'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\ContactEveryone\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransport;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

final class ContactEveryoneTransportTest extends TransportTestCase
{
public function createTransport(HttpClientInterface $client = null): ContactEveryoneTransport
{
return new ContactEveryoneTransport('API_TOKEN', 'Symfony', 'Foo', $client ?? $this->createMock(HttpClientInterface::class));
}

public function toStringProvider(): iterable
{
yield ['contact-everyone://contact-everyone.orange-business.com?diffusionname=Symfony&category=Foo', $this->createTransport()];
}

public function supportedMessagesProvider(): iterable
{
yield [new SmsMessage('0611223344', 'Hello!')];
}

public function unsupportedMessagesProvider(): iterable
{
yield [new ChatMessage('Hello!')];
yield [$this->createMock(MessageInterface::class)];
}

public function testSendSuccessfully()
{
$messageId = Uuid::v4()->toRfc4122();
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$response->method('getContent')->willReturn($messageId);
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
return $response;
});

$transport = $this->createTransport($client);

$sentMessage = $transport->send(new SmsMessage('phone', 'testMessage'));

$this->assertSame($messageId, $sentMessage->getMessageId());
}
}
Loading