|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Proklung\Notifier\Bitrix; |
| 4 | + |
| 5 | +use Bitrix\Main\ArgumentException; |
| 6 | +use Bitrix\Main\Mail\Internal\EventTable; |
| 7 | +use Bitrix\Main\ObjectPropertyException; |
| 8 | +use Bitrix\Main\SystemException; |
| 9 | +use Bitrix\Main\Type\DateTime; |
| 10 | +use Exception; |
| 11 | +use RuntimeException; |
| 12 | +use Symfony\Component\Notifier\Exception\TransportExceptionInterface; |
| 13 | +use Symfony\Component\Notifier\Message\SmsMessage; |
| 14 | +use Symfony\Component\Notifier\TexterInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class BitrixSmsSender |
| 18 | + * @package Proklung\Notifier\Bitrix |
| 19 | + * |
| 20 | + * @since 28.07.2021 |
| 21 | + */ |
| 22 | +class BitrixSmsSender |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var EventBridgeSms $eventBridge Обработка битриксовых данных события SMS. |
| 26 | + */ |
| 27 | + private $eventBridge; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var TexterInterface $notifier Notifier. |
| 31 | + */ |
| 32 | + private $notifier; |
| 33 | + |
| 34 | + /** |
| 35 | + * BitrixSmsSender constructor. |
| 36 | + * |
| 37 | + * @param EventBridgeSms $eventBridge Обработка битриксовых данных события. |
| 38 | + * @param TexterInterface $notifier Notifier. |
| 39 | + */ |
| 40 | + public function __construct(EventBridgeSms $eventBridge, TexterInterface $notifier) |
| 41 | + { |
| 42 | + $this->eventBridge = $eventBridge; |
| 43 | + $this->notifier = $notifier; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Статический фасад. |
| 48 | + * |
| 49 | + * @param TexterInterface $notifier Notifier. |
| 50 | + * |
| 51 | + * @return static |
| 52 | + */ |
| 53 | + public static function getInstance(TexterInterface $notifier): self |
| 54 | + { |
| 55 | + return new static(new EventBridgeSms(), $notifier); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Отправить сообщение. |
| 60 | + * |
| 61 | + * @param string $codeEvent Код события. |
| 62 | + * @param array $arFields Параметры события. |
| 63 | + * |
| 64 | + * @return void |
| 65 | + * @throws ArgumentException | ObjectPropertyException | SystemException Битриксовые ошибки. |
| 66 | + * @throws TransportExceptionInterface Ошибки транспорта SMS. |
| 67 | + */ |
| 68 | + public function send(string $codeEvent, array $arFields): void |
| 69 | + { |
| 70 | + $template = $this->eventBridge->fetchTemplates($codeEvent, 's1'); |
| 71 | + if ($template === null) { |
| 72 | + throw new RuntimeException('Template SMS object cannot be NULL'); |
| 73 | + } |
| 74 | + |
| 75 | + $message = $this->eventBridge->compileMessage($template, $arFields); |
| 76 | + |
| 77 | + $sms = new SmsMessage( |
| 78 | + $message->getReceiver(), |
| 79 | + $message->getText() |
| 80 | + ); |
| 81 | + |
| 82 | + $success = 'Y'; |
| 83 | + $errorMessage = ''; |
| 84 | + try { |
| 85 | + $sentMessage = $this->notifier->send($sms); |
| 86 | + } catch (Exception $e) { |
| 87 | + $success = 'N'; |
| 88 | + $errorMessage = $e->getMessage(); |
| 89 | + } |
| 90 | + |
| 91 | + // Эмуляция поведения Битрикса при обработке событий. |
| 92 | + try { |
| 93 | + EventTable::add( |
| 94 | + [ |
| 95 | + 'EVENT_NAME' => $codeEvent, |
| 96 | + 'SUCCESS_EXEC' => $success, |
| 97 | + 'MESSAGE_ID' => 99999, // Признак, что отправлено через Notifier |
| 98 | + 'DUPLICATE' => 'N', |
| 99 | + 'LID' => SITE_ID, |
| 100 | + 'LANGUAGE_ID' => LANGUAGE_ID, |
| 101 | + 'DATE_INSERT' => new DateTime, |
| 102 | + 'DATE_EXEC' => new DateTime, |
| 103 | + 'C_FIELDS' => $success === 'N' ? ['error' => $errorMessage] : ['data' => $sentMessage->getMessageId()], |
| 104 | + ] |
| 105 | + ); |
| 106 | + } catch (Exception $e) { |
| 107 | + // Silence. Не самый важный момент. |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments