8000 feature #39096 [Notifier] add iqsms bridge (alexandrbarabolia) · symfony/symfony@e533bd3 · GitHub
[go: up one dir, main page]

Skip to content

Commit e533bd3

Browse files
committed
feature #39096 [Notifier] add iqsms bridge (alexandrbarabolia)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Notifier] add iqsms bridge | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | symfony/symfony-docs#14555 Hi, I've created integration to notifier to support russian sms operator - [iqsms](https://iqsms.ru) Can you grab this code and make as symfony/iqsms-notifier? This PR includes changes in notifier and framework-bundle to support smsapi transport as well as other included in notifier component. Could someone integrate this into notifier component? Commits ------- bf94bcb [Notifier] add iqsms bridge
2 parents 928594e + bf94bcb commit e533bd3

File tree

14 files changed

+403
-0
lines changed

14 files changed

+403
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
108108
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
109109
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
110+
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
110111
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
111112
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
112113
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
@@ -2220,6 +2221,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22202221
MattermostTransportFactory::class => 'notifier.transport_factory.mattermost',
22212222
GoogleChatTransportFactory::class => 'notifier.transport_factory.googlechat',
22222223
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
2224+
IqsmsTransportFactory::class => 'notifier.transport_factory.iqsms',
22232225
RocketChatTransportFactory::class => 'notifier.transport_factory.rocketchat',
22242226
InfobipTransportFactory::class => 'notifier.transport_factory.infobip',
22252227
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
1818
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
1919
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
20+
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
2122
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
2223
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
@@ -111,6 +112,10 @@
111112
->parent('notifier.transport_factory.abstract')
112113
->tag('texter.transport_factory')
113114

115+
->set('notifier.transport_factory.iqsms', IqsmsTransportFactory::class)
116+
->parent('notifier.transport_factory.abstract')
117+
->tag('texter.transport_factory')
118+
114119
->set('notifier.transport_factory.discord', DiscordTransportFactory::class)
115120
->parent('notifier.transport_factory.abstract')
116121
->tag('chatter.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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.3.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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\Iqsms;
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\< F438 span class=pl-v>Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Oleksandr Barabolia <alexandrbarabolya@gmail.com>
25+
*
26+
* @experimental in 5.3
27+
*/
28+
final class IqsmsTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'api.iqsms.ru';
31+
32+
private $login;
33+
private $password;
34+
private $from;
35+
36+
public function __construct(string $login, string $password, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
37+
{
38+
$this->login = $login;
39+
$this->password = $password;
40+
$this->from = $from;
41+
42+
parent::__construct($client, $dispatcher);
43+
}
44+
45+
public function __toString(): string
46+
{
47+
return sprintf('iqsms://%s?from=%s', $this->getEndpoint(), $this->from);
48+
}
49+
50+
public function supports(MessageInterface $message): bool
51+
{
52+
return $message instanceof SmsMessage;
53+
}
54+
55+
protected function doSend(MessageInterface $message): SentMessage
56+
{
57+
if (!$message instanceof SmsMessage) {
58+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
59+
}
60+
61+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/messages/v2/send.json', [
62+
'json' => [
63+
'messages' => [
64+
[
65+
'phone' => $message->getPhone(),
66+
'text' => $message->getSubject(),
67+
'sender' => $this->from,
68+
'clientId' => uniqid(),
69+
],
70+
],
71+
'login' => $this->login,
72+
'password' => $this->password,
73+
],
74+
]);
75+
76+
$result = $response->toArray(false);
77+
foreach ($result['messages'] as $msg) {
78+
if ('accepted' !== $msg['status']) {
79+
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $msg['status']), $response);
80+
}
81+
}
82+
83+
$message = new SentMessage($message, (string) $this);
84+
$message->setMessageId($result['messages'][0]['smscId']);
85+
86+
return $message;
87+
}
88+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Iqsms;
13+
14+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
16+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Notifier\Transport\Dsn;
18+
use Symfony\Component\Notifier\Transport\TransportInterface;
19+
20+
/**
21+
* @author Oleksandr Barabolia <alexandrbarabolya@gmail.com>
22+
*
23+
* @experimental in 5.3
24+
*/
25+
final class IqsmsTransportFactory extends AbstractTransportFactory
26+
{
27+
/**
28+
* @return IqsmsTransport
29+
*/
30+
public function create(Dsn $dsn): TransportInterface
31+
{
32+
$scheme = $dsn->getScheme();
33+
34+
if ('iqsms' !== $scheme) {
35+
throw new UnsupportedSchemeException($dsn, 'iqsms', $this->getSupportedSchemes());
36+
}
37+
38+
$login = $this->getUser($dsn);
39+
$password = $this->getPassword($dsn);
40+
$from = $dsn->getOption('from');
41+
42+
if (!$from) {
43+
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn());
44+
}
45+
46+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
47+
$port = $dsn->getPort();
48+
49+
return (new IqsmsTransport($login, $password, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
50+
}
51+
52+
protected function getSupportedSchemes(): array
53+
{
54+
return ['iqsms'];
55+
}
56+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 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+
Iqsms Notifier
2+
==============
3+
4+
Provides [Iqsms[(https://iqsms.ru) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
IQSMS_DSN=iqsms://LOGIN:PASSWORD@default?from=FROM
11+
```
12+
13+
where:
14+
- `LOGIN` is your IQSMS login
15+
- `PASSWORD` is your IQSMS password
16+
- `FROM` is the sender
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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Iqsms\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
16+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
17+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
18+
use Symfony\Component\Notifier\Transport\Dsn;
19+
20+
final class IqsmsTransportFactoryTest extends TestCase
21+
{
22+
public function testCreateWithDsn()
23+
{
24+
$factory = $this->createFactory();
25+
26+
$transport = $factory->create(Dsn::fromString('iqsms://login:password@host.test?from=some'));
27+
$this->assertSame('iqsms://host.test?from=some', (string) $transport);
28+
}
29+
30+
public function testCreateWithMissingOptionFromThrowsIncompleteDsnException()
31+
{
32+
$factory = $this->createFactory();
33+
34+
$this->expectException(IncompleteDsnException::class);
35+
36+
$factory->create(Dsn::fromString('iqsms://login:password@default'));
37+
}
38+
39+
public function testSupportsReturnsTrueWithSupportedScheme()
40+
{
41+
$factory = $this->createFactory();
42+
43+
$this->assertTrue($factory->supports(Dsn::fromString('iqsms://login:password@default?from=some')));
44+
}
45+
46+
public function testSupportsReturnsFalseWithUnsupportedScheme()
47+
{
48+
$factory = $this->createFactory();
49+
50+
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://login:password@default?from=some')));
51+
}
52+
53+
public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
54+
{
55+
$factory = $this->createFactory();
56+
57+
$this->expectException(UnsupportedSchemeException::class);
58+
59+
$factory->create(Dsn::fromString('somethingElse://login:password@default?from=some'));
60+
}
61+
62+
public function testUnsupportedSchemeThrowsUnsupportedSchemeExceptionEvenIfRequiredOptionIsMissing()
63+
{
64+
$factory = $this->createFactory();
65+
66+
$this->expectException(UnsupportedSchemeException::class);
67+
68+
// unsupported scheme and missing "from" option
69+
$factory->create(Dsn::fromString('somethingElse://login:password@default'));
70+
}
71+
72+
private function createFactory(): IqsmsTransportFactory
73+
{
74+
return new IqsmsTransportFactory();
75+
}
76+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Iqsms\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransport;
16+
use Symfony\Component\Notifier\Exception\LogicException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Contracts\HttpClient\HttpClientInterface;
20+
21+
final class IqsmsTransportTest extends TestCase
22+
{
23+
public function testToStringContainsProperties()
24+
{
25+
$transport = $this->createTransport();
26+
27+
$this->assertSame('iqsms://host.test?from=sender', (string) $transport);
28+
}
29+
30+
public function testSupportsMessageInterface()
31+
{
32+
$transport = $this->createTransport();
33+
34+
$this->assertTrue($transport->supports(new SmsMessage('9031223344', 'Hello!')));
35+
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
36+
}
37+
38+
public function testSendNonSmsMessageThrowsException()
39+
{
40+
$transport = $this->createTransport();
41+
42+
$this->expectException(LogicException::class);
43+
44+
$transport->send($this->createMock(MessageInterface::class));
45+
}
46+
47+
private function createTransport(): IqsmsTransport
48+
{
49+
return (new IqsmsTransport('login', 'password', 'sender', $this->createMock(HttpClientInterface::class)))->setHost('host.test');
50+
}
51+
}

0 commit comments

Comments
 (0)
0