8000 [Notifier] Add MessageMedia Bridge · symfony/symfony@69fa398 · GitHub
[go: up one dir, main page]

Skip to content

Commit 69fa398

Browse files
committed
[Notifier] Add MessageMedia Bridge
1 parent c82d8c9 commit 69fa398

15 files changed

+372
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
128128
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
129129
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransport;
130+
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaTransportFactory;
130131
use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransportFactory;
131132
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
132133
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
@@ -2430,6 +2431,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24302431
MattermostTransportFactory::class => 'notifier.transport_factory.mattermost',
24312432
MercureTransportFactory::class => 'notifier.transport_factory.mercure',
24322433
MessageBirdTransport::class => 'notifier.transport_factory.messagebird',
2434+
Messag 579F eMediaTransportFactory::class => 'notifier.transport_factory.messagemedia',
24332435
MicrosoftTeamsTransportFactory::class => 'notifier.transport_factory.microsoftteams',
24342436
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
24352437
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',

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

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
3030
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
3131
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransportFactory;
32+
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaTransportFactory;
3233
use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransportFactory;
3334
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
3435
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
@@ -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.messagemedia', MessageMediaTransportFactory::class)
195+
->parent('notifier.transport_factory.abstract')
196+
->tag('texter.transport_factory')
192197
;
193198
};
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
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
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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\MessageMedia;
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 Adrian Nguyen <vuphuong87@gmail.com>
25+
*/
26+
final class MessageMediaTransport extends AbstractTransport
27+
{
28+
protected const HOST = 'api.messagemedia.com';
29+
30+
private $apiKey;
31+
private $apiSecret;
32+
private $from;
33+
34+
public function __construct(string $apiKey, string $apiSecret, ?string $from = null, ?HttpClientInterface $client = null, ?EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->apiKey = $apiKey;
37+
$this->apiSecret = $apiSecret;
38+
$this->from = $from;
39+
40+
parent::__construct($client, $dispatcher);
41+
}
42+
43+
public function __toString(): string
44+
{
45+
if (null !== $this->from) {
46+
return sprintf('messagemedia://%s?from=%s', $this->getEndpoint(), $this->from);
47+
}
48+
49+
return sprintf('messagemedia://%s', $this->getEndpoint());
50+
}
51+
52+
public function supports(MessageInterface $message): bool
53+
{
54+
return $message instanceof SmsMessage;
55+
}
56+
57+
protected function doSend(MessageInterface $message): SentMessage
58+
{
59+
if (!$message instanceof SmsMessage) {
60+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
61+
}
62+
63+
$endpoint = sprintf('https://%s/v1/messages', $this->getEndpoint());
64+
$response = $this->client->request(
65+
'POST',
66+
$endpoint,
67+
[
68+
'auth_basic' => $this->apiKey.':'.$this->apiSecret,
69+
'body' => [
70+
'destination_number' => $message->getPhone(),
71+
'source_number' => $this->from,
72+
'content' => $message->getSubject(),
73+
],
74+
]
75+
);
76+
77+
if (202 !== $response->getStatusCode()) {
78+
$error = $response->toArray(false);
79+
80+
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['message']), $response);
81+
}
82+
83+
$success = $response->toArray(false)[0];
84+
85+
if (false === isset($success['message_id'])) {
86+
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $success['message']), $response);
87+
}
88+
89+
$sentMessage = new SentMessage($message, (string) $this);
90+
$sentMessage->setMessageId($success['message_id']);
91+
92+
return $sentMessage;
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\MessageMedia;
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 Adrian Nguyen <vuphuong87@gmail.com>
21+
*/
22+
final class MessageMediaTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return MessageMediaTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('messagemedia' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'messagemedia', $this->getSupportedSchemes());
33+
}
34+
35+
$apiKey = $this->getUser($dsn);
36+
$apiSecret = $this->getPassword($dsn);
37+
$from = $dsn->getOption('from');
38+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
39+
$port = $dsn->getPort();
40+
41+
return (new MessageMediaTransport($apiKey, $apiSecret, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
42+
}
43+
44+
protected function getSupportedSchemes(): array
45+
{
46+
return ['messagemedia'];
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
MessageMedia Notifier
2+
=================
3+
4+
Provides [MessageMedia](https://messagemedia.com/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
MESSAGEMEDIA_DSN=messagemedia://APIKEY:APISECRET@default?from=FROM
11+
```
12+
13+
where:
14+
- `APIKEY` is your API key
15+
- `APISECRET` is your API secret
16+
- `FROM` is your sender (optional)
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\MessageMedia\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
final class MessageMediaTransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
/**
21+
* @return MessageMediaTransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
24+
{
25+
return new MessageMediaTransportFactory();
26+
}
27+
28+
public function createProvider(): iterable
29+
{
30+
yield [
31+
'messagemedia://host.test',
32+
'messagemedia://apiKey:apiSecret@host.test',
33+
];
34+
35+
yield [
36+
'messagemedia://host.test?from=TEST',
37+
'messagemedia://apiKey:apiSecret@host.test?from=TEST',
38+
];
39+
}
40+
41+
public function supportsProvider(): iterable
42+
{
43+
yield [true, 'messagemedia://apiKey:apiSecret@default'];
44+
yield [false, 'somethingElse://apiKey:apiSecret@default'];
45+
}
46+
47+
public function unsupportedSchemeProvider(): iterable
48+
{
49+
yield ['somethingElse://apiKey:apiSecret@default'];
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\MessageMedia\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaTransport;
15+
use Symfony\Component\Notifier\Message\ChatMessage;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SmsMessage;
18+
use Symfony\Component\Notifier\Test\TransportTestCase;
19+
use Symfony\Component\Notifier\Transport\TransportInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
final class MessageMediaTransportTest extends TransportTestCase
23+
{
24+
/**
25+
* @return MessageMediaTransport
26+
*/
27+
public function createTransport(?HttpClientInterface $client = null, ?string $from = null): TransportInterface
28+
{
29+
return new MessageMediaTransport('apiKey', 'apiSecret', $from, $client ?? $this->createMock(HttpClientInterface::class));
30+
}
31+
32+
public function toStringProvider(): iterable
33+
{
34+
yield ['messagemedia://api.messagemedia.com', $this->createTransport()];
35+
yield ['messagemedia://api.messagemedia.com?from=TEST', $this->createTransport(null, 'TEST')];
36+
}
37+
38+
public function supportedMessagesProvider(): iterable
39+
{
40+
yield [new SmsMessage('0491570156', 'Hello!')];
41+
}
42+
43+
public function unsupportedMessagesProvider(): iterable
44+
{
45+
yield [new ChatMessage('Hello!')];
46+
yield [$this->createMock(MessageInterface::class)];
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "symfony/messagemedia-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony MessageMedia Notifier Bridge",
5+
"keywords": ["sms", "messagemedia", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Adrian Nguyen",
11+
"email": "vuphuong87@gmail.com"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": ">=7.2.5",
20+
"symfony/http-client": "^4.3|^5.0|^6.0",
21+
"symfony/notifier": "^5.3|^6.0"
22+
},
23+
"autoload": {
24+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\MessageMedia\\": "" },
25+
"exclude-from-classmap": [
26+
"/Tests/"
27+
]
28+
},
29+
"minimum-stability": "dev"
30+
}

0 commit comments

Comments
 (0)
0