8000 [Notifier] Add bridge for smsc.ru · symfony/symfony@0a69dc2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a69dc2

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

15 files changed

+356
-0
lines changed

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

+2
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

+5
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')
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
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
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.
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)
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+
}
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;
10000
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+
}
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+
}
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\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+
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+
}
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": "fabien@symfony.com"
12+
},
13+
{
14+
"name": "Symfo F438 ny 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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
failOnRisky="true"
9+
failOnWarning="true"
10+
>
11+
<php>
12+
<ini name="error_reporting" value="-1" />
13+
</php>
14+
15+
<testsuites>
16+
<testsuite name="Symfony SMSC Notifier Bridge Test Suite">
17+
<directory>./Tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<filter>
22+
<whitelist>
23+
<directory>./</directory>
24+
<exclude>
25+
<directory>./Tests</directory>
26+
<directory>./vendor</directory>
27+
</exclude>
28+
</whitelist>
29+
</filter>
30+
</phpunit>

src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ class UnsupportedSchemeException extends LogicException
144144
'class' => Bridge\SmsBiuras\SmsBiurasTransportFactory::class,
145145
'package' => 'symfony/sms-biuras-notifier',
146146
],
147+
'smsc' => [
148+
'class' => Bridge\Smsc\SmscTransportFactory::class,
149+
'package' => 'symfony/smsc-notifier',
150+
],
147151
'spothit' => [
148152
'class' => Bridge\SpotHit\SpotHitTransportFactory::class,
149153
'package' => 'symfony/spot-hit-notifier',

0 commit comments

Comments
 (0)
0