|
| 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\Smsc; |
| 13 | + |
| 14 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 15 | +use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; |
| 16 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 17 | +use Symfony\Component\Notifier\Message\SentMessage; |
| 18 | +use Symfony\Component\Notifier\Message\SmsMessage; |
| 19 | +use Symfony\Component\Notifier\Transport\AbstractTransport; |
| 20 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
| 21 | +use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; |
| 22 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 23 | + |
| 24 | +class SmscTransport extends AbstractTransport |
| 25 | +{ |
| 26 | + protected const HOST = 'smsc.ru'; |
| 27 | + |
| 28 | + private $login; |
| 29 | + private $password; |
| 30 | + private $from; |
| 31 | + |
| 32 | + public function __construct($username, $password, $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) |
| 33 | + { |
| 34 | + $this->login = $username; |
| 35 | + $this->password = $password; |
| 36 | + $this->from = $from; |
| 37 | + |
| 38 | + parent::__construct($client, $dispatcher); |
| 39 | + } |
| 40 | + |
| 41 | + public function __toString(): string |
| 42 | + { |
| 43 | + return sprintf('smsc://%s?from=%s', $this->getEndpoint(), (string) $this->from); |
| 44 | + } |
| 45 | + |
| 46 | + public function supports(MessageInterface $message): bool |
| 47 | + { |
| 48 | + return $message instanceof SmsMessage; |
| 49 | + } |
| 50 | + |
| 51 | + protected function doSend(MessageInterface $message): SentMessage |
| 52 | + { |
| 53 | + if (!$message instanceof SmsMessage) { |
| 54 | + throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); |
| 55 | + } |
| 56 | + |
| 57 | + $query = [ |
| 58 | + 'login' => $this->login, |
| 59 | + 'psw' => $this->password, |
| 60 | + 'sender' => (string) $this->from, |
| 61 | + 'phones' => $message->getPhone(), |
| 62 | + 'mes' => $message->getSubject(), |
| 63 | + 'fmt' => 3, // response as JSON |
| 64 | + 'charset' => 'utf-8', |
| 65 | + 'time' => '0-24', |
| 66 | + ]; |
| 67 | + |
| 68 | + $endpoint = sprintf('https://%s/sys/send.php', $this->getEndpoint()); |
| 69 | + $response = $this->client->request('GET', $endpoint, ['query' => $query]); |
| 70 | + |
| 71 | + try { |
| 72 | + $result = $response->toArray(); |
| 73 | + } catch (ExceptionInterface $e) { |
| 74 | + throw new TransportException('Unable to send the SMS.', $response, 0, $e); |
| 75 | + } |
| 76 | + |
| 77 | + if (\array_key_exists('error', $result)) { |
| 78 | + throw new TransportException(sprintf('Unable to send the SMS: code = %d, message = "%s".', $result['error_code'], $result['error']), $response); |
| 79 | + } |
| 80 | + |
| 81 | + $sentMessage = new SentMessage($message, (string) $this); |
| 82 | + $sentMessage->setMessageId((string) ($result['id'] ?? '')); |
| 83 | + |
| 84 | + return $sentMessage; |
| 85 | + } |
| 86 | +} |