Skip to content

Commit bf594b7

Browse files
committed
Add Mobyt Notifier bridge
1 parent d2b5ee0 commit bf594b7

File tree

13 files changed

+420
-0
lines changed

13 files changed

+420
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
100100
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
101101
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
102+
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
102103
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
103104
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
104105
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
@@ -2091,6 +2092,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20912092
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
20922093
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
20932094
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
2095+
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
20942096
];
20952097

20962098
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
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
1717
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
1818
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
19+
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
2122
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
@@ -85,6 +86,10 @@
8586
->parent('notifier.transport_factory.abstract')
8687
->tag('texter.transport_factory')
8788

89+
->set('notifier.transport_factory.mobyt', MobytTransportFactory::class)
90+
->parent('notifier.transport_factory.abstract')
91+
->tag('texter.transport_factory')
92+
8893
->set('notifier.transport_factory.null', NullTransportFactory::class)
8994
->parent('notifier.transport_factory.abstract')
9095
->tag('chatter.transport_factory')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitignore export-ignore
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: 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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Mobyt;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
use Symfony\Component\Notifier\Notification\Notification;
16+
17+
/**
18+
* @author Bastien Durand <bdurand-dev@outlook.com>
19+
*
20+
* @experimental in 5.2
21+
*/
22+
final class MobytOptions implements MessageOptionsInterface
23+
{
24+
const MESSAGE_TYPE_QUALITY_HIGH = 'N';
25+
const MESSAGE_TYPE_QUALITY_MEDIUM = 'L';
26+
const MESSAGE_TYPE_QUALITY_LOW = 'LL';
27+
28+
private $options = [];
29+
30+
public function __construct(array $options = [])
31+
{
32+
$this->options = $options;
33+
}
34+
35+
public static function fromNotification(Notification $notification): self
36+
{
37+
$options = new self();
38+
switch ($notification->getImportance()) {
39+
case Notification::IMPORTANCE_HIGH:
40+
case Notification::IMPORTANCE_URGENT:
41+
$options->messageType(self::MESSAGE_TYPE_QUALITY_HIGH);
42+
break;
43+
case Notification::IMPORTANCE_MEDIUM:
44+
$options->messageType(self::MESSAGE_TYPE_QUALITY_MEDIUM);
45+
break;
46+
case Notification::IMPORTANCE_LOW:
47+
$options->messageType(self::MESSAGE_TYPE_QUALITY_LOW);
48+
break;
49+
default:
50+
$options->messageType(self::MESSAGE_TYPE_QUALITY_HIGH);
51+
}
52+
53+
return $options;
54+
}
55+
56+
public function toArray(): array
57+
{
58+
$options = $this->options;
59+
unset($options['message'], $options['recipient']);
60+
61+
return $options;
62+
}
63+
64+
public function getRecipientId(): ?string
65+
{
66+
return $this->options['recipient'] ?? null;
67+
}
68+
69+
public function messageType(string $type)
70+
{
71+
$this->options['message_type'] = $type;
72+
}
73+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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\Mobyt;
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\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Basien Durand <bdurand-dev@outlook.com>
25+
*
26+
* @experimental in 5.2
27+
*/
28+
final class MobytTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'app.mobyt.fr';
31+
32+
private $accountSid;
33+
private $authToken;
34+
private $from;
35+
private $typeQuality;
36+
37+
public function __construct(string $accountSid, string $authToken, $from, string $typeQuality, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
38+
{
39+
$this->accountSid = $accountSid;
40+
$this->authToken = $authToken;
41+
$this->from = $from;
42+
$this->typeQuality = $typeQuality;
43+
44+
parent::__construct($client, $dispatcher);
45+
}
46+
47+
public function __toString(): string
48+
{
49+
return sprintf('mobyt://%s?from=%s&type_quality=%s', $this->getEndpoint(), $this->from, $this->typeQuality);
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 LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, \get_class($message)));
61+
}
62+
63+
if ($message->getOptions() && !$message->getOptions() instanceof MobytOptions) {
64+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, MobytOptions::class));
65+
}
66+
67+
$options = $message->getOptions() ? $message->getOptions()->toArray() : [];
68+
$options['message_type'] = $options['message_type'] ?? $this->typeQuality;
69+
70+
$options['message'] = $options['message'] ?? $message->getSubject();
71+
$options['recipient'] = [$message->getPhone()];
72+
73+
$options['sender'] = $options['sender'] ?? $this->from;
74+
75+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/API/v1.0/REST/sms', [
76+
'headers' => [
77+
'Content-type: application/json',
78+
'user_key: '.$this->accountSid,
79+
'Access_token: '.$this->authToken,
80+
],
81+
'body' => json_encode(array_filter($options)),
82+
]);
83+
84+
if (401 === $response->getStatusCode() || 404 === $response->getStatusCode()) {
85+
throw new TransportException(sprintf('Unable to send the SMS: "%s". Check your credentials.', $message->getSubject()), $response);
86+
}
87+
88+
if (201 !== $response->getStatusCode()) {
89+
$error = $response->toArray(false);
90+
91+
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['result']), $response);
92+
}
93+
94+
$success = $response->toArray(false);
95+
96+
$message = new SentMessage($message, (string) $this);
97+
$message->setMessageId($success['order_id']);
98+
99+
return $message;
100+
}
101+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Mobyt;
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 Bastien Durand <bdurand-dev@outlook.com>
21+
*
22+
* @experimental in 5.2
23+
*/
24+
final class MobytTransportFactory extends AbstractTransportFactory
25+
{
26+
/**
27+
* @return MobytTransport
28+
*/
29+
public function create(Dsn $dsn): TransportInterface
30+
{
31+
$scheme = $dsn->getScheme();
32+
$accountSid = $this->getUser($dsn);
33+
$authToken = $this->getPassword($dsn);
34+
$from = $dsn->getOption('from');
35+
$typeQuality = $dsn->getOption('type_quality', MobytOptions::MESSAGE_TYPE_QUALITY_LOW);
36+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
37+
$port = $dsn->getPort();
38+
39+
if ('mobyt' === $scheme) {
40+
return (new MobytTransport($accountSid, $authToken, $from, $typeQuality, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
41+
}
42+
43+
throw new UnsupportedSchemeException($dsn, 'mobyt', $this->getSupportedSchemes());
44+
}
45+
46+
protected function getSupportedSchemes(): array
47+
{
48+
return ['mobyt'];
49+
}
50+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Mobyt Notifier
2+
===============
3+
4+
Provides [Mobyt](https://www.mobyt.it/en/) integration for Symfony Notifier.
5+
6+
DSN should be as follow:
7+
8+
```
9+
mobyt://USER_KEY:ACCESS_TOKEN@default?from=FROM
10+
```
11+
12+
`USER_KEY` and `ACCESS_TOKEN` are given by Mobyt ; `FROM` is the sender name.
13+
14+
Resources
15+
---------
16+
17+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
18+
* [Report issues](https://github.com/symfony/symfony/issues) and
19+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
20+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Bridge\Mobyt\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Notifier\Bridge\Mobyt\MobytOptions;
7+
use Symfony\Component\Notifier\Notification\Notification;
8+
9+
class MobytOptionsTest extends TestCase
10+
{
11+
/**
12+
* @dataProvider fromNotificationDataProvider
13+
*/
14+
public function testFromNotification($importance, $expectedMessageType)
15+
{
16+
$notification = (new Notification('Foo'))->importance($importance);
17+
18+
$options = (MobytOptions::fromNotification($notification))->toArray();
19+
20+
$this->assertEquals($expectedMessageType, $options['message_type']);
21+
}
22+
23+
public function testFromNotificationDefaultLevel()
24+
{
25+
$notification = (new Notification('Foo'))->importance('Bar');
26+
27+
$options = (MobytOptions::fromNotification($notification))->toArray();
28+
29+
$this->assertEquals(MobytOptions::MESSAGE_TYPE_QUALITY_HIGH, $options['message_type']);
30+
}
31+
32+
public function fromNotificationDataProvider(): \Generator
33+
{
34+
yield [Notification::IMPORTANCE_URGENT, MobytOptions::MESSAGE_TYPE_QUALITY_HIGH];
35+
yield [Notification::IMPORTANCE_HIGH, MobytOptions::MESSAGE_TYPE_QUALITY_HIGH];
36+
yield [Notification::IMPORTANCE_MEDIUM, MobytOptions::MESSAGE_TYPE_QUALITY_MEDIUM];
37+
yield [Notification::IMPORTANCE_LOW, MobytOptions::MESSAGE_TYPE_QUALITY_LOW];
38+
}
39+
40+
public function testGetRecipientIdWhenSet()
41+
{
42+
$options = [
43+
'recipient' => 'foo',
44+
];
45+
$mobytOptions = new MobytOptions($options);
46+
47+
$this->assertEquals('foo', $mobytOptions->getRecipientId());
48+
}
49+
50+
public function testGetRecipientIdWhenNotSet()
51+
{
52+
$this->assertNull((new MobytOptions())->getRecipientId());
53+
}
54+
55+
public function testToArray()
56+
{
57+
$options = [
58+
'message' => 'foo',
59+
'recipient' => 'bar',
60+
];
61+
$this->assertEmpty((new MobytOptions($options))->toArray());
62+
}
63+
64+
public function testMessageType()
65+
{
66+
$mobytOptions = new MobytOptions();
67+
$mobytOptions->messageType('foo');
68+
69+
$this->assertEquals(['message_type' => 'foo'], $mobytOptions->toArray());
70+
}
71+
}

0 commit comments

Comments
 (0)