|
| 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×tamp=%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 | +} |
0 commit comments