Skip to content

Commit f378da4

Browse files
committed
Сопряжение битриксовых событий и транспорта email.
1 parent 8667fae commit f378da4

File tree

3 files changed

+101
-5
lines changed

3 files changed

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

lib/Bitrix/EventBridge.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class EventBridge
2424
* @param array $context Значения полей ($arFields).
2525
* @param array $sites ID сайтов.
2626
*
27-
* @return mixed
27+
* @return array
2828
*/
29-
public function compileMessage(EventInfo $eventInfo, array $context, array $sites = [])
29+
public function compileMessage(EventInfo $eventInfo, array $context, array $sites = []) : array
3030
{
3131
$message = EventMessageCompiler::createInstance([
3232
'EVENT' => $eventInfo->getEventCode(),
@@ -37,7 +37,11 @@ public function compileMessage(EventInfo $eventInfo, array $context, array $site
3737
]);
3838
$message->compile();
3939

40-
return $message->getMailBody();
40+
return ['body' => $message->getMailBody(),
41+
'mail_to' => $message->getMailTo(),
42+
'charset' => $message->getMailCharset(),
43+
'subject' => $message->getMailSubject(),
44+
];
4145
}
4246

4347
/**
@@ -48,7 +52,7 @@ public function compileMessage(EventInfo $eventInfo, array $context, array $site
4852
*
4953
* @throws ArgumentException | ObjectPropertyException | SystemException Битриксовые ошибки.
5054
*/
51-
public function getMessageTemplate(string $codeEvent, array $sites = [])
55+
public function getMessageTemplate(string $codeEvent, array $sites = []) : array
5256
{
5357
$arSites = ['s1'];
5458
if (count($sites) > 0) {

readme.MD

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,25 @@ return [
179179

180180
## Прочее
181181

182-
#### FlashBag
182+
### FlashBag
183183

184184
Для обработки отправок в канал `browser`. Два варианта:
185185

186186
- публичный сервис `session_instance` вернет экземпляр сессии, а там есть метод `getFlashBag`.
187187
- хэлпер `\Proklung\Notifier\DI\Services::getFlashBag()`, делающий тоже самое.
188+
189+
### Использование битриксовых почтовых событий и их шаблонов
190+
191+
```php
192+
use Proklung\Notifier\Bitrix\BitrixMailEventSender;
193+
194+
$bitrixEventHandler = BitrixMailEventSender::getInstance(
195+
\Proklung\Notifier\DI\Services::getInstance()->get('notifier')
196+
);
197+
198+
// Массив, идентичный с параметром fields при отправке Битриксом сообщений
199+
// См. https://dev.1c-bitrix.ru/api_help/main/reference/cevent/send.php
200+
$arFields = ['NAME' => 'testing email', 'EMAIL' => 'recipitient@gmail.com'];
201+
202+
$bitrixEventHandler->send('CODE_MAIL_EVENT', $arFields);
203+
```

0 commit comments

Comments
 (0)