|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\Sender; |
| 4 | + |
| 5 | +use Bitrix\Main\ArgumentException; |
| 6 | +use Bitrix\Main\ObjectPropertyException; |
| 7 | +use Bitrix\Main\SystemException; |
| 8 | +use Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeMail; |
| 9 | +use Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\Utils\EventTableUpdater; |
| 10 | +use RuntimeException; |
| 11 | +use Symfony\Component\Notifier\ChatterInterface; |
| 12 | +use Symfony\Component\Notifier\Exception\TransportExceptionInterface; |
| 13 | +use Symfony\Component\Notifier\Message\ChatMessage; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class BitrixTelegramEventSender |
| 17 | + * @package Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\Sender |
| 18 | + * |
| 19 | + * @since 28.07.2021 |
| 20 | + */ |
| 21 | +class BitrixTelegramEventSender |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var EventBridgeMail $eventBridge Обработка битриксовых данных события. |
| 25 | + */ |
| 26 | + private $eventBridge; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ChatterInterface $notifier Notifier. |
| 30 | + */ |
| 31 | + private $notifier; |
| 32 | + |
| 33 | + /** |
| 34 | + * BitrixChatEventSender constructor. |
| 35 | + * |
| 36 | + * @param EventBridgeMail $eventBridge Обработка битриксовых данных события. |
| 37 | + * @param ChatterInterface $notifier Notifier. |
| 38 | + */ |
| 39 | + public function __construct(EventBridgeMail $eventBridge, ChatterInterface $notifier) |
| 40 | + { |
| 41 | + $this->eventBridge = $eventBridge; |
| 42 | + $this->notifier = $notifier; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Статический фасад. |
| 47 | + * |
| 48 | + * @param ChatterInterface $notifier Notifier. |
| 49 | + * |
| 50 | + * @return static |
| 51 | + * @throws RuntimeException Когда пакет symfony/telegram-notifier не установлен. |
| 52 | + */ |
| 53 | + public static function getInstance(ChatterInterface $notifier) : self |
| 54 | + { |
| 55 | + if (!class_exists(TelegramTransport::class)) { |
| 56 | + throw new RuntimeException( |
| 57 | + sprintf( |
| 58 | + 'Unable to send notification via "%s" as the bridge is not installed; try running "composer require %s".', |
| 59 | + 'telegram', |
| 60 | + 'symfony/telegram-notifier' |
| 61 | + ) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + return new static(new EventBridgeMail(), $notifier); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Отправить сообщение. |
| 70 | + * |
| 71 | + * @param string $codeEvent Код события. |
| 72 | + * @param array $arFields Параметры события. |
| 73 | + * |
| 74 | + * @return void |
| 75 | + * @throws ArgumentException | ObjectPropertyException | SystemException Битриксовые ошибки. |
| 76 | + * @throws TransportExceptionInterface Ошибки транспорта. |
| 77 | + */ |
| 78 | + public function send(string $codeEvent, array $arFields) : void |
| 79 | + { |
| 80 | + $eventsInfo = $this->eventBridge->getMessageTemplate($codeEvent); |
| 81 | + foreach ($eventsInfo as $eventInfo) { |
| 82 | + $compileData = $this->eventBridge->compileMessage($eventInfo, $arFields, ['s1']); |
| 83 | + |
| 84 | + $notification = (new ChatMessage($compileData['subject'] . ' ' . $compileData['body'])) |
| 85 | + ->transport('telegram'); |
| 86 | + |
| 87 | + $telegramOptions = (new TelegramOptions()) |
| 88 | + ->parseMode('Markdown') |
| 89 | + ->disableWebPagePreview(true) |
| 90 | + ->disableNotification(false); |
| 91 | + |
| 92 | + $notification->options($telegramOptions); |
| 93 | + |
| 94 | + $this->notifier->send($notification); |
| 95 | + |
| 96 | + // Эмуляция поведения Битрикса при обработке событий. |
| 97 | + EventTableUpdater::create($eventInfo->getEventCode(), $eventInfo->getMessageData(), 99999); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments