Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
smsednaru.php
1<?php
2
4
12
14{
15 use Sender\Traits\RussianProvider;
16
18
19 public const ID = 'smsednaru';
20
21 public function __construct()
22 {
23 $this->informant = new SMS\Informant();
24 $this->optionManager = new Option($this->getType(), $this->getId());
25 $this->utils = new SMS\Utils($this->getId(), $this->optionManager);
26
27 if (!$this->isMigratedToNewAPI())
28 {
29 $this->initializeOldApiComponents();
30
31 return;
32 }
33 $this->initializeNewApiComponents();
34
35 $this->migrateToNewApi();
36 }
37
38 public function getId(): string
39 {
40 return $this->informant->getId();
41 }
42
43 public function getName(): string
44 {
45 return $this->informant->getName();
46 }
47
48 public function getShortName(): string
49 {
50 return $this->informant->getShortName();
51 }
52
53 public function getType(): string
54 {
55 return $this->informant->getType();
56 }
57
58 public function getManageUrl(): string
59 {
60 return $this->informant->getManageUrl();
61 }
62
63 public function getExternalId(): string
64 {
65 return $this->informant->getExternalId();
66 }
67
68 public function isRegistered(): bool
69 {
70 return $this->registrar->isRegistered();
71 }
72
73 public function register(array $fields): Result
74 {
75 $result = $this->registrar->register($fields);
76 if ($result->isSuccess())
77 {
78 \Bitrix\Main\Application::getInstance()->addBackgroundJob([$this, 'refreshFromList']);
79 \Bitrix\Main\Application::getInstance()->addBackgroundJob([$this, 'addRefreshFromListAgent']);
80 }
81 return $result;
82 }
83
84 public function getOwnerInfo(): array
85 {
86 return $this->registrar->getOwnerInfo();
87 }
88
89 public function getExternalManageUrl(): string
90 {
91 return $this->registrar->getExternalManageUrl();
92 }
93
94 public function getCallbackUrl(): string
95 {
96 return $this->registrar->getCallbackUrl();
97 }
98
99 public function isConfirmed(): bool
100 {
101 return $this->registrar->isConfirmed();
102 }
103
104 public function confirmRegistration(array $fields): Result
105 {
106 return $this->registrar->confirmRegistration($fields);
107 }
108
109 public function sendConfirmationCode(): Result
110 {
111 return $this->registrar->sendConfirmationCode();
112 }
113
117 public function getFromList(): array
118 {
119 return $this->initiator->getFromList();
120 }
121
126 public function refreshFromList(): void
127 {
128 $this->utils->updateSavedChannelList($this->initiator->getChannelType());
129 }
130
131 public function isCorrectFrom($from): bool
132 {
133 return $this->initiator->isCorrectFrom($from);
134 }
135
136 public function getDefaultFrom(): ?string
137 {
138 return $this->initiator->getDefaultFrom();
139 }
140
141 public function getFirstFromList()
142 {
143 return $this->initiator->getDefaultFrom();
144 }
145
146 public function getMessageStatus(array $messageFields): Sender\Result\MessageStatus
147 {
148 return $this->sender->getMessageStatus($messageFields);
149 }
150
151 public function sendMessage(array $messageFields): Sender\Result\SendMessage
152 {
153 return $this->sender->sendMessage($messageFields);
154 }
155
156 public function prepareMessageBodyForSave(string $text): string
157 {
158 return $this->sender->prepareMessageBodyForSave($text);
159 }
160
161 public function getMessageTemplates(string $subject = ''): Result
162 {
163 return $this->utils->getMessageTemplates($subject);
164 }
165
166 public function getSentTemplateMessage(string $from, string $to): string
167 {
168 return $this->utils->getSentTemplateMessage($from, $to);
169 }
170
172 {
173 $this->optionManager->setSocketTimeout($socketTimeout);
174 return $this;
175 }
176
178 {
179 $this->optionManager->setStreamTimeout($streamTimeout);
180 return $this;
181 }
182
183 public static function resolveStatus($serviceStatus): ?int
184 {
185 return (new SMS\StatusResolver())->resolveStatus($serviceStatus);
186 }
187
188 public function getRegistrationUrl(): string
189 {
190 return 'https://edna.ru/sms-bitrix/';
191 }
192
193 public function isMigratedToNewAPI(): bool
194 {
195 $isMigratedToNewAPI = \Bitrix\Main\Config\Option::get('messageservice', $this->getMigratingOptionName(), 'N');
196 if ($isMigratedToNewAPI === 'Y')
197 {
198 return true;
199 }
200
201 $currentDateTime = time();
202 $migratedDateTime = gmmktime(-3, 15,0, 12,1,2022); //December 01, 2022 00:15 MSK
203
204 return $currentDateTime >= $migratedDateTime;
205 }
206
207 private function initializeOldApiComponents(): void
208 {
209 $this->registrar = new SMS\Old\Registrar($this->getId(), $this->optionManager, $this->utils);
210 $this->initiator = new SMS\Old\Initiator($this->optionManager, $this->registrar, $this->utils, $this->getId());
211 $this->sender = new SMS\Old\Sender($this->optionManager,$this->registrar, $this->utils);
212 }
213
214 private function initializeNewApiComponents(): void
215 {
216 $this->registrar = new SMS\Registrar($this->getId(), $this->optionManager, $this->utils);
217 $this->initiator = new SMS\Initiator($this->optionManager, $this->registrar, $this->utils, $this->getId());
218 $this->sender = new SMS\Sender($this->optionManager,$this->registrar, $this->utils);
219 }
220
221 public function migrateToNewApi(): void
222 {
223 $oldRegistrar = new SMS\Old\Registrar($this->getId(), $this->optionManager, $this->utils);
224
225 if (!$this->canUse() && !$oldRegistrar->canUse())
226 {
227 return;
228 }
229
230 if ($this->getOption(InternalOption::SENDER_ID) !== null)
231 {
232 return;
233 }
234
235 $channelListResult = $this->utils->getChannelList(Edna\Constants\ChannelType::SMS);
236
237 $subjectIdList = [];
238 foreach ($channelListResult->getData() as $channel)
239 {
240 if (isset($channel['active'], $channel['subjectId']) && $channel['active'] === true)
241 {
242 $subjectIdList[] = $channel['subjectId'];
243 $this->utils->setCallback(
244 $this->getCallbackUrl(),
245 [
247 ],
248 $channel['subjectId']
249 );
250 }
251 }
252 $this->setOption(InternalOption::SENDER_ID, $subjectIdList);
253 $this->setNewApi(true);
254 }
255
256 public function setNewApi(bool $mode): void
257 {
258 \Bitrix\Main\Config\Option::set('messageservice', $this->getMigratingOptionName(),$mode ? 'Y' : 'N');
259 }
260
261 private function getMigratingOptionName(): string
262 {
263 return $this->getId() . '_' . InternalOption::NEW_API_AVAILABLE;
264 }
265
271 public function addRefreshFromListAgent(): void
272 {
273 $cacheManager = new CacheManager($this->getId());
274 $period = (int)ceil( $cacheManager->getTtl(CacheManager::CHANNEL_CACHE_ENTITY_ID) * .9);// async with cache expiration
275
276 \CAgent::AddAgent(static::class . "::refreshFromListAgent();", 'messageservice', 'Y', $period);
277 }
278
283 public static function refreshFromListAgent(): string
284 {
285 $sender = new static();
286 if (!$sender::isSupported() || !$sender->isRegistered())
287 {
288 return '';
289 }
290
291 $sender->refreshFromList();
292
293 return __METHOD__ . '();';
294 }
295}
getOption($optionName, $defaultValue=null)
Providers Sender $sender
Definition base.php:13
getSentTemplateMessage(string $from, string $to)