-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] Add LightSms notifier bridge #40607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
62 commits
Select commit
Hold shift + click to select a range
b075c0e
* LightSms notifier
f2ba226
* LightSmsTransport.php - bug fix
be8f994
* LightSmsTransport.php - return type
167f325
* LightSmsTransport.php - logic error
6792535
* composer.json - requirements bug fix
2f65b92
* LightSmsTransport.php - Coding Standard
15686c0
* LightSmsTransport.php - Coding Standard
a0fae7d
* tests
d1ccd46
* Attached file changes which are required to run the lightsms notifier
ce41756
* notifier_transports.php - Coding Standard
728a3e2
* Transport.php - missing use
4a11b94
* github account author
febff46
Update src/Symfony/Component/Notifier/Bridge/LightSms/LICENSE
StaffNowa 9e1809e
* small changes
3cbbc85
* HOST split into two parts
5e54dfe
* LightSmsTransport.php - quick fix for private constant.
9f89014
Update src/Symfony/Component/Notifier/Bridge/LightSms/README.md
StaffNowa c02dbbd
Update src/Symfony/Component/Notifier/Bridge/LightSms/README.md
StaffNowa b2e4638
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa 49b4780
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa b0891be
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa 1b073c2
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa e0a68bd
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa 3d0d79c
Update src/Symfony/Component/Notifier/Bridge/LightSms/Tests/LightSmsT…
StaffNowa 0e41bc9
Update src/Symfony/Component/Notifier/Bridge/LightSms/Tests/LightSmsT…
StaffNowa 079406e
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa 7b51e0d
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa 2e0e1d7
Update README.md
StaffNowa 66c34ba
Update README.md
StaffNowa 8620e82
* LightSmsTransport.php - move timestamp
5d2e692
* LightSmsTransport.php - escape phone number
fc13bb2
* LightSmsTransport.php - changed login for validation (the same like…
b0e64b9
* LightSmsTransport.php - not ok throw exception
e20ef1e
* LightSmsTransport.php - change + to 00
7f13dbf
* sender changed to from
f16b4d2
* phone changed to from
7180c1f
Update LightSmsTransport.php
StaffNowa 08b0729
Update LightSmsTransport.php
StaffNowa 80ef5ba
* LightSmsTransport.php - Unable to send the SMS: Closing direction t…
08235e5
* LightSmsTransport.php - bug fix
23a446a
* LightSmsTransport.php - issue with Symfony\Component\Notifier\Bridg…
265f776
* LightSmsTransport.php - issue with Symfony\Component\Notifier\Bridg…
b9f9ff8
* LightSmsTransport.php - tests fail
58ac708
* LightSmsTransport.php - return back www (without will not work). No…
1b59a7d
* LightSmsTransport.php - string param
1c993b7
* ERROR_CODES -> int
1ff97e4
* LightSmsTransport.php - build signature and use http_build_query, t…
83d2598
* Coding Standard patch
a197dee
* LightSmsTransport.php - better to remove if we do not have it?
95e82f6
Update LightSmsTransport.php
StaffNowa 178d9c2
* pull request #40696
StaffNowa 9a832ef
* LightSmsTransport.php - via mistake removed www which return (Closi…
StaffNowa 0d7488b
* type cast. On success lightsms return error code like string. On er…
StaffNowa 9b2e2d0
* type cast. On success lightsms return error code like string. On er…
StaffNowa bea5256
* type cast. On success lightsms return error code like string. On er…
StaffNowa 21e972a
* coding standard
StaffNowa 4213564
* composer.json - fix Fabien comment from another pull request #40646
StaffNowa 2a9ac2d
* LightSmsTransport.php - fix
026dcd9
* LightSmsTransport.php - isset
f1f83b9
* LightSmsTransport.php - use query string parameters
68a12fa
* fix tests
37c665e
* LightSmsTransport.php - make fabbot happy
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/Symfony/Component/Notifier/Bridge/LightSms/.gitattributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2021 Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
179 changes: 179 additions & 0 deletions
179
src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTransport.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\LightSms; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Vasilij Duško <vasilij@prado.lt> | ||
*/ | ||
final class LightSmsTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'www.lightsms.com'; | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private $login; | ||
private $password; | ||
private $from; | ||
|
||
private const ERROR_CODES = [ | ||
1 => 'Missing Signature', | ||
2 => 'Login not specified', | ||
3 => 'Text not specified', | ||
4 => 'Phone number not specified', | ||
5 => 'Sender not specified', | ||
6 => 'Invalid signature', | ||
7 => 'Invalid login', | ||
8 => 'Invalid sender name', | ||
9 => 'Sender name not registered', | ||
10 => 'Sender name not approved', | ||
11 => 'There are forbidden words in the text', | ||
12 => 'Error in SMS sending', | ||
13 => 'Phone number is in the blackist. SMS sending to this number is forbidden.', | ||
14 => 'There are more than 50 numbers in the request', | ||
15 => 'List not specified', | ||
16 => 'Invalid phone number', | ||
17 => 'SMS ID not specified', | ||
18 => 'Status not obtained', | ||
19 => 'Empty response', | ||
20 => 'The number already exists', | ||
21 => 'No name', | ||
22 => 'Template already exists', | ||
23 => 'Missing Month (Format: YYYY-MM)', | ||
24 => 'Timestamp not specified', | ||
25 => 'Error in access to the list', | ||
26 => 'There are no numbers in the list', | ||
27 => 'No valid numbers', | ||
28 => 'Missing start date (Format: YYYY-MM-DD)', | ||
29 => 'Missing end date (Format: YYYY-MM-DD)', | ||
30 => 'No date (format: YYYY-MM-DD)', | ||
31 => 'Closing direction to the user', | ||
32 => 'Not enough money', | ||
33 => 'Missing phone number', | ||
34 => 'Phone is in stop list', | ||
35 => 'Not enough money', | ||
36 => 'Can not obtain information about phone', | ||
37 => 'Base Id is not set', | ||
38 => 'Phone number already exists in this database', | ||
39 => 'Phone number does not exist in this database', | ||
999 => 'Unknown Error', | ||
]; | ||
|
||
public function __construct(string $login, string $password, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->login = $login; | ||
$this->password = $password; | ||
$this->from = $from; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('lightsms://%s?from=%s', $this->getEndpoint(), $this->from); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage; | ||
} | ||
|
||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof SmsMessage) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); | ||
} | ||
|
||
$timestamp = time(); | ||
$data = [ | ||
'login' => $this->login, | ||
'phone' => $this->escapePhoneNumber($message->getPhone()), | ||
'text' => $message->getSubject(), | ||
'sender' => $this->from, | ||
'timestamp' => $timestamp, | ||
]; | ||
$data['signature'] = $this->generateSignature($data, $timestamp); | ||
|
||
$endpoint = sprintf('https://%s/external/get/send.php', $this->getEndpoint()); | ||
$response = $this->client->request( | ||
'GET', | ||
$endpoint, | ||
[ | ||
'query' => $data, | ||
] | ||
); | ||
|
||
if (Response::HTTP_OK !== $response->getStatusCode()) { | ||
throw new TransportException('Unable to send the SMS.', $response); | ||
} | ||
|
||
$content = $response->toArray(false); | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// it happens if the host without www | ||
if (isset($content['']['error'])) { | ||
throw new TransportException('Unable to send the SMS: '.$this->getErrorMsg((int) $content['']['error']), $response); | ||
} | ||
|
||
if (isset($content['error'])) { | ||
throw new TransportException('Unable to send the SMS: '.$this->getErrorMsg((int) $content['error']), $response); | ||
} | ||
|
||
$phone = $this->escapePhoneNumber($message->getPhone()); | ||
if (32 === (int) $content[$phone]['error']) { | ||
throw new TransportException('Unable to send the SMS: '.$this->getErrorMsg((int) $content[$phone]['error']), $response); | ||
} | ||
|
||
if (0 === (int) $content[$phone]['error']) { | ||
$sentMessage = new SentMessage($message, (string) $this); | ||
if (isset($content[$phone]['id_sms'])) { | ||
$sentMessage->setMessageId($content[$phone]['id_sms']); | ||
} | ||
|
||
return $sentMessage; | ||
} | ||
|
||
throw new TransportException('Unable to send the SMS.', $response); | ||
} | ||
|
||
private function generateSignature(array $data, int $timestamp): string | ||
{ | ||
$params = [ | ||
'timestamp' => $timestamp, | ||
'login' => $this->login, | ||
'phone' => $data['phone'], | ||
'sender' => $this->from, | ||
'text' => $data['text'], | ||
]; | ||
|
||
ksort($params); | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reset($params); | ||
|
||
return md5(implode('', $params).$this->password); | ||
} | ||
|
||
private function escapePhoneNumber(string $phoneNumber): string | ||
{ | ||
return str_replace('+', '00', $phoneNumber); | ||
} | ||
|
||
private function getErrorMsg(int $errorCode): string | ||
{ | ||
return self::ERROR_CODES[$errorCode] ?? self::ERROR_CODES[999]; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTransportFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\LightSms; | ||
|
||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
|
||
/** | ||
* @author Vasilij Duško <vasilij@prado.lt> | ||
*/ | ||
final class LightSmsTransportFactory extends AbstractTransportFactory | ||
{ | ||
/** | ||
* @return LightSmsTransport | ||
*/ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if ('lightsms' !== $scheme) { | ||
throw new UnsupportedSchemeException($dsn, 'lightsms', $this->getSupportedSchemes()); | ||
} | ||
|
||
$login = $this->getUser($dsn); | ||
$token = $this->getPassword($dsn); | ||
$from = $dsn->getRequiredOption('from'); | ||
|
||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
return (new LightSmsTransport($login, $token, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['lightsms']; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
LightSms Notifier | ||
================= | ||
|
||
Provides [LightSms](https://www.lightsms.com/) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
LIGHTSMS_DSN=lightsms://LOGIN:TOKEN@default?from=PHONE | ||
``` | ||
|
||
where: | ||
- `LOGIN` is your LightSms login | ||
- `TOKEN` is the token displayed in your account | ||
- `PHONE` is your LightSms sender phone number | ||
|
||
See your account info at https://www.lightsms.com/external/client/api/ | ||
|
||
Resources | ||
--------- | ||
|
||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Notifier/Bridge/LightSms/Tests/LightSmsTransportFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\LightSms\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory; | ||
use Symfony\Component\Notifier\Test\TransportFactoryTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportFactoryInterface; | ||
|
||
final class LightSmsTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
/** | ||
* @return LightSmsTransportFactory | ||
*/ | ||
public function createFactory(): TransportFactoryInterface | ||
{ | ||
return new LightSmsTransportFactory(); | ||
} | ||
|
||
public function createProvider(): iterable | ||
{ | ||
yield [ | ||
'lightsms://host.test?from=0611223344', | ||
'lightsms://login:token@host.test?from=0611223344', | ||
]; | ||
} | ||
|
||
public function supportsProvider(): iterable | ||
{ | ||
yield [true, 'lightsms://login:token@default?from=37061234567']; | ||
yield [false, 'somethingElse://login:token@default?from=37061234567']; | ||
} | ||
|
||
public function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield ['somethingElse://login:token@default?from=37061234567']; | ||
yield ['somethingElse://login:token@default']; // missing "from" option | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Notifier/Bridge/LightSms/Tests/LightSmsTransportTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\LightSms\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransport; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Test\TransportTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
final class LightSmsTransportTest extends TransportTestCase | ||
{ | ||
/** | ||
* @return LightSmsTransport | ||
*/ | ||
public function createTransport(?HttpClientInterface $client = null): TransportInterface | ||
{ | ||
return new LightSmsTransport('accountSid', 'authToken', 'from', $client ?: $this->createMock(HttpClientInterface::class)); | ||
} | ||
|
||
public function toStringProvider(): iterable | ||
{ | ||
yield ['lightsms://www.lightsms.com?from=from', $this->createTransport()]; | ||
} | ||
|
||
public function supportedMessagesProvider(): iterable | ||
{ | ||
yield [new SmsMessage('0611223344', 'Hello!')]; | ||
} | ||
|
||
public function unsupportedMessagesProvider(): iterable | ||
{ | ||
yield [new ChatMessage('Hello!')]; | ||
yield [$this->createMock(MessageInterface::class)]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.