Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d46125a

Browse files
committed
feature #42180 [Notifier] Add bridge for smsc.ru (kozlice)
This PR was merged into the 5.4 branch. Discussion ---------- [Notifier] Add bridge for smsc.ru | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | symfony/symfony-docs#15536 Hi, This is a notifier bridge for Russian SMS provider [smsc](https://smsc.ru). This PR contains the new bridge and updates to framework bundle in all the places where all the other notifiers are listed. Commits ------- 1c935ce [Notifier] Add bridge for smsc.ru
2 parents b175ebd + 1c935ce commit d46125a

File tree

16 files changed

+371
-0
lines changed

16 files changed

+371
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
141141
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
142142
use Symfony\Component\Notifier\Bridge\SmsBiuras\SmsBiurasTransportFactory;
143+
use Symfony\Component\Notifier\Bridge\Smsc\SmscTransportFactory;
143144
use Symfony\Component\Notifier\Bridge\SpotHit\SpotHitTransportFactory;
144145
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
145146
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
@@ -2449,6 +2450,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24492450
SlackTransportFactory::class => 'notifier.transport_factory.slack',
24502451
SmsapiTransportFactory::class => 'notifier.transport_factory.smsapi',
24512452
SmsBiurasTransportFactory::class => 'notifier.transport_factory.smsbiuras',
2453+
SmscTransportFactory::class => 'notifier.transport_factory.smsc',
24522454
SpotHitTransportFactory::class => 'notifier.transport_factory.spothit',
24532455
TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
24542456
TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
4343
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
4444
use Symfony\Component\Notifier\Bridge\SmsBiuras\SmsBiurasTransportFactory;
45+
use Symfony\Component\Notifier\Bridge\Smsc\SmscTransportFactory;
4546
use Symfony\Component\Notifier\Bridge\SpotHit\SpotHitTransportFactory;
4647
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
4748
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
@@ -189,6 +190,10 @@
189190
->parent('notifier.transport_factory.abstract')
190191
->tag('texter.transport_factory')
191192

193+
->set('notifier.transport_factory.smsc', SmscTransportFactory::class)
194+
->parent('notifier.transport_factory.abstract')
195+
->tag('texter.transport_factory')
196+
192197
->set('notifier.transport_factory.messagebird', MessageBirdTransportFactory::class)
193198
->parent('notifier.transport_factory.abstract')
194199
->tag('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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.4
5+
---
6+
7+
* Add the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
SMSC Notifier
2+
=============
3+
4+
Provides [SMSC](https://smsc.ru/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
SMSC_DSN=smsc://LOGIN:PASSWORD@default?from=FROM
11+
```
12+
13+
where:
14+
- `LOGIN` is your login
15+
- `PASSWORD` is your API password
16+
- `FROM` is your sender (NB: text identity, not a phone number)
17+
18+
Resources
19+
---------
20+
21+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
22+
* [Report issues](https://github.com/symfony/symfony/issues) and
23+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
24+
in the [main Symfony repository](https://github.com/symfony/symfony)
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 <[email protected]>
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\Smsc;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SentMessage;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface as HttpDecodingExceptionInterface;
22+
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface as HttpExceptionInterface;
23+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface as HttpTransportExceptionInterface;
24+
use Symfony\Contracts\HttpClient\HttpClientInterface;
25+
26+
/**
27+
* @author Valentin Nazarov <[email protected]>
28+
*/
29+
final class SmscTransport extends AbstractTransport
30+
{
31+
protected const HOST = 'smsc.ru';
32+
33+
private $login;
34+
private $password;
35+
private $from;
36+
37+
public function __construct($username, $password, $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
38+
{
39+
$this->login = $username;
40+
$this->password = $password;
41+
$this->from = $from;
42+
43+
parent::__construct($client, $dispatcher);
44+
}
45+
46+
public function __toString(): string
47+
{
48+
return sprintf('smsc://%s?from=%s', $this->getEndpoint(), (string) $this->from);
49+
}
50+
51+
public function supports(MessageInterface $message): bool
52+
{
53+
return $message instanceof SmsMessage;
54+
}
55+
56+
protected function doSend(MessageInterface $message): SentMessage
57+
{
58+
if (!$message instanceof SmsMessage) {
59+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
60+
}
61+
62+
$body = [
63+
'login' => $this->login,
64+
'psw' => $this->password,
65+
'sender' => (string) $this->from,
66+
'phones' => $message->getPhone(),
67+
'mes' => $message->getSubject(),
68+
'fmt' => 3, // response as JSON
69+
'charset' => 'utf-8',
70+
'time' => '0-24',
71+
];
72+
73+
$endpoint = sprintf('https://%s/sys/send.php', $this->getEndpoint());
74+
$response = $this->client->request('POST', $endpoint, ['body' => $body]);
75+
76+
try {
77+
$result = $response->toArray();
78+
} catch (HttpTransportExceptionInterface $e) {
79+
throw new TransportException('Could not reach the remote smsc.ru server.', $response, 0, $e);
80+
} catch (HttpDecodingExceptionInterface $e) {
81+
throw new TransportException('Could not decode the response from remote smsc.ru server.', $response, 0, $e);
82+
} catch (HttpExceptionInterface $e) {
83+
throw new TransportException('Unexpected response from remote smsc.ru server.', $response, 0, $e);
84+
}
85+
86+
if (\array_key_exists('error', $result)) {
87+
throw new TransportException(sprintf('Unable to send the SMS: code = %d, message = "%s".', $result['error_code'], $result['error']), $response);
88+
}
89+
90+
$sentMessage = new SentMessage($message, (string) $this);
91+
$sentMessage->setMessageId((string) ($result['id'] ?? ''));
92+
93+
return $sentMessage;
94+
}
95+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Smsc;
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 Valentin Nazarov <[email protected]>
21+
*/
22+
final class SmscTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return SmscTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('smsc' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'smsc', $this->getSupportedSchemes());
33+
}
34+
35+
$login = $dsn->getUser();
36+
$password = $dsn->getPassword();
37+
$from = $dsn->getRequiredOption('from');
38+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
39+
40+
return (new SmscTransport($login, $password, $from, $this->client, $this->dispatcher))->setHost($host);
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return ['smsc'];
46+
}
47+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Smsc\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Smsc\SmscTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
final class SmscTransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
/**
21+
* @return SmscTransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
24+
{
25+
return new SmscTransportFactory();
26+
}
27+
28+
public function createProvider(): iterable
29+
{
30+
yield [
31+
'smsc://host.test?from=MyApp',
32+
'smsc://login:[email protected]?from=MyApp',
33+
];
34+
}
35+
36+
public function supportsProvider(): iterable
37+
{
38+
yield [true, 'smsc://login:password@default?from=MyApp'];
39+
yield [false, 'somethingElse://login:password@default?from=MyApp'];
40+
}
41+
42+
public function missingRequiredOptionProvider(): iterable
43+
{
44+
yield 'missing option: from' => ['smsc://login:password@default'];
45+
}
46+
47+
public function unsupportedSchemeProvider(): iterable
48+
{
49+
yield ['somethingElse://login:password@default?from=MyApp'];
50+
yield ['somethingElse://login:password@default']; // missing "from" option
51+
}
52+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Smsc\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Smsc\SmscTransport;
15+
use Symfony\Component\Notifier\Message\ChatMessage;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SmsMessage;
18+
use Symfony\Component\Notifier\Test\TransportTestCase;
19+
use Symfony\Component\Notifier\Transport\TransportInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
final class SmscTransportTest extends TransportTestCase
23+
{
24+
public function createTransport(HttpClientInterface $client = null): TransportInterface
25+
{
26+
return new SmscTransport('login', 'password', 'MyApp', $client ?? $this->createMock(HttpClientInterface::class));
27+
}
28+
29+
public function toStringProvider(): iterable
30+
{
31+
yield ['smsc://smsc.ru?from=MyApp', $this->createTransport()];
32+
}
33+
34+
public function supportedMessagesProvider(): iterable
35+
{
36+
yield [new SmsMessage('0611223344', 'Hello!')];
37+
}
38+
39+
public function unsupportedMessagesProvider(): iterable
40+
{
41+
yield [new ChatMessage('Hello!')];
42+
yield [$this->createMock(MessageInterface::class)];
43+
}
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "symfony/smsc-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony SMSC Notifier Bridge",
5+
"keywords": ["sms", "smsc", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Fabien Potencier",
11+
"email": "[email protected]"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": ">=7.2.5",
20+
"symfony/http-client": "^4.4|^5.2|^6.0",
21+
"symfony/notifier": "^5.3|^6.0"
22+
},
23+
"autoload": {
24+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Smsc\\": "" },
25+
"exclude-from-classmap": [
26+
"/Tests/"
27+
]
28+
},
29+
"minimum-stability": "dev"
30+
}

0 commit comments

Comments
 (0)