-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] add iqsms bridge #39096
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
fabpot
merged 1 commit into
symfony:5.x
from
alexandrbarabolia:feature/add_iqsms_bridge
Dec 17, 2020
Merged
[Notifier] add iqsms bridge #39096
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,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes 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.3.0 | ||
----- | ||
|
||
* Added the bridge |
88 changes: 88 additions & 0 deletions
88
src/Symfony/Component/Notifier/Bridge/Iqsms/IqsmsTransport.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,88 @@ | ||
<?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\Iqsms; | ||
|
||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; | ||
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 Oleksandr Barabolia <alexandrbarabolya@gmail.com> | ||
* | ||
* @experimental in 5.3 | ||
*/ | ||
final class IqsmsTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'api.iqsms.ru'; | ||
|
||
private $login; | ||
private $password; | ||
private $from; | ||
|
||
public function __construct(string $login, string $password, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->login = $login; | ||
$this->password = $password; | ||
$this->from = $from; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('iqsms://%s?from=%s', $this->getEndpoint(), $this->from); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage; | ||
} | ||
|
||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof SmsMessage) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); | ||
} | ||
|
||
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/messages/v2/send.json', [ | ||
'json' => [ | ||
'messages' => [ | ||
[ | ||
'phone' => $message->getPhone(), | ||
'text' => $message->getSubject(), | ||
'sender' => $this->from, | ||
'clientId' => uniqid(), | ||
], | ||
], | ||
'login' => $this->login, | ||
'password' => $this->password, | ||
], | ||
]); | ||
|
||
$result = $response->toArray(false); | ||
foreach ($result['messages'] as $msg) { | ||
if ('accepted' !== $msg['status']) { | ||
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $msg['status']), $response); | ||
} | ||
} | ||
|
||
$message = new SentMessage($message, (string) $this); | ||
$message->setMessageId($result['messages'][0]['smscId']); | ||
|
||
return $message; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Symfony/Component/Notifier/Bridge/Iqsms/IqsmsTransportFactory.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,56 @@ | ||
<?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\Iqsms; | ||
|
||
use Symfony\Component\Notifier\Exception\IncompleteDsnException; | ||
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 Oleksandr Barabolia <alexandrbarabolya@gmail.com> | ||
* | ||
* @experimental in 5.3 | ||
*/ | ||
final class IqsmsTransportFactory extends AbstractTransportFactory | ||
{ | ||
/** | ||
* @return IqsmsTransport | ||
*/ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if ('iqsms' !== $scheme) { | ||
throw new UnsupportedSchemeException($dsn, 'iqsms', $this->getSupportedSchemes()); | ||
} | ||
|
||
$login = $this->getUser($dsn); | ||
$password = $this->getPassword($dsn); | ||
$from = $dsn->getOption('from'); | ||
|
||
if (!$from) { | ||
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); | ||
} | ||
|
||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
return (new IqsmsTransport($login, $password, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['iqsms']; | ||
} | ||
} |
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) 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. |
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,24 @@ | ||
Iqsms Notifier | ||
============== | ||
|
||
Provides [Iqsms[(https://iqsms.ru) integration for Symfony Notifier. | ||
alexandrbarabolia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
IQSMS_DSN=iqsms://LOGIN:PASSWORD@default?from=FROM | ||
``` | ||
|
||
alexandrbarabolia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where: | ||
- `LOGIN` is your IQSMS login | ||
- `PASSWORD` is your IQSMS password | ||
- `FROM` is the sender | ||
|
||
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) |
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/Notifier/Bridge/Iqsms/Tests/IqsmsTransportFactoryTest.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,76 @@ | ||
<?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\Iqsms\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory; | ||
use Symfony\Component\Notifier\Exception\IncompleteDsnException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
|
||
final class IqsmsTransportFactoryTest extends TestCase | ||
{ | ||
public function testCreateWithDsn() | ||
{ | ||
$factory = $this->createFactory(); | ||
|
||
$transport = $factory->create(Dsn::fromString('iqsms://login:password@host.test?from=some')); | ||
$this->assertSame('iqsms://host.test?from=some', (string) $transport); | ||
} | ||
|
||
public function testCreateWithMissingOptionFromThrowsIncompleteDsnException() | ||
{ | ||
$factory = $this->createFactory(); | ||
|
||
$this->expectException(IncompleteDsnException::class); | ||
|
||
$factory->create(Dsn::fromString('iqsms://login:password@default')); | ||
} | ||
|
||
public function testSupportsReturnsTrueWithSupportedScheme() | ||
{ | ||
$factory = $this->createFactory(); | ||
|
||
$this->assertTrue($factory->supports(Dsn::fromString('iqsms://login:password@default?from=some'))); | ||
} | ||
|
||
public function testSupportsReturnsFalseWithUnsupportedScheme() | ||
{ | ||
$factory = $this->createFactory(); | ||
|
||
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://login:password@default?from=some'))); | ||
} | ||
|
||
public function testUnsupportedSchemeThrowsUnsupportedSchemeException() | ||
{ | ||
$factory = $this->createFactory(); | ||
|
||
$this->expectException(UnsupportedSchemeException::class); | ||
|
||
$factory->create(Dsn::fromString('somethingElse://login:password@default?from=some')); | ||
} | ||
|
||
public function testUnsupportedSchemeThrowsUnsupportedSchemeExceptionEvenIfRequiredOptionIsMissing() | ||
{ | ||
$factory = $this->createFactory(); | ||
|
||
$this->expectException(UnsupportedSchemeException::class); | ||
|
||
// unsupported scheme and missing "from" option | ||
$factory->create(Dsn::fromString('somethingElse://login:password@default')); | ||
} | ||
|
||
private function createFactory(): IqsmsTransportFactory | ||
{ | ||
return new IqsmsTransportFactory(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Symfony/Component/Notifier/Bridge/Iqsms/Tests/IqsmsTransportTest.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,51 @@ | ||
<?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\Iqsms\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransport; | ||
use Symfony\Component\Notifier\Exception\LogicException; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
final class IqsmsTransportTest extends TestCase | ||
{ | ||
public function testToStringContainsProperties() | ||
{ | ||
$transport = $this->createTransport(); | ||
|
||
$this->assertSame('iqsms://host.test?from=sender', (string) $transport); | ||
} | ||
|
||
public function testSupportsMessageInterface() | ||
{ | ||
$transport = $this->createTransport(); | ||
|
||
$this->assertTrue($transport->supports(new SmsMessage('9031223344', 'Hello!'))); | ||
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class))); | ||
} | ||
|
||
public function testSendNonSmsMessageThrowsException() | ||
{ | ||
$transport = $this->createTransport(); | ||
|
||
$this->expectException(LogicException::class); | ||
|
||
$transport->send($this->createMock(MessageInterface::class)); | ||
} | ||
|
||
private function createTransport(): IqsmsTransport | ||
{ | ||
return (new IqsmsTransport('login', 'password', 'sender', $this->createMock(HttpClientInterface::class)))->setHost('host.test'); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.