Skip to content

Commit 5fcb1b8

Browse files
author
Olivier Dolbeau
committed
Add Esendex Notifier bridge
1 parent efdc35c commit 5fcb1b8

File tree

13 files changed

+429
-0
lines changed

13 files changed

+429
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
use Symfony\Component\Mime\Header\Headers;
9696
use Symfony\Component\Mime\MimeTypeGuesserInterface;
9797
use Symfony\Component\Mime\MimeTypes;
98+
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
9899
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
99100
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
100101
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
@@ -2129,6 +2130,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
21292130
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
21302131
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
21312132
SmsapiTransportFactory::class => 'notifier.transport_factory.smsapi',
2133+
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
21322134
];
21332135

21342136
foreach ($classToServices as $class => $service) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
1415
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
1516
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
1617
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
@@ -100,6 +101,10 @@
100101
->parent('notifier.transport_factory.abstract')
101102
->tag('texter.transport_factory')
102103

104+
->set('notifier.transport_factory.esendex', EsendexTransportFactory::class)
105+
->parent('notifier.transport_factory.abstract')
106+
->tag('texter.transport_factory')
107+
103108
->set('notifier.transport_factory.null', NullTransportFactory::class)
104109
->parent('notifier.transport_factory.abstract')
105110
->tag('chatter.transport_factory')
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<service id="notifier.transport_factory.abstract" class="Symfony\Component\Notifier\Transport\AbstractTransportFactory" abstract="true">
9+
<argument type="service" id="event_dispatcher" />
10+
<argument type="service" id="http_client" on-invalid="ignore" />
11+
</service>
12+
13+
<service id="notifier.transport_factory.slack" class="Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory" parent="notifier.transport_factory.abstract">
14+
<tag name="chatter.transport_factory" />
15+
</service>
16+
17+
<service id="notifier.transport_factory.telegram" class="Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory" parent="notifier.transport_factory.abstract">
18+
<tag name="chatter.transport_factory" />
19+
</service>
20+
21+
<service id="notifier.transport_factory.mattermost" class="Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory" parent="notifier.transport_factory.abstract">
22+
<tag name="chatter.transport_factory" />
23+
</service>
24+
25+
<service id="notifier.transport_factory.nexmo" class="Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory" parent="notifier.transport_factory.abstract">
26+
<tag name="texter.transport_factory" />
27+
</service>
28+
29+
<service id="notifier.transport_factory.rocketchat" class="Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory" parent="notifier.transport_factory.abstract">
30+
<tag name="chatter.transport_factory" />
31+
</service>
32+
33+
<service id="notifier.transport_factory.twilio" class="Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory" parent="notifier.transport_factory.abstract">
34+
<tag name="texter.transport_factory" />
35+
</service>
36+
37+
<service id="notifier.transport_factory.firebase" class="Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory" parent="notifier.transport_factory.abstract">
38+
<tag name="texter.transport_factory" />
39+
</service>
40+
41+
<service id="notifier.transport_factory.freemobile" class="Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory" parent="notifier.transport_factory.abstract">
42+
<tag name="texter.transport_factory" />
43+
</service>
44+
45+
<service id="notifier.transport_factory.ovhcloud" class="Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory" parent="notifier.transport_factory.abstract">
46+
<tag name="texter.transport_factory" />
47+
</service>
48+
49+
<service id="notifier.transport_factory.sinch" class="Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory" parent="notifier.transport_factory.abstract">
50+
<tag name="texter.transport_factory" />
51+
</service>
52+
53+
<service id="notifier.transport_factory.esendex" class="Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory" parent="notifier.transport_factory.abstract">
54+
<tag name="texter.transport_factory" />
55+
</service>
56+
57+
<service id="notifier.transport_factory.null" class="Symfony\Component\Notifier\Transport\NullTransportFactory" parent="notifier.transport_factory.abstract">
58+
<tag name="chatter.transport_factory" />
59+
<tag name="texter.transport_factory" />
60+
</service>
61+
</services>
62+
</container>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.2.0
5+
-----
6+
7+
* Added 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 <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\Esendex;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
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\Component\HttpClient\Exception\JsonException;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @experimental in 5.2
26+
*/
27+
final class EsendexTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.esendex.com';
30+
31+
private $token;
32+
private $accountReference;
33+
private $from;
34+
35+
public function __construct(string $token, string $accountReference, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->token = $token;
38+
$this->accountReference = $accountReference;
39+
$this->from = $from;
40+
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
return sprintf('esendex://%s', $this->getEndpoint());
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 LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
58+
}
59+
60+
$messageData = [
61+
'to' => $message->getPhone(),
62+
'body' => $message->getSubject(),
63+
];
64+
if (null !== $this->from) {
65+
$messageData['from'] = $this->from;
66+
}
67+
68+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v1.0/messagedispatcher', [
69+
'auth_basic' => $this->token,
70+
'headers' => [
71+
'Content-Type' => 'application/json',
72+
'Accept' => 'application/json',
73+
],
74+
'json' => [
75+
'accountreference' => $this->accountReference,
76+
'messages' => [$messageData],
77+
],
78+
]);
79+
80+
if (200 === $response->getStatusCode()) {
81+
return new SentMessage($message, (string) $this);
82+
}
83+
84+
$message = sprintf('Unable to send the SMS: error %d.', $response->getStatusCode());
85+
86+
try {
87+
$result = $response->toArray(false);
88+
if (!empty($result['errors'])) {
89+
$error = $result['errors'][0];
90+
91+
$message .= sprintf(' Details from Esendex: %s: "%s".', $error['code'], $error['description']);
92+
}
93+
} catch (JsonException $e) {
94+
}
95+
96+
throw new TransportException($message, $response);
97+
}
98+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Esendex;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @experimental in 5.2
21+
*/
22+
final class EsendexTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return EsendexTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
$token = $this->getUser($dsn).':'.$this->getPassword($dsn);
31+
$accountReference = $dsn->getOption('accountreference');
32+
$from = $dsn->getOption('from');
33+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34+
$port = $dsn->getPort();
35+
36+
if ('esendex' === $scheme) {
37+
return (new EsendexTransport($token, $accountReference, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
38+
}
39+
40+
throw new UnsupportedSchemeException($dsn, 'esendex', $this->getSupportedSchemes());
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return ['esendex'];
46+
}
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019-2020 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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Esendex Notifier
2+
================
3+
4+
Provides Esendex integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
// .env file
11+
ESENDEX_DSN='esendex://EMAIL:PASSWORD@default?accountreference=ACCOUNT_REFERENCE&from=FROM'
12+
```
13+
14+
where:
15+
- `EMAIL` is your Esendex account email
16+
- `PASSWORD` is the Esendex API password
17+
- `ACCOUNT_REFERENCE` is the Esendex account reference that the messages should be sent from.
18+
- `FROM` is the alphanumeric originator for the message to appear to originate from.
19+
20+
See your account info at https://mobile.free.fr/moncompte/index.php?page=options
21+
See Esendex documentation at https://developers.esendex.com/api-reference#smsapis
22+
23+
Resources
24+
---------
25+
26+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
27+
* [Report issues](https://github.com/symfony/symfony/issues) and
28+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
29+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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\Esendex\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\MockHttpClient;
16+
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransport;
17+
use Symfony\Component\Notifier\Exception\LogicException;
18+
use Symfony\Component\Notifier\Exception\TransportException;
19+
use Symfony\Component\Notifier\Message\SmsMessage;
20+
use Symfony\Component\Notifier\Message\MessageInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
use Symfony\Contracts\HttpClient\ResponseInterface;
23+
24+
final class EsendexTransportTest extends TestCase
25+
{
26+
public function testToString(): void
27+
{
28+
$transport = new EsendexTransport('testToken', 'accountReference', 'from', $this->createMock(HttpClientInterface::class));
29+
$transport->setHost('testHost');
30+
31+
$this->assertSame(sprintf('esendex://%s', 'testHost'), (string) $transport);
32+
}
33+
34+
public function testSupportsSmsMessage(): void
35+
{
36+
$transport = new EsendexTransport('testToken', 'accountReference', 'from', $this->createMock(HttpClientInterface::class));
37+
38+
$this->assertTrue($transport->supports(new SmsMessage('phone', 'testSmsMessage')));
39+
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
40+
}
41+
42+
public function testSendNonSmsMessageThrows(): void
43+
{
44+
$transport = new EsendexTransport('testToken', 'accountReference', 'from', $this->createMock(HttpClientInterface::class));
45+
46+
$this->expectException(LogicException::class);
47+
$transport->send($this->createMock(MessageInterface::class));
48+
}
49+
50+
public function testSendWithErrorResponseThrows(): void
51+
{
52+
$response = $this->createMock(ResponseInterface::class);
53+
$response->expects($this->exactly(2))
54+
->method('getStatusCode')
55+
->willReturn(500);
56+
57+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
58+
return $response;
59+
});
60+
61+
$transport = new EsendexTransport('testToken', 'accountReference', 'from', $client);
62+
63+
$this->expectException(TransportException::class);
64+
$this->expectExceptionMessage('Unable to send the SMS: error 500.');
65+
$transport->send(new SmsMessage('phone', 'testMessage'));
66+
}
67+
68+
public function testSendWithErrorResponseContainingDetailsThrows(): void
69+
{
70+
$response = $this->createMock(ResponseInterface::class);
71+
$response->expects($this->exactly(2))
72+
->method('getStatusCode')
73+
->willReturn(500);
74+
$response->expects($this->once())
75+
->method('getContent')
76+
->willReturn(json_encode(['errors' => [['code' => 'accountreference_invalid', 'description' => 'Invalid Account Reference EX0000000']]]));
77+
78+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
79+
return $response;
80+
});
81+
82+
$transport = new EsendexTransport('testToken', 'accountReference', 'from', $client);
83+
84+
$this->expectException(TransportException::class);
85+
$this->expectExceptionMessage('Unable to send the SMS: error 500. Details from Esendex: accountreference_invalid: "Invalid Account Reference EX0000000".');
86+
$transport->send(new SmsMessage('phone', 'testMessage'));
87+
}
88+
}

0 commit comments

Comments
 (0)