-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] Add bridge for smsc.ru #42180
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
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,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
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.4 | ||
--- | ||
|
||
* Add 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) 2021 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 @@ | ||
SMSC Notifier | ||
============= | ||
|
||
Provides [SMSC](https://smsc.ru/) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
SMSC_DSN=smsc://LOGIN:PASSWORD@default?from=FROM | ||
``` | ||
|
||
where: | ||
- `LOGIN` is your login | ||
- `PASSWORD` is your API password | ||
- `FROM` is your sender (NB: text identity, not a phone number) | ||
|
||
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) |
95 changes: 95 additions & 0 deletions
95
src/Symfony/Component/Notifier/Bridge/Smsc/SmscTransport.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,95 @@ | ||
<?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\Smsc; | ||
|
||
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\Exception\DecodingExceptionInterface as HttpDecodingExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface as HttpExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface as HttpTransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Valentin Nazarov <i.kozlice@protonmail.com> | ||
*/ | ||
final class SmscTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'smsc.ru'; | ||
|
||
private $login; | ||
private $password; | ||
private $from; | ||
|
||
public function __construct($username, $password, $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->login = $username; | ||
$this->password = $password; | ||
$this->from = $from; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('smsc://%s?from=%s', $this->getEndpoint(), (string) $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); | ||
} | ||
|
||
$body = [ | ||
'login' => $this->login, | ||
'psw' => $this->password, | ||
'sender' => (string) $this->from, | ||
'phones' => $message->getPhone(), | ||
'mes' => $message->getSubject(), | ||
'fmt' => 3, // response as JSON | ||
'charset' => 'utf-8', | ||
'time' => '0-24', | ||
]; | ||
|
||
$endpoint = sprintf('https://%s/sys/send.php', $this->getEndpoint()); | ||
$response = $this->client->request('POST', $endpoint, ['body' => $body]); | ||
|
||
try { | ||
$result = $response->toArray(); | ||
} catch (HttpTransportExceptionInterface $e) { | ||
throw new TransportException('Could not reach the remote smsc.ru server.', $response, 0, $e); | ||
} catch (HttpDecodingExceptionInterface $e) { | ||
throw new TransportException('Could not decode the response from remote smsc.ru server.', $response, 0, $e); | ||
} catch (HttpExceptionInterface $e) { | ||
throw new TransportException('Unexpected response from remote smsc.ru server.', $response, 0, $e); | ||
} | ||
|
||
if (\array_key_exists('error', $result)) { | ||
throw new TransportException(sprintf('Unable to send the SMS: code = %d, message = "%s".', $result['error_code'], $result['error']), $response); | ||
} | ||
|
||
$sentMessage = new SentMessage($message, (string) $this); | ||
$sentMessage->setMessageId((string) ($result['id'] ?? '')); | ||
|
||
return $sentMessage; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Notifier/Bridge/Smsc/SmscTransportFactory.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,47 @@ | ||
<?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\Smsc; | ||
|
||
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 Valentin Nazarov <i.kozlice@protonmail.com> | ||
*/ | ||
final class SmscTransportFactory extends AbstractTransportFactory | ||
{ | ||
/** | ||
* @return SmscTransport | ||
*/ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if ('smsc' !== $scheme) { | ||
throw new UnsupportedSchemeException($dsn, 'smsc', $this->getSupportedSchemes()); | ||
} | ||
|
||
$login = $dsn->getUser(); | ||
$password = $dsn->getPassword(); | ||
$from = $dsn->getRequiredOption('from'); | ||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
|
||
return (new SmscTransport($login, $password, $from, $this->client, $this->dispatcher))->setHost($host); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['smsc']; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Symfony/Component/Notifier/Bridge/Smsc/Tests/SmscTransportFactoryTest.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,52 @@ | ||
<?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\Smsc\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\Smsc\SmscTransportFactory; | ||
use Symfony\Component\Notifier\Test\TransportFactoryTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportFactoryInterface; | ||
|
||
final class SmscTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
/** | ||
* @return SmscTransportFactory | ||
*/ | ||
public function createFactory(): TransportFactoryInterface | ||
{ | ||
return new SmscTransportFactory(); | ||
} | ||
|
||
public function createProvider(): iterable | ||
{ | ||
yield [ | ||
'smsc://host.test?from=MyApp', | ||
'smsc://login:password@host.test?from=MyApp', | ||
]; | ||
} | ||
|
||
public function supportsProvider(): iterable | ||
{ | ||
yield [true, 'smsc://login:password@default?from=MyApp']; | ||
yield [false, 'somethingElse://login:password@default?from=MyApp']; | ||
} | ||
|
||
public function missingRequiredOptionProvider(): iterable | ||
{ | ||
yield 'missing option: from' => ['smsc://login:password@default']; | ||
} | ||
|
||
public function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield ['somethingElse://login:password@default?from=MyApp']; | ||
yield ['somethingElse://login:password@default']; // missing "from" option | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Symfony/Component/Notifier/Bridge/Smsc/Tests/SmscTransportTest.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,44 @@ | ||
<?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\Smsc\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\Smsc\SmscTransport; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Test\TransportTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
final class SmscTransportTest extends TransportTestCase | ||
{ | ||
public function createTransport(HttpClientInterface $client = null): TransportInterface | ||
{ | ||
return new SmscTransport('login', 'password', 'MyApp', $client ?? $this->createMock(HttpClientInterface::class)); | ||
} | ||
|
||
public function toStringProvider(): iterable | ||
{ | ||
yield ['smsc://smsc.ru?from=MyApp', $this->createTransport()]; | ||
} | ||
|
||
public function supportedMessagesProvider(): iterable | ||
{ | ||
yield [new SmsMessage('0611223344', 'Hello!')]; | ||
} | ||
|
||
public function unsupportedMessagesProvider(): iterable | ||
{ | ||
yield [new ChatMessage('Hello!')]; | ||
yield [$this->createMock(MessageInterface::class)]; | ||
} | ||
} |
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,30 @@ | ||
{ | ||
"name": "symfony/smsc-notifier", | ||
"type": "symfony-bridge", | ||
"description": "Symfony SMSC Notifier Bridge", | ||
"keywords": ["sms", "smsc", "notifier"], | ||
"homepage": "https://symfony.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Fabien Potencier", | ||
"email": "fabien@symfony.com" | ||
}, | ||
{ | ||
"name": "Symfony Community", | ||
"homepage": "https://symfony.com/contributors" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.2.5", | ||
"symfony/http-client": "^4.4|^5.2|^6.0", | ||
"symfony/notifier": "^5.3|^6.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Smsc\\": "" }, | ||
"exclude-from-classmap": [ | ||
"/Tests/" | ||
] | ||
}, | ||
"minimum-stability": "dev" | ||
} |
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.