8000 add iqsms bridge · symfony/symfony@1f1ceda · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f1ceda

Browse files
add iqsms bridge
1 parent c82c997 commit 1f1ceda

File tree

12 files changed

+263
-0
lines changed

12 files changed

+263
-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;
@@ -2224,6 +2225,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22242225
MattermostTransportFactory::class => 'notifier.transport_factory.mattermost',
22252226
GoogleChatTransportFactory::class => 'notifier.transport_factory.googlechat',
22262227
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
2228+
IqsmsTransportFactory::class => 'notifier.transport_factory.iqsms',
22272229
RocketChatTransportFactory::class => 'notifier.transport_factory.rocketchat',
22282230
InfobipTransportFactory::class => 'notifier.transport_factory.infobip',
22292231
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.2.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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\LogicException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
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\HttpClientInterface;
22+
23+
/**
24+
* @author Oleksandr Barabolia <alexandrbarabolya@gmail.com>
25+
*
26+
* @experimental in 5.2
27+
*/
28+
final class IqsmsTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'api.iqsms.ru';
31+
32+
protected const STATUS_ACCEPTED = 'accepted';
33+
34+
private $login;
35+
private $password;
36+
private $from;
37+
38+
public function __construct(string $login, string $password, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
39+
{
40+
$this->login = $login;
41+
$this->password = $password;
42+
$this->from = $from;
43+
44+
parent::__construct($client, $dispatcher);
45+
}
46+
47+
public function __toString(): string
48+
{
49+
return sprintf('iqsms://%s?from=%s', $this->getEndpoint(), $this->from);
50+
}
51+
52+
public function supports(MessageInterface $message): bool
53+
{
54+
return $message instanceof SmsMessage;
55+
}
56+
57+
protected function doSend(MessageInterface $message): SentMessage
58+
{
59+
if (!$message instanceof SmsMessage) {
60+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
61+
}
62+
63+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/messages/v2/send.json', [
64+
'json' => [
65+
'messages' => [
66+
[
67+
'phone' => $message->getPhone(),
68+
'text' => $message->getSubject(),
69+
'sender' => $this->from,
70+
'clientId' => uniqid(),
71+
],
72+
],
73+
'login' => $this->login,
74+
'password' => $this->password,
75+
],
76+
]);
77+
78+
$result = $response->toArray(false);
79+
foreach ($result['messages'] as $msg) {
80+
if ($msg['status'] === static::STATUS_ACCEPTED) {
81+
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $msg['status']), $response);
82+
}
83+
}
84+
85+
$success = $response->toArray(false);
86+
87+
$message = new SentMessage($message, (string) $this);
88+
$message->setMessageId($success['messages'][0]['smscId']);
89+
90+
return $message;
91+
}
92+
}
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;
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 Oleksandr Barabolia <alexandrbarabolya@gmail.com>
21+
*
22+
* @experimental in 5.2
23+
*/
24+
final class IqsmsTransportFactory extends AbstractTransportFactory
25+
{
26+
/**
27+
* @return IqsmsTransport
28+
*/
29+
public function create(Dsn $dsn): TransportInterface
30+
{
31+
$scheme = $dsn->getScheme();
32+
$login = $this->getUser($dsn);
33+
$password = $this->getPassword($dsn);
34+
$from = $dsn->getOption('from');
35+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
36+
$port = $dsn->getPort();
37+
38+
if ('iqsms' === $scheme) {
39+
return (new IqsmsTransport($login, $password, $from, $this->client, $this->dispatcher))
40+
->setHost($host)
41+
->setPort($port);
42+
}
43+
44+
throw new UnsupportedSchemeException($dsn, 'iqsms', $this->getSupportedSchemes());
45+
}
46+
47+
protected function getSupportedSchemes(): array
48+
{
49+
return ['iqsms'];
50+
}
51+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Nexmo Notifier
2+
==============
3+
4+
Provides Iqsms integration for Symfony Notifier.
5+
6+
Resources
7+
---------
8+
9+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
10+
* [Report issues](https://github.com/symfony/symfony/issues) and
11+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
12+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "symfony/iqsms-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony Iqsms Notifier Bridge",
5+
"keywords": ["sms", "iqsms", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Oleksandr Barabolia",
11+
"email": "alexandrbarabolya@gmail.com"
12+
},
13+
{
14+
"name": "Fabien Potencier",
15+
"email": "fabien@symfony.com"
16+
},
17+
{
18+
"name": "Symfony Community",
19+
"homepage": "https://symfony.com/contributors"
20+
}
21+
],
22+
"require": {
23+
"php": ">=7.2.5",
24+
"symfony/http-client": "^4.3|^5.0",
25+
"symfony/notifier": "^5.2"
26+
},
27+
"autoload": {
28+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Iqsms\\": "" },
29+
"exclude-from-classmap": [
30+
"/Tests/"
31+
]
32+
},
33+
"minimum-stability": "dev"
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 Nexmo Notifier Bridge Test Suite">
17+
<directory>./Tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<filter>
22+
<whitelist>
23+
<directory>./</directory>
24+
<exclude>
25+
<directory>./Resources</directory>
26+
<directory>./Tests</directory>
27+
<directory>./vendor</directory>
28+
</exclude>
29+
</whitelist>
30+
</filter>
31+
</phpunit>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class UnsupportedSchemeException extends LogicException
4242
'class' => Bridge\Nexmo\NexmoTransportFactory::class,
4343
'package' => 'symfony/nexmo-notifier',
4444
],
45+
'iqsms' => [
46+
'class' => Bridge\Iqsms\IqsmsTransportFactory::class,
47+
'package' => 'symfony/iqsms-notifier',
48+
],
4549
'rocketchat' => [
4650
'class' => Bridge\RocketChat\RocketChatTransportFactory::class,
4751
'package' => 'rocketchat-notifier',

src/Symfony/Component/Notifier/Transport.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
1717
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
1818
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
19+
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
2122
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
@@ -51,6 +52,7 @@ class Transport
5152
TelegramTransportFactory::class,
5253
MattermostTransportFactory::class,
5354
NexmoTransportFactory::class,
55+
IqsmsTransportFactory::class,
5456
RocketChatTransportFactory::class,
5557
TwilioTransportFactory::class,
5658
InfobipTransportFactory::class,

0 commit comments

Comments
 (0)
0