Skip to content

[Notifier] Add Mobyt bridge #36648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
Expand Down Expand Up @@ -2091,6 +2092,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
];

foreach ($classToServices as $class => $service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
Expand Down Expand Up @@ -85,6 +86,10 @@
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.mobyt', MobytTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.null', NullTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mobyt/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitignore export-ignore
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mobyt/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.2.0
-----

* Added the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mobyt/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019-2020 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
73 changes: 73 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mobyt/MobytOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Mobyt;

use Symfony\Component\Notifier\Message\MessageOptionsInterface;
use Symfony\Component\Notifier\Notification\Notification;

/**
* @author Bastien Durand <bdurand-dev@outlook.com>
*
* @experimental in 5.2
*/
final class MobytOptions implements MessageOptionsInterface
{
const MESSAGE_TYPE_QUALITY_HIGH = 'N';
const MESSAGE_TYPE_QUALITY_MEDIUM = 'L';
const MESSAGE_TYPE_QUALITY_LOW = 'LL';

private $options = [];

public function __construct(array $options = [])
{
$this->options = $options;
}

public static function fromNotification(Notification $notification): self
{
$options = new self();
switch ($notification->getImportance()) {
case Notification::IMPORTANCE_HIGH:
case Notification::IMPORTANCE_URGENT:
$options->messageType(self::MESSAGE_TYPE_QUALITY_HIGH);
break;
case Notification::IMPORTANCE_MEDIUM:
$options->messageType(self::MESSAGE_TYPE_QUALITY_MEDIUM);
break;
case Notification::IMPORTANCE_LOW:
$options->messageType(self::MESSAGE_TYPE_QUALITY_LOW);
break;
default:
$options->messageType(self::MESSAGE_TYPE_QUALITY_HIGH);
}

return $options;
}

public function toArray(): array
{
$options = $this->options;
unset($options['message'], $options['recipient']);

return $options;
}

public function getRecipientId(): ?string
{
return $this->options['recipient'] ?? null;
}

public function messageType(string $type)
{
$this->options['message_type'] = $type;
}
}
101 changes: 101 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Mobyt;

use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Basien Durand <bdurand-dev@outlook.com>
*
* @experimental in 5.2
*/
final class MobytTransport extends AbstractTransport
{
protected const HOST = 'app.mobyt.fr';

private $accountSid;
private $authToken;
private $from;
private $typeQuality;

public function __construct(string $accountSid, string $authToken, $from, string $typeQuality, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->accountSid = $accountSid;
$this->authToken = $authToken;
$this->from = $from;
$this->typeQuality = $typeQuality;

parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('mobyt://%s?from=%s&type_quality=%s', $this->getEndpoint(), $this->from, $this->typeQuality);
}

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage;
}

protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, \get_class($message)));
}

if ($message->getOptions() && !$message->getOptions() instanceof MobytOptions) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, MobytOptions::class));
}

$options = $message->getOptions() ? $message->getOptions()->toArray() : [];
$options['message_type'] = $options['message_type'] ?? $this->typeQuality;

$options['message'] = $options['message'] ?? $message->getSubject();
$options['recipient'] = [$message->getPhone()];

$options['sender'] = $options['sender'] ?? $this->from;

$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/API/v1.0/REST/sms', [
'headers' => [
'Content-type: application/json',
'user_key: '.$this->accountSid,
'Access_token: '.$this->authToken,
],
'body' => json_encode(array_filter($options)),
]);

if (401 === $response->getStatusCode() || 404 === $response->getStatusCode()) {
throw new TransportException(sprintf('Unable to send the SMS: "%s". Check your credentials.', $message->getSubject()), $response);
}

if (201 !== $response->getStatusCode()) {
$error = $response->toArray(false);

throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['result']), $response);
}

$success = $response->toArray(false);

$message = new SentMessage($message, (string) $this);
$message->setMessageId($success['order_id']);

return $message;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must return a SentMessage instance. Can you also test with a real account to be sure that everything works as expected?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Mobyt;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Transport\TransportInterface;

/**
* @author Bastien Durand <bdurand-dev@outlook.com>
*
* @experimental in 5.2
*/
final class MobytTransportFactory extends AbstractTransportFactory
{
/**
* @return MobytTransport
*/
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$accountSid = $this->getUser($dsn);
$authToken = $this->getPassword($dsn);
$from = $dsn->getOption('from');
$typeQuality = $dsn->getOption('type_quality', MobytOptions::MESSAGE_TYPE_QUALITY_LOW);
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

if ('mobyt' === $scheme) {
return (new MobytTransport($accountSid, $authToken, $from, $typeQuality, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

throw new UnsupportedSchemeException($dsn, 'mobyt', $this->getSupportedSchemes());
}

protected function getSupportedSchemes(): array
{
return ['mobyt'];
}
}
20 changes: 20 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mobyt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Mobyt Notifier
===============

Provides [Mobyt](https://www.mobyt.it/en/) integration for Symfony Notifier.

DSN should be as follow:

```
mobyt://USER_KEY:ACCESS_TOKEN@default?from=FROM
```

`USER_KEY` and `ACCESS_TOKEN` are given by Mobyt ; `FROM` is the sender name.

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Symfony\Component\Notifier\Bridge\Mobyt\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytOptions;
use Symfony\Component\Notifier\Notification\Notification;

class MobytOptionsTest extends TestCase
{
/**
* @dataProvider fromNotificationDataProvider
*/
public function testFromNotification($importance, $expectedMessageType)
{
$notification = (new Notification('Foo'))->importance($importance);

$options = (MobytOptions::fromNotification($notification))->toArray();

$this->assertEquals($expectedMessageType, $options['message_type']);
}

public function testFromNotificationDefaultLevel()
{
$notification = (new Notification('Foo'))->importance('Bar');

$options = (MobytOptions::fromNotification($notification))->toArray();

$this->assertEquals(MobytOptions::MESSAGE_TYPE_QUALITY_HIGH, $options['message_type']);
}

public function fromNotificationDataProvider(): \Generator
{
yield [Notification::IMPORTANCE_URGENT, MobytOptions::MESSAGE_TYPE_QUALITY_HIGH];
yield [Notification::IMPORTANCE_HIGH, MobytOptions::MESSAGE_TYPE_QUALITY_HIGH];
yield [Notification::IMPORTANCE_MEDIUM, MobytOptions::MESSAGE_TYPE_QUALITY_MEDIUM];
yield [Notification::IMPORTANCE_LOW, MobytOptions::MESSAGE_TYPE_QUALITY_LOW];
}

public function testGetRecipientIdWhenSet()
{
$options = [
'recipient' => 'foo',
];
$mobytOptions = new MobytOptions($options);

$this->assertEquals('foo', $mobytOptions->getRecipientId());
}

public function testGetRecipientIdWhenNotSet()
{
$this->assertNull((new MobytOptions())->getRecipientId());
}

public function testToArray()
{
$options = [
'message' => 'foo',
'recipient' => 'bar',
];
$this->assertEmpty((new MobytOptions($options))->toArray());
}

public function testMessageType()
{
$mobytOptions = new MobytOptions();
$mobytOptions->messageType('foo');

$this->assertEquals(['message_type' => 'foo'], $mobytOptions->toArray());
}
}
Loading