Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sendingemailnotification.php
1<?php
2
4
20use TypeError;
21
23{
24 private const MAX_ATTEMPTS_INVITATION = 3;
25 private const LOG_MARKER = 'DEBUG_CALENDAR_EMAIL_NOTIFICATION';
26
27 private Logger $logger;
28
29 public function __construct()
30 {
31 $this->logger = new Logger(self::LOG_MARKER);
32 }
33
41 public function process(Interfaces\Message $message): string
42 {
43 $serializedNotificationData = $this->getSerializedNotificationData($message);
44 if (is_null($serializedNotificationData))
45 {
46 return self::REJECT;
47 }
48
49 try
50 {
51 $notificationData = $this->unserializeNotificationData($serializedNotificationData);
52 }
53 catch (TypeError $exception)
54 {
55 $this->logger->log($exception);
56
57 return self::REJECT;
58 }
59
60 if (!$this->isNotificationFieldsCorrect($notificationData))
61 {
62 $this->logger->log($serializedNotificationData);
63
64 return self::REJECT;
65 }
66
67 $invitation = new InvitationInfo(
68 $notificationData['eventId'],
69 $notificationData['addresserId'],
70 $notificationData['receiverId'],
71 $notificationData['type'],
72 $notificationData['changeFields'] ?? [],
73 $notificationData['counterInvitation'] + 1,
74 );
75
76 $notification = $invitation->getSenderInvitation();
77 if (is_null($notification))
78 {
79 $this->logger->log($serializedNotificationData);
80
81 return self::REJECT;
82 }
83
84 $this->setLanguageId();
85 if ($notification->send())
86 {
87 $notification->executeAfterSuccessfulInvitation();
88
89 return self::ACK;
90 }
91
92 if ($notification->getCountAttempsSend() < self::MAX_ATTEMPTS_INVITATION)
93 {
94 self::sendMessageToQueue($invitation->toArray());
95
96 return self::ACK;
97 }
98
99 $failSent = [];
100 $failSent[$notification->getEventParentId()] = $this->getDataForNotify($notification);
101 $this->sendFailSendNotify($failSent);
102
103 return self::REJECT;
104 }
105
106 private function getSerializedNotificationData(Interfaces\Message $message): ?string
107 {
108 $messageBody = $message->getBody();
109 if (!is_array($messageBody) || !isset($messageBody['requestInvitation']))
110 {
111 return null;
112 }
113
114 return $messageBody['requestInvitation'];
115 }
116
117 private function isNotificationFieldsCorrect (mixed $notification): bool
118 {
119 if (!is_array($notification))
120 {
121 return false;
122 }
123
124 return (
125 isset($notification['eventId'])
126 || isset($notification['addresserId'])
127 || isset($notification['receiverId'])
128 || isset($notification['type'])
129 || isset($notification['counterInvitation'])
130 );
131 }
132
133 private function unserializeNotificationData(string $serializeNotificationData): mixed
134 {
135 $notification = str_replace("\'", "'", $serializeNotificationData);
136 $notification = Emoji::decode($notification);
137
138 return unserialize($notification, ['allowed_classes' => false]);
139 }
140
141 private function sendFailSendNotify(array $failSent): void
142 {
143 foreach ($failSent as $parentId => $item)
144 {
145 if (isset($item[0]))
146 {
147 $item = $item[0];
148 }
149 \CCalendarNotify::Send([
150 'mode' => 'fail_ical_invite',
151 'eventId' => $parentId,
152 'userId' => $item['userId'],
153 'guestId' => $item['userId'],
154 'items' => $item,
155 'name' => $item['name'],
156 'icalMethod' => $item['method'],
157 ]);
158 }
159 }
160
161 private function getDataForNotify(SenderInvitation $sender): array
162 {
163 $event = $sender->getEvent();
164 return [
165 'email' => $sender->getReceiver()->getEmail(),
166 'eventId' => $event['PARENT_ID'],
167 'name' => $event['NAME'],
168 'userId' => $event['MEETING_HOST'],
169 'method' => $sender->getMethod(),
170 ];
171 }
172
178 private function setLanguageId(): void
179 {
180 $siteDb = SiteTable::getById(SITE_ID);
181 if ($site = $siteDb->fetchObject())
182 {
183 Loc::setCurrentLang($site->getLanguageId());
184 }
185 }
186
192 public static function sendMessageToQueue(array $invitation): void
193 {
194 $serializedData = str_replace("'", "\'", serialize($invitation));
195 $message = (new Message())
196 ->setBody(['requestInvitation' => $serializedData])
197 ->setRoutingKey('calendar:sending_email_notification')
198 ;
199
200 (new Producer())->send($message);
201 }
202
209 public static function sendBatchOfMessagesToQueue(array $invitations): void
210 {
211 $messages = [];
212
213 if (!is_iterable($invitations))
214 {
215 AddMessage2Log('Ical senders collection is not iterable', 'calendar', 4);
216 return;
217 }
218
219 foreach ($invitations as $invitation)
220 {
221 $serializedData = str_replace("'", "\'", serialize($invitation));
222 $messages[] = (new Message())
223 ->setBody(['requestInvitation' => $serializedData])
224 ->setRoutingKey('calendar:sending_email_notification')
225 ;
226 }
227
228 (new Producer())->sendBatch($messages);
229 }
230}
static setCurrentLang($language)
Definition loc.php:95