-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
[Notifier] Add Mobyt bridge #36648
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.2.0 | ||
----- | ||
|
||
* Added the bridge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
73
src/Symfony/Component/Notifier/Bridge/Mobyt/MobytOptions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
101
src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransport.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransportFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) |
71 changes: 71 additions & 0 deletions
71
src/Symfony/Component/Notifier/Bridge/Mobyt/Tests/MobytOptionsTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?