|
| 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\SmsBiuras; |
| 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@prado.lt> |
| 26 | + */ |
| 27 | +final class SmsBiurasTransport extends AbstractTransport |
| 28 | +{ |
| 29 | + protected const HOST = 'savitarna.smsbiuras.lt'; |
| 30 | + |
| 31 | + private $uid; |
| 32 | + private $apiKey; |
| 33 | + private $from; |
| 34 | + private $testMode; |
| 35 | + |
| 36 | + private const ERROR_CODES = [ |
| 37 | + 1 => 'The message was processed and sent to the mobile operator. But delivery confirmations have not yet been returned.', |
| 38 | + 2 => 'SMS not delivered.', |
| 39 | + 3 => 'The SMS message was successfully delivered to the recipient.', |
| 40 | + 4 => 'The message was sent and expired because it could not be delivered to the recipient during its validity period (48 hours according to our default platform).', |
| 41 | + 5 => 'The message was received but the operator returned "Rejected" as the final status.', |
| 42 | + 6 => 'Missing parameters, check that you are using all required parameters.', |
| 43 | + 7 => ' Wrong apikey or uid.', |
| 44 | + 8 => 'Sender ID - "from". Must be approved by an administrator.', |
| 45 | + 9 => 'Balance insufficient, please top up the account.', |
| 46 | + 10 => 'Bad date format for schedule parameter. Ex: urlencode("2021-03-11 12:00").', |
| 47 | + 999 => 'Unknown Error', |
| 48 | + ]; |
| 49 | + |
| 50 | + public function __construct(string $uid, string $apiKey, string $from, bool $testMode, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) |
| 51 | + { |
| 52 | + $this->uid = $uid; |
| 53 | + $this->apiKey = $apiKey; |
| 54 | + $this->from = $from; |
| 55 | + $this->testMode = $testMode; |
| 56 | + |
| 57 | + parent::__construct($client, $dispatcher); |
| 58 | + } |
| 59 | + |
| 60 | + public function __toString(): string |
| 61 | + { |
| 62 | + if ($this->testMode) { |
| 63 | + return sprintf('smsbiuras://%s?from=%s&test_mode=%s', $this->getEndpoint(), $this->from, $this->testMode); |
| 64 | + } |
| 65 | + |
| 66 | + return sprintf('smsbiuras://%s?from=%s', $this->getEndpoint(), $this->from); |
| 67 | + } |
| 68 | + |
| 69 | + public function supports(MessageInterface $message): bool |
| 70 | + { |
| 71 | + return $message instanceof SmsMessage; |
| 72 | + } |
| 73 | + |
| 74 | + protected function doSend(MessageInterface $message): SentMessage |
| 75 | + { |
| 76 | + if (!$message instanceof SmsMessage) { |
| 77 | + throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); |
| 78 | + } |
| 79 | + |
| 80 | + $endpoint = sprintf('https://%s/api?', $this->getEndpoint()); |
| 81 | + |
| 82 | + $response = $this->client->request('GET', $endpoint, [ |
| 83 | + 'query' => [ |
| 84 | + 'uid' => $this->uid, |
| 85 | + 'apikey' => $this->apiKey, |
| 86 | + 'message' => $message->getSubject(), |
| 87 | + 'from' => $this->from, |
| 88 | + 'test' => $this->testMode ? 0 : 1, |
| 89 | + 'to' => $message->getPhone(), |
| 90 | + ], |
| 91 | + ]); |
| 92 | + |
| 93 | + if (Response::HTTP_OK !== $response->getStatusCode()) { |
| 94 | + throw new TransportException('Unable to send the SMS.', $response); |
| 95 | + } |
| 96 | + |
| 97 | + $matches = []; |
| 98 | + if (preg_match('/^ERROR: (\d+)$/', $response->getContent(), $matches)) { |
| 99 | + throw new TransportException('Unable to send the SMS: '.$this->getErrorMsg($matches[1] ?? 999), $response); |
| 100 | + } |
| 101 | + |
| 102 | + $matches = []; |
| 103 | + if (preg_match('/^OK: (\d+)$/', $response->getContent(), $matches)) { |
| 104 | + $sentMessage = new SentMessage($message, (string) $this); |
| 105 | + $sentMessage->setMessageId($matches[1] ?? 0); |
| 106 | + |
| 107 | + return $sentMessage; |
| 108 | + } |
| 109 | + |
| 110 | + throw new TransportException('Unable to send the SMS.', $response); |
| 111 | + } |
| 112 | + |
| 113 | + private function getErrorMsg(int $errorCode): string |
| 114 | + { |
| 115 | + return self::ERROR_CODES[$errorCode] ?? self::ERROR_CODES[999]; |
| 116 | + } |
| 117 | +} |
0 commit comments