8000 * LightSms notifier · symfony/symfony@b075c0e · GitHub
[go: up one dir, main page]

Skip to content

Commit b075c0e

Browse files
Vasilij DuskoVasilij Dusko | CREATION
Vasilij Dusko
authored and
Vasilij Dusko | CREATION
committed
* LightSms notifier
1 parent 40a2c19 commit b075c0e

File tree

6 files changed

+310
-0
lines changed

6 files changed

+310
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020-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: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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\LightSms;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Message\SmsMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Vasilij Duško <vasilij@d4d.lt>
26+
*/
27+
final class LightSmsTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'www.lightsms.com/external/get/send.php';
30+
31+
private $login;
32+
private $password;
33+
private $phone;
34+
/**
35+
* @var MessageInterface
36+
*/
37+
private $message;
38+
39+
private $errorCodes = [
40+
'000' => 'Service unavailable',
41+
'1' => 'Signature not specified',
42+
'2' => 'Login not specified',
43+
'3' => 'Text not specified',
44+
'4' => 'Phone number not specified',
45+
'5' => 'Sender not specified',
46+
'6' => 'Invalid signature',
47+
'7' => 'Invalid login',
48+
'8' => 'Invalid sender name',
49+
'9' => 'Sender name not registered',
50+
'10' => 'Sender name not approved',
51+
'11' => 'There are forbidden words in the text',
52+
'12' => 'Error in SMS sending',
53+
'13' => 'Phone number is in the blackist. SMS sending to this number is forbidden.',
54+
'14' => 'There are more than 50 numbers in the request',
55+
'15' => 'List not specified',
56+
'16' => 'Invalid phone number',
57+
'17' => 'SMS ID not specified',
58+
'18' => 'Status not obtained',
59+
'19' => 'Empty response',
60+
'20' => 'The number already exists',
61+
'21' => 'No name',
62+
'22' => 'Template already exists',
63+
'23' => 'Month not specifies (Format: YYYY-MM)',
64+
'24' => 'Timestamp not specified',
65+
'25' =>'Error in access to the list',
66+
'26' => 'There are no numbers in the list',
67+
'27' => 'No valid numbers',
68+
'28' => 'Date of start not specified (Format: YYYY-MM-DD)',
69+
'29' => 'Date of end not specified (Format: YYYY-MM-DD)',
70+
'30' => 'No date (format: YYYY-MM-DD)',
71+
'31' => 'Closing direction to the user',
72+
'32' => 'Not enough money',
73+
'33' => 'Phone number is not set',
74+
'34' => 'Phone is in stop list',
75+
'35' => 'Not enough money',
76+
'36' => 'Can not obtain information about phone',
77+
'37' => 'Base Id is not set',
78+
'38' => 'Phone number is already exist in this base',
79+
'39' => 'Phone number is not exist in this base',
80+
];
81+
82+
public function __construct(
83+
string $login,
84+
string $password,
85+
string $phone,
86+
HttpClientInterface $client = null,
87+
EventDispatcherInterface $dispatcher = null
88+
) {
89+
$this->login = $login;
90+
$this->password = $password;
91+
$this->phone = $phone;
92+
93+
parent::__construct($client, $dispatcher);
94+
}
95+
96+
public function __toString(): string
97+
{
98+
return sprintf('lightsms://%s?phone=%s', $this->getEndpoint(), $this->phone);
99+
}
100+
101+
public function supports(MessageInterface $message): bool
102+
{
103+
return $message instanceof SmsMessage && $this->phone === str_replace('+', '', $message->getPhone());
104+
}
105+
106+
protected function doSend(MessageInterface $message): void
107+
{
108+
if (!$message instanceof SmsMessage) {
109+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, \get_class($message)));
110+
}
111+
112+
$this->message = $message;
113+
114+
$signature = $this->getSignature();
115+
116+
$endpoint = sprintf(
117+
'https://%s?login=%s&signature=%s&phone=%s&text=%s&sender=%s&timestamp=%s',
118+
$this->getEndpoint(),
119+
$this->login,
120+
$signature,
121+
str_replace('+', '', $message->getPhone()),
122+
$message->getSubject(),
123+
$this->phone,
124+
time()
125+
);
126+
127+
128+
$response = $this->client->request('GET', $endpoint);
129+
130+
$content = $response->toArray(false);
131+
132+
if (isset($content['error'])) {
133+
throw new TransportException('Unable to send the SMS: '.$this->errorCodes[$content['error']], $response);
134+
}
135+
}
136+
137+
private function getSignature(): string
138+
{
139+
140+
$params = [
141+
'timestamp' => time(),
142+
'login' => $this->login,
143+
'phone' => str_replace('+', '', $this->message->getPhone()),
144+
'sender' => $this->phone,
145+
'text' => $this->message->getSubject(),
146+
];
147+
148+
ksort($params);
149+
reset($params);
150+
151+
return md5(implode($params) . $this->password);
152+
}
153+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\LightSms;
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 Vasilij Duško <vasilij@d4d.lt>
21+
*/
22+
final class LightSmsTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return LightSmsTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('lightsms' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'lightsms', $this->getSupportedSchemes());
33+
}
34+
35+
$login = $this->getUser($dsn);
36+
$token = $this->getPassword($dsn);
37+
$phone = $dsn->getOption('phone');
38+
39+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
40+
$port = $dsn->getPort();
41+
42+
return (new LightSmsTransport($login, $token, $phone, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
43+
}
44+
45+
protected function getSupportedSchemes(): array
46+
{
47+
return ['lightsms'];
48+
}
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
LightSms Notifier
2+
====================
3+
4+
Provides [LightSms](https://www.lightsms.com/) integration for Symfony Notifier.
5+
6+
This provider allows you to receive an SMS notification on your mobile number.
7+
8+
DSN example
9+
-----------
10+
11+
```
12+
LIGHTSMS_DSN=lightsms://LOGIN:TOKEN@default?phone=PHONE
13+
```
14+
15+
where:
16+
- `LOGIN` is your LightSms login
17+
- `TOKEN` is the token displayed in your account
18+
- `PHONE` is your LightSms phone number
19+
20+
See your account info at https://www.lightsms.com/external/client/api/
21+
22+
Resources
23+
---------
24+
25+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
26+
* [Report issues](https://github.com/symfony/symfony/issues) and
27+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
28+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "symfony/lightsms-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony LightSms Notifier Bridge",
5+
"keywords": ["sms", "LightSms", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Vasilij Duško",
11+
"email": "vasilij@d4d.lt"
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.3|^5.0",
21+
"symfony/notifier": "~5.0.0"
22+
},
23+
"autoload": {
24+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\LightSms\\": "" },
25+
"exclude-from-classmap": [
26+
"/Tests/"
27+
]
28+
},
29+
"minimum-stability": "dev"
30+
}
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 LightSms 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>

0 commit comments

Comments
 (0)
0