Skip to content

Commit dbb8c78

Browse files
committed
Битриксовые события и Notifier
1 parent e0c34b9 commit dbb8c78

File tree

11 files changed

+467
-12
lines changed

11 files changed

+467
-12
lines changed

DependencyInjection/BitrixOrdinaryToolsExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
99
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1010
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11+
use Symfony\Component\Notifier\NotifierInterface;
1112

1213
/**
1314
* Class BitrixOrdinaryToolsExtension
@@ -35,6 +36,11 @@ public function load(array $configs, ContainerBuilder $container) : void
3536
$loader->load('image_resizer.yaml');
3637
$loader->load('loggers.yaml');
3738

39+
$loader->load('notifier.yaml');
40+
if (class_exists(NotifierInterface::class)) {
41+
$loader->load('notifier_transports.yaml');
42+
}
43+
3844
if (!class_exists('Prokl\CustomFrameworkExtensionsBundle\DependencyInjection\CustomFrameworkExtensionsExtension')) {
3945
throw new LogicException(
4046
'Чтобы использовать Твиг нужно установить и активировать core.framework.extension.bundle.

Resources/config/notifier.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
autoconfigure: true
5+
public: true
6+
7+
Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeMail:
8+
class: Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeMail
9+
10+
Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeSms:
11+
class: Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeSms
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
autoconfigure: true
5+
public: true
6+
7+
# Отправка битриксового события через почту.
8+
notifier_bitrix_event_sender.mail:
9+
class: Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\Sender\BitrixMailEventSender
10+
arguments:
11+
- '@Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeMail'
12+
- '@notifier'
13+
14+
# Отправка битриксового события по sms.
15+
notifier_bitrix_event_sender.sms:
16+
class: Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\Sender\BitrixSmsSender
17+
arguments:
18+
- '@Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeSms'
19+
- '@texter'
20+
21+
# Отправка битриксового события в Телеграм.
22+
notifier_bitrix_event_sender.telegram:
23+
class: Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\Sender\BitrixTelegramEventSender
24+
arguments:
25+
- '@Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeMail'
26+
- '@chatter'

Resources/config/services.yaml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,4 @@ services:
102102
# Работа с почтовыми событиями.
103103
Prokl\BitrixOrdinaryToolsBundle\Services\Email\SendNotification:
104104
class: Prokl\BitrixOrdinaryToolsBundle\Services\Email\SendNotification
105-
arguments: ['@Bitrix\Main\Mail\Event', '@bitrix_ordinary_tools.cfile']
106-
107-
Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridge:
108-
class: Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridge
109-
110-
Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeSms:
111-
class: Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeSms
105+
arguments: ['@Bitrix\Main\Mail\Event', '@bitrix_ordinary_tools.cfile']

Services/Email/EventBridge/EventInfo.php renamed to Services/Email/EventBridge/DTO/EventInfo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
namespace Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge;
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\DTO;
44

55
/**
66
* Class EventInfo
77
* DTO с информацией о шаблоне битриксового события.
8-
* @package Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge
8+
* @package Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\DTO
99
*
1010
* @since 28.07.2021
1111
*/

Services/Email/EventBridge/EventBridge.php renamed to Services/Email/EventBridge/EventBridgeMail.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
use Bitrix\Main\Mail\Internal\EventMessageTable;
99
use Bitrix\Main\ObjectPropertyException;
1010
use Bitrix\Main\SystemException;
11+
use Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\DTO\EventInfo;
1112

1213
/**
13-
* Class EventBridge
14+
* Class EventBridgeMail
1415
* @package Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge
1516
*
1617
* @since 28.07.2021
1718
*/
18-
class EventBridge
19+
class EventBridgeMail
1920
{
2021
/**
2122
* Получить скомпилированный текст письма для события.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 Symfony\Component\Notifier\Notification\Notification;
11+
use Symfony\Component\Notifier\NotifierInterface;
12+
use Symfony\Component\Notifier\Recipient\Recipient;
13+
14+
/**
15+
* Class BitrixMailEventSender
16+
* @package Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\Sender
17+
*
18+
* @since 28.07.2021
19+
*/
20+
class BitrixMailEventSender
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|null $importance Важность сообщения (в понимании Notifier).
62+
*
63+
* @return void
64+
* @throws ArgumentException | ObjectPropertyException | SystemException Битриксовые ошибки.
65+
*/
66+
public function send(string $codeEvent, array $arFields, ?string $importance = null) : void
67+
{
68+
$eventsInfo = $this->eventBridge->getMessageTemplate($codeEvent);
69+
foreach ($eventsInfo as $eventInfo) {
70+
$compileData = $this->eventBridge->compileMessage($eventInfo, $arFields, ['s1']);
71+
72+
if ($importance !== null) {
73+
$notification = (new Notification($compileData['subject']))
74+
->content($compileData['body'])
75+
->importance($importance);
76+
} else {
77+
$notification = (new Notification($compileData['subject'], ['email']))
78+
->content($compileData['body']);
79+
}
80+
81+
$recipient = new Recipient($compileData['mail_to']);
82+
83+
$this->notifier->send($notification, $recipient);
84+
85+
// Эмуляция поведения Битрикса при обработке событий.
86+
EventTableUpdater::create($eventInfo->getEventCode(), $eventInfo->getMessageData(), 99999);
87+
}
88+
}
89+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 Exception;
9+
use Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\EventBridgeSms;
10+
use Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\Utils\EventTableUpdater;
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 Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge\Sender
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+
$fields = $success === 'N' ? ['error' => $errorMessage] : ['data' => $sentMessage->getMessageId()];
93+
94+
EventTableUpdater::create($codeEvent, $fields, 99999, $success);
95+
}
96+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)