Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Sender.php
1<?php
2
4
11
12abstract class Sender extends Providers\Base\Sender
13{
14
17 protected EdnaRu $utils;
19
20 abstract protected function initializeDefaultExternalSender(): Providers\ExternalSender;
21 abstract protected function getSendMessageParams(array $messageFields): Result;
22 abstract protected function getSendMessageMethod(array $messageFields): string;
23 abstract protected function isTemplateMessage(array $messageFields): bool;
24 abstract protected function sendHSMtoChat(array $messageFields): Result;
25
30 public function __construct(
31 Providers\OptionManager $optionManager,
32 Providers\SupportChecker $supportChecker,
33 EdnaRu $utils
34 )
35 {
36 $this->optionManager = $optionManager;
37 $this->supportChecker = $supportChecker;
38 $this->utils = $utils;
39 $this->externalSender = $this->initializeDefaultExternalSender();
40 }
41
42
43 public function sendMessage(array $messageFields): SendMessage
44 {
45 if (!$this->supportChecker->canUse())
46 {
47 $result = new SendMessage();
48 $result->addError(new Error('Service is unavailable'));
49
50 return $result;
51 }
52
53 $paramsResult = $this->getSendMessageParams($messageFields);
54 if (!$paramsResult->isSuccess())
55 {
56 $providerId = $this->optionManager->getProviderId();
57
58 $cacheManager = new Providers\CacheManager($providerId);
59 $cacheManager->deleteValue(Providers\CacheManager::CHANNEL_CACHE_ENTITY_ID);
60
61 $sender = MessageService\Sender\SmsManager::getSenderById($providerId);
62 \Bitrix\Main\Application::getInstance()->addBackgroundJob([$sender, 'refreshFromList']);
63
64 $result = new SendMessage();
65 $result->addErrors($paramsResult->getErrors());
66
67 return $result;
68 }
69
70 $requestParams = $paramsResult->getData();
71 $method = $this->getSendMessageMethod($messageFields);
72
73 if ($this->isTemplateMessage($messageFields))
74 {
75 $this->sendHSMtoChat($messageFields);
76 }
77
78 $result = new SendMessage();
79
80 $requestResult = $this->externalSender->callExternalMethod($method, $requestParams);
81 if (!$requestResult->isSuccess())
82 {
83 $result->addErrors($requestResult->getErrors());
84
85 return $result;
86 }
87
88 $apiData = $requestResult->getData();
89 $result->setExternalId($apiData['requestId']);
90 $result->setAccepted();
91
92 return $result;
93 }
94
95 public function getMessageStatus(array $messageFields): MessageStatus
96 {
97 return new MessageStatus();
98 }
99}
Providers OptionManager $optionManager
Definition Sender.php:15
__construct(Providers\OptionManager $optionManager, Providers\SupportChecker $supportChecker, EdnaRu $utils)
Definition Sender.php:30
getMessageStatus(array $messageFields)
Definition Sender.php:95
Providers ExternalSender $externalSender
Definition Sender.php:18
Providers SupportChecker $supportChecker
Definition Sender.php:16