Skip to content

Commit 0a69dc2

Browse files
author
Valentin Nazarov
committed
[Notifier] Add bridge for smsc.ru
1 parent 7df57da commit 0a69dc2

File tree

15 files changed

+356
-0
lines changed

15 files changed

+356
-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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\ExceptionInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
class SmscTransport extends AbstractTransport
25+
{
26+
protected const HOST = 'smsc.ru';
27+
28+
private $login;
29+
private $password;
30+
private $from;
31+
32+
public function __construct($username, $password, $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
33+
{
34+
$this->login = $username;
35+
$this->password = $password;
36+
$this->from = $from;
37+
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return sprintf('smsc://%s?from=%s', $this->getEndpoint(), (string) $this->from);
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof SmsMessage;
49+
}
50+
51+
protected function doSend(MessageInterface $message): SentMessage
52+
{
53+
if (!$message instanceof SmsMessage) {
54+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
55+
}
56+
57+
$query = [
58+
'login' => $this->login,
59+
'psw' => $this->password,
60+
'sender' => (string) $this->from,
61+
'phones' => $message->getPhone(),
62+
'mes' => $message->getSubject(),
63+
'fmt' => 3, // response as JSON
64+
'charset' => 'utf-8',
65+
'time' => '0-24',
66+
];
67+
68+
$endpoint = sprintf('https://%s/sys/send.php', $this->getEndpoint());
69+
$response = $this->client->request('GET', $endpoint, ['query' => $query]);
70+
71+
try {
72+
$result = $response->toArray();
73+
} catch (ExceptionInterface $e) {
74+
throw new TransportException('Unable to send the SMS.', $response, 0, $e);
75+
}
76+
77+
if (\array_key_exists('error', $result)) {
78+
throw new TransportException(sprintf('Unable to send the SMS: code = %d, message = "%s".', $result['error_code'], $result['error']), $response);
79+
}
80+
81+
$sentMessage = new SentMessage($message, (string) $this);
82+
$sentMessage->setMessageId((string) ($result['id'] ?? ''));
83+
84+
return $sentMessage;
85+
}
86+
}
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 <fabien@symfony.com>
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+
class SmscTransportFactory extends AbstractTransportFactory
20+
{
21+
/**
22+
* @return SmscTransport
23+
*/
24+
public function create(Dsn $dsn): TransportInterface
25+
{
26+
$scheme = $dsn->getScheme();
27+
28+
if ('smsc' !== $scheme) {
29+
throw new UnsupportedSchemeException($dsn, 'smsc', $this->getSupportedSchemes());
30+
}
31+
32+
$login = $dsn->getUser();
33+
$password = $dsn->getPassword();
34+
$from = $dsn->getRequiredOption('from');
35+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
36+
37+
return (new SmscTransport($login, $password, $from, $this->client, $this->dispatcher))->setHost($host);
38+
}
39+
40+
protected function getSupportedSchemes(): array
41+
{
42+
return ['smsc'];
43+
}
44+
}
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 <fabien@symfony.com>
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+
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:password@host.test?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+
}

0 commit comments

Comments
 (0)