Skip to content

Commit 3b5cdbb

Browse files
committed
Add Mobyt Notifier bridge
1 parent 0633308 commit 3b5cdbb

File tree

14 files changed

+416
-1
lines changed

14 files changed

+416
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
9595
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
9696
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
97+
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
9798
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
9899
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
99100
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
@@ -2047,6 +2048,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20472048
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',
20482049
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
20492050
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
2051+
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
20502052
];
20512053

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<tag name="texter.transport_factory" />
5151
</service>
5252

53+
<service id="notifier.transport_factory.mobyt" class="Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory" parent="notifier.transport_factory.abstract">
54+
<tag name="texter.transport_factory" />
55+
</service>
56+
5357
<service id="notifier.transport_factory.null" class="Symfony\Component\Notifier\Transport\NullTransportFactory" parent="notifier.transport_factory.abstract">
5458
<tag name="chatter.transport_factory" />
5559
<tag name="texter.transport_factory" />
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.1.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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.1
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']);
60+
unset($options['recipient']);
61+
62+
return $options;
63+
}
64+
65+
public function getRecipientId(): ?string
66+
{
67+
return $this->options['recipient'] ?? null;
68+
}
69+
70+
public function messageType(string $type)
71+
{
72+
$this->options['message_type'] = $type;
73+
}
74+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\SmsMessage;
18+
use Symfony\Component\Notifier\Transport\AbstractTransport;
19+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
/**
23+
* @author Basien Durand <bdurand-dev@outlook.com>
24+
*
25+
* @experimental in 5.1
26+
*/
27+
final class MobytTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'app.mobyt.fr';
30+
31+
private $accountSid;
32+
private $authToken;
33+
private $from;
34+
35+
public function __construct(string $accountSid, string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->accountSid = $accountSid;
38+
$this->authToken = $authToken;
39+
$this->from = $from;
40+
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
return sprintf('mobyt://%s?from=%s', $this->getEndpoint(), $this->from);
47+
}
48+
49+
public function supports(MessageInterface $message): bool
50+
{
51+
return $message instanceof SmsMessage;
52+
}
53+
54+
protected function doSend(MessageInterface $message): void
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_class($message)));
58+
}
59+
60+
if ($message->getOptions() && !$message->getOptions() instanceof MobytOptions) {
61+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, MobytOptions::class));
62+
}
63+
64+
if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) {
65+
$opts = MobytOptions::fromNotification($notification);
66+
}
67+
68+
$options = $opts ? $opts->toArray() : [];
69+
$options['message_type'] = $options['message_type'] ?? MobytOptions::MESSAGE_TYPE_QUALITY_HIGH;
70+
71+
$options['message'] = $options['message'] ?? $message->getSubject();
72+
$options['recipient'] = [$message->getPhone()];
73+
74+
$options['sender'] = $options['sender'] ?? $this->from;
75+
76+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/API/v1.0/REST/sms', [
77+
'headers' => [
78+
'Content-type: application/json',
79+
'user_key: '.$this->accountSid,
80+
'Access_token: '.$this->authToken,
81+
],
82+
'body' => json_encode(array_filter($options)),
83+
]);
84+
85+
if (401 === $response->getStatusCode() || 404 === $response->getStatusCode()) {
86+
throw new TransportException(sprintf('Unable to send the SMS: "%s". Check your credentials.', $message->getSubject()), $response);
87+
}
88+
89+
if (201 !== $response->getStatusCode()) {
90+
$error = $response->toArray(false);
91+
92+
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['result']), $response);
93+
}
94+
}
95+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.1
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+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
36+
$port = $dsn->getPort();
37+
38+
if ('mobyt' === $scheme) {
39+
return (new MobytTransport($accountSid, $authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
40+
}
41+
42+
throw new UnsupportedSchemeException($dsn, 'mobyt', $this->getSupportedSchemes());
43+
}
44+
45+
protected function getSupportedSchemes(): array
46+
{
47+
return ['mobyt'];
48+
}
49+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Mobyt Notifier
2+
===============
3+
4+
Provides [Mobyt](https://www.mobyt.it/en/) integration for Symfony Notifier.
5+
6+
Resources
7+
---------
8+
9+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
10+
* [Report issues](https://github.com/symfony/symfony/issues) and
11+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
12+
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy