8000 [Notifier] add Mailjet SMS bridge · symfony/symfony@ca85797 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca85797

Browse files
committed
[Notifier] add Mailjet SMS bridge
1 parent f12a419 commit ca85797

File tree

16 files changed

+361
-0
lines changed

16 files changed

+361
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
125125
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory;
126126
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
127+
use Symfony\Component\Notifier\Bridge\Mailjet\MailjetTransportFactory as MailjetNotifierTransportFactory;
127128
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
128129
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
129130
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransport;
@@ -2427,6 +2428,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24272428
IqsmsTransportFactory::class => 'notifier.transport_factory.iqsms',
24282429
LightSmsTransportFactory::class => 'notifier.transport_factory.lightsms',
24292430
LinkedInTransportFactory::class => 'notifier.transport_factory.linkedin',
2431+
MailjetNotifierTransportFactory::class => 'notifier.transport_factory.mailjet',
24302432
MattermostTransportFactory::class => 'notifier.transport_factory.mattermost',
24312433
MercureTransportFactory::class => 'notifier.transport_factory.mercure',
24322434
MessageBirdTransport::class => 'notifier.transport_factory.messagebird',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
2727
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory;
2828
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
29+
use Symfony\Component\Notifier\Bridge\Mailjet\MailjetTransportFactory;
2930
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
3031
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
3132
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransportFactory;
@@ -189,5 +190,9 @@
189190
->set('notifier.transport_factory.messagebird', MessageBirdTransportFactory::class)
190191
->parent('notifier.transport_factory.abstract')
191192
->tag('texter.transport_factory')
193+
194+
->set('notifier.transport_factory.mailjet', MailjetTransportFactory::class)
195+
->parent('notifier.transport_factory.abstract')
196+
->tag('texter.transport_factory')
192197
;
193198
};
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+
5.4
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) 2021 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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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\Mailjet;
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+
/**
24+
* @author Jérôme Nadaud <jerome@nadaud.io>
25+
*/
26+
final class MailjetTransport extends AbstractTransport
27+
{
28+
protected const HOST = 'api.mailjet.com';
29+
30+
private $authToken;
31+
private $from;
32+
33+
public function __construct(string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
34+
{
35+
$this->authToken = $authToken;
36+
$this->from = $from;
37+
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return sprintf('mailjet://%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+
$endpoint = sprintf('https://%s/v4/sms-send', $this->getEndpoint());
58+
59+
$response = $this->client->request('POST', $endpoint, [
60+
'auth_bearer' => $this->authToken,
61+
'json' => [
62+
'From' => $this->from,
63+
'To' => $message->getPhone(),
64+
'Text' => $message->getSubject(),
65+
],
66+
]);
67+
68+
if (200 !== $response->getStatusCode()) {
69+
$content = $response->toArray(false);
70+
$errorMessage = $content['requestError']['serviceException']['messageId'] ?? '';
71+
$errorInfo = $content['requestError']['serviceException']['text'] ?? '';
72+
73+
throw new TransportException(sprintf('Unable to send the SMS: '.$errorMessage.' (%s).', $errorInfo), $response);
74+
}
75+
76+
return new SentMessage($message, (string) $this);
77+
}
78+
}
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\Mailjet;
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+
* @author Jérôme Nadaud <jerome@nadaud.io>
21+
*/
22+
final class MailjetTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return MailjetTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('mailjet' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'mailjet', $this->getSupportedSchemes());
33+
}
34+
35+
$authToken = $this->getPassword($dsn);
36+
$from = $this->getUser($dsn);
37+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
38+
$port = $dsn->getPort();
39+
40+
return (new MailjetTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return ['mailjet'];
46+
}
47+
}
Lines changed: 23 additions & 0 deletions
  • 38BA
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Mailjet Notifier
2+
================
3+
4+
Provides [Mailjet](https://mailjet.com) SMS integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
MAILJET_DSN=mailjet://FROM:AUTH_TOKEN@default
11+
```
12+
13+
where:
14+
- `AUTH_TOKEN` is your Mailjet SMS auth token
15+
- `FROM` is an alphanumeric sender ID
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Mailjet\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Mailjet\MailjetTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
final class MailjetTransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
/**
21+
* @return MailjetTransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
24+
{
25+
return new MailjetTransportFactory();
26+
}
27+
28+
public function createProvider(): iterable
29+
{
30+
yield [
31+
'mailjet://Mailjet@host.test',
32+
'mailjet://Mailjet:authtoken@host.test',
33+
];
34+
}
35+
36+
public function supportsProvider(): iterable
37+
{
38+
yield [true, 'mailjet://Mailjet:authtoken@default'];
39+
yield [false, 'somethingElse://Mailjet:authtoken@default'];
40+
}
41+
42+
public function missingRequiredOptionProvider(): iterable
43+
{
44+
yield 'missing option: from' => ['mailjet://authtoken@default'];
45+
}
46+
47+
public function unsupportedSchemeProvider(): iterable
48+
{
49+
yield ['somethingElse://default']; // missing "from" and "token" option
50+
yield ['somethingElse://authtoken@default']; // missing "from" option
51+
}
52+
}

0 commit comments

Comments
 (0)
0