* LightSms notifier

This commit is contained in:
Vasilij Dusko 2021-03-28 12:28:03 +03:00 committed by Vasilij Dusko | CREATION
parent 40a2c19ae7
commit b075c0eae2
6 changed files with 310 additions and 0 deletions

View File

@ -0,0 +1,19 @@
Copyright (c) 2020-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.

View File

@ -0,0 +1,153 @@
<?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@d4d.lt>
*/
final class LightSmsTransport extends AbstractTransport
{
protected const HOST = 'www.lightsms.com/external/get/send.php';
private $login;
private $password;
private $phone;
/**
* @var MessageInterface
*/
private $message;
private $errorCodes = [
'000' => 'Service unavailable',
'1' => 'Signature not specified',
'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' => 'Month not specifies (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' => 'Date of start not specified (Format: YYYY-MM-DD)',
'29' => 'Date of end not specified (Format: YYYY-MM-DD)',
'30' => 'No date (format: YYYY-MM-DD)',
'31' => 'Closing direction to the user',
'32' => 'Not enough money',
'33' => 'Phone number is not set',
'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 is already exist in this base',
'39' => 'Phone number is not exist in this base',
];
public function __construct(
string $login,
string $password,
string $phone,
HttpClientInterface $client = null,
EventDispatcherInterface $dispatcher = null
) {
$this->login = $login;
$this->password = $password;
$this->phone = $phone;
parent::__construct($client, $dispatcher);
}
public function __toString(): string
{
return sprintf('lightsms://%s?phone=%s', $this->getEndpoint(), $this->phone);
}
public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage && $this->phone === str_replace('+', '', $message->getPhone());
}
protected function doSend(MessageInterface $message): void
{
if (!$message instanceof SmsMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, \get_class($message)));
}
$this->message = $message;
$signature = $this->getSignature();
$endpoint = sprintf(
'https://%s?login=%s&signature=%s&phone=%s&text=%s&sender=%s&timestamp=%s',
$this->getEndpoint(),
$this->login,
$signature,
str_replace('+', '', $message->getPhone()),
$message->getSubject(),
$this->phone,
time()
);
$response = $this->client->request('GET', $endpoint);
$content = $response->toArray(false);
if (isset($content['error'])) {
throw new TransportException('Unable to send the SMS: '.$this->errorCodes[$content['error']], $response);
}
}
private function getSignature(): string
{
$params = [
'timestamp' => time(),
'login' => $this->login,
'phone' => str_replace('+', '', $this->message->getPhone()),
'sender' => $this->phone,
'text' => $this->message->getSubject(),
];
ksort($params);
reset($params);
return md5(implode($params) . $this->password);
}
}

View File

@ -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@d4d.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);
$phone = $dsn->getOption('phone');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();
return (new LightSmsTransport($login, $token, $phone, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}
protected function getSupportedSchemes(): array
{
return ['lightsms'];
}
}

View File

@ -0,0 +1,28 @@
LightSms Notifier
====================
Provides [LightSms](https://www.lightsms.com/) integration for Symfony Notifier.
This provider allows you to receive an SMS notification on your mobile number.
DSN example
-----------
```
LIGHTSMS_DSN=lightsms://LOGIN:TOKEN@default?phone=PHONE
```
where:
- `LOGIN` is your LightSms login
- `TOKEN` is the token displayed in your account
- `PHONE` is your LightSms 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)

View File

@ -0,0 +1,30 @@
{
"name": "symfony/lightsms-notifier",
"type": "symfony-bridge",
"description": "Symfony LightSms Notifier Bridge",
"keywords": ["sms", "LightSms", "notifier"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Vasilij Duško",
"email": "vasilij@d4d.lt"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": ">=7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "~5.0.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\LightSms\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev"
}

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>
<testsuites>
<testsuite name="Symfony LightSms Notifier Bridge Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>