|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Proklung\Notifier\Bitrix\Sender; |
| 4 | + |
| 5 | +use Bitrix\Main\ArgumentException; |
| 6 | +use Bitrix\Main\ObjectPropertyException; |
| 7 | +use Bitrix\Main\SystemException; |
| 8 | +use Proklung\Notifier\Bitrix\EventBridgeMail; |
| 9 | +use Proklung\Notifier\Bitrix\Utils\EventTableUpdater; |
| 10 | +use Symfony\Component\Notifier\Notification\Notification; |
| 11 | +use Symfony\Component\Notifier\NotifierInterface; |
| 12 | +use Symfony\Component\Notifier\Recipient\Recipient; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class BitrixPolicySender |
| 16 | + * @package Proklung\Notifier\Bitrix\Sender |
| 17 | + * |
| 18 | + * @since 29.07.2021 |
| 19 | + */ |
| 20 | +class BitrixPolicySender |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var EventBridgeMail $eventBridge Обработка битриксовых данных события. |
| 24 | + */ |
| 25 | + private $eventBridge; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var NotifierInterface $notifier Notifier. |
| 29 | + */ |
| 30 | + private $notifier; |
| 31 | + |
| 32 | + /** |
| 33 | + * BitrixMailEventSender constructor. |
| 34 | + * |
| 35 | + * @param EventBridgeMail $eventBridge Обработка битриксовых данных события. |
| 36 | + * @param NotifierInterface $notifier Notifier. |
| 37 | + */ |
| 38 | + public function __construct(EventBridgeMail $eventBridge, NotifierInterface $notifier) |
| 39 | + { |
| 40 | + $this->eventBridge = $eventBridge; |
| 41 | + $this->notifier = $notifier; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Статический фасад. |
| 46 | + * |
| 47 | + * @param NotifierInterface $notifier Notifier. |
| 48 | + * |
| 49 | + * @return static |
| 50 | + */ |
| 51 | + public static function getInstance(NotifierInterface $notifier) : self |
| 52 | + { |
| 53 | + return new static(new EventBridgeMail(), $notifier); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Отправить сообщение. |
| 58 | + * |
| 59 | + * @param string $codeEvent Код события. |
| 60 | + * @param array $arFields Параметры события. |
| 61 | + * @param string $importance Важность сообщения (в понимании Notifier). |
| 62 | + * |
| 63 | + * @return void |
| 64 | + * @throws ArgumentException | ObjectPropertyException | SystemException Битриксовые ошибки. |
| 65 | + */ |
| 66 | + public function send(string $codeEvent, array $arFields, string $importance) : void |
| 67 | + { |
| 68 | + $eventsInfo = $this->eventBridge->getMessageTemplate($codeEvent); |
| 69 | + foreach ($eventsInfo as $eventInfo) { |
| 70 | + $compileData = $this->eventBridge->compileMessage($eventInfo, $arFields, ['s1']); |
| 71 | + |
| 72 | + $notification = (new Notification($compileData['subject'])) |
| 73 | + ->content($compileData['body']) |
| 74 | + ->importance($importance); |
| 75 | + |
| 76 | + $recipient = new Recipient($compileData['mail_to']); |
| 77 | + |
| 78 | + $this->notifier->send($notification, $recipient); |
| 79 | + |
| 80 | + // Эмуляция поведения Битрикса при обработке событий. |
| 81 | + EventTableUpdater::create($eventInfo->getEventCode(), $eventInfo->getMessageData(), 99999); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments