Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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 62 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
b075c0e
* LightSms notifier
Mar 28, 2021
f2ba226
* LightSmsTransport.php - bug fix
Mar 28, 2021
be8f994
* LightSmsTransport.php - return type
Mar 28, 2021
167f325
* LightSmsTransport.php - logic error
Mar 28, 2021
6792535
* composer.json - requirements bug fix
Mar 28, 2021
2f65b92
* LightSmsTransport.php - Coding Standard
Mar 28, 2021
15686c0
* LightSmsTransport.php - Coding Standard
Mar 28, 2021
a0fae7d
* tests
Mar 28, 2021
d1ccd46
* Attached file changes which are required to run the lightsms notifier
Mar 28, 2021
ce41756
* notifier_transports.php - Coding Standard
Mar 28, 2021
728a3e2
* Transport.php - missing use
Mar 28, 2021
4a11b94
* github account author
Mar 28, 2021
febff46
Update src/Symfony/Component/Notifier/Bridge/LightSms/LICENSE
StaffNowa Mar 28, 2021
9e1809e
* small changes
Mar 28, 2021
3cbbc85
* HOST split into two parts
Mar 28, 2021
5e54dfe
* LightSmsTransport.php - quick fix for private constant.
Mar 28, 2021
9f89014
Update src/Symfony/Component/Notifier/Bridge/LightSms/README.md
StaffNowa Mar 28, 2021
c02dbbd
Update src/Symfony/Component/Notifier/Bridge/LightSms/README.md
StaffNowa Mar 28, 2021
b2e4638
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa Mar 28, 2021
49b4780
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa Mar 28, 2021
b0891be
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa Mar 28, 2021
1b073c2
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa Mar 28, 2021
e0a68bd
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa Mar 28, 2021
3d0d79c
Update src/Symfony/Component/Notifier/Bridge/LightSms/Tests/LightSmsT…
StaffNowa Mar 28, 2021
0e41bc9
Update src/Symfony/Component/Notifier/Bridge/LightSms/Tests/LightSmsT…
StaffNowa Mar 28, 2021
079406e
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa Mar 28, 2021
7b51e0d
Update src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTranspo…
StaffNowa Mar 28, 2021
2e0e1d7
Update README.md
StaffNowa Mar 28, 2021
66c34ba
Update README.md
StaffNowa Mar 28, 2021
8620e82
* LightSmsTransport.php - move timestamp
Mar 29, 2021
5d2e692
* LightSmsTransport.php - escape phone number
Mar 29, 2021
fc13bb2
* LightSmsTransport.php - changed login for validation (the same like…
Mar 29, 2021
b0e64b9
* LightSmsTransport.php - not ok throw exception
Mar 29, 2021
e20ef1e
* LightSmsTransport.php - change + to 00
Mar 29, 2021
7f13dbf
* sender changed to from
Mar 29, 2021
f16b4d2
* phone changed to from
Mar 29, 2021
7180c1f
Update LightSmsTransport.php
StaffNowa Mar 29, 2021
08b0729
Update LightSmsTransport.php
StaffNowa Mar 29, 2021
80ef5ba
* LightSmsTransport.php - Unable to send the SMS: Closing direction t…
Mar 30, 2021
08235e5
* LightSmsTransport.php - bug fix
Mar 30, 2021
23a446a
* LightSmsTransport.php - issue with Symfony\Component\Notifier\Bridg…
Mar 30, 2021
265f776
* LightSmsTransport.php - issue with Symfony\Component\Notifier\Bridg…
Mar 30, 2021
b9f9ff8
* LightSmsTransport.php - tests fail
Mar 30, 2021
58ac708
* LightSmsTransport.php - return back www (without will not work). No…
Mar 30, 2021
1b59a7d
* LightSmsTransport.php - string param
Apr 1, 2021
1c993b7
* ERROR_CODES -> int
Apr 1, 2021
1ff97e4
* LightSmsTransport.php - build signature and use http_build_query, t…
Apr 1, 2021
83d2598
* Coding Standard patch
Apr 1, 2021
a197dee
* LightSmsTransport.php - better to remove if we do not have it?
Apr 1, 2021
95e82f6
Update LightSmsTransport.php
StaffNowa Apr 2, 2021
178d9c2
* pull request #40696
StaffNowa Apr 4, 2021
9a832ef
* LightSmsTransport.php - via mistake removed www which return (Closi…
StaffNowa Apr 4, 2021
0d7488b
* type cast. On success lightsms return error code like string. On er…
StaffNowa Apr 4, 2021
9b2e2d0
* type cast. On success lightsms return error code like string. On er…
StaffNowa Apr 4, 2021
bea5256
* type cast. On success lightsms return error code like string. On er…
StaffNowa Apr 4, 2021
21e972a
* coding standard
StaffNowa Apr 4, 2021
4213564
* composer.json - fix Fabien comment from another pull request #40646
StaffNowa Apr 5, 2021
2a9ac2d
* LightSmsTransport.php - fix
Apr 6, 2021
026dcd9
* LightSmsTransport.php - isset
Apr 6, 2021
f1f83b9
* LightSmsTransport.php - use query string parameters
Apr 6, 2021
68a12fa
* fix tests
Apr 6, 2021
37c665e
* LightSmsTransport.php - make fabbot happy
Apr 6, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory;
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
Expand Down Expand Up @@ -159,5 +160,9 @@
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')
->tag('texter.transport_factory')

->set('notifier.transport_factory.lightsms', LightSmsTransportFactory::class)
StaffNowa marked this conversation as resolved.
Show resolved Hide resolved
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')
;
};
4 changes: 4 additions & 0 deletions 4 src/Symfony/Component/Notifier/Bridge/LightSms/.gitattributes
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
3 changes: 3 additions & 0 deletions 3 src/Symfony/Component/Notifier/Bridge/LightSms/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
19 changes: 19 additions & 0 deletions 19 src/Symfony/Component/Notifier/Bridge/LightSms/LICENSE
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
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];
}
}
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'];
}
}
26 changes: 26 additions & 0 deletions 26 src/Symfony/Component/Notifier/Bridge/LightSms/README.md
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)
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
}
}
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)];
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.