Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
EdnaUtils.php
1<?php
3
10
11abstract class EdnaUtils implements EdnaRu
12{
13 protected string $providerId;
16
17 abstract public function getMessageTemplates(string $subject = ''): Result;
18 abstract protected function initializeDefaultExternalSender(): Providers\ExternalSender;
19
20 public function __construct(string $providerId, Providers\OptionManager $optionManager)
21 {
22 $this->providerId = $providerId;
23 $this->optionManager = $optionManager;
24 $this->externalSender = $this->initializeDefaultExternalSender();
25 }
26
33 public function getChannelList(string $imType): Result
34 {
35 if (!in_array($imType, Constants\ChannelType::getAllTypeList(), true))
36 {
37 return (new Result())->addError(new Error('Incorrect imType'));
38 }
39
40 $channelResult = $this->gelAllChannelList();
41 if (!$channelResult->isSuccess())
42 {
43 return (new Result())->addError(new Error('Edna service error'));
44 }
45
46 $channelList = [];
47 foreach ($channelResult->getData() as $channel)
48 {
49 if (is_array($channel) && isset($channel['type']) && $channel['type'] === $imType)
50 {
51 $channelList[] = $channel;
52 }
53 }
54 if (empty($channelList))
55 {
56 return (new Result())->addError(new Error("There are no $imType channels in your profile"));
57 }
58
59 $result = new Result();
60 $result->setData($channelList);
61
62 return $result;
63 }
64
69 public function getCascadeList(): Result
70 {
71 $apiResult = $this->externalSender->callExternalMethod(Constants\Method::GET_CASCADES, [
72 'offset' => 0,
73 'limit' => 0
74 ]);
75
76 return $apiResult;
77 }
78
86 public function setCallback(string $callbackUrl, array $callbackTypes, ?int $subjectId = null): Result
87 {
88 $typeList = Constants\CallbackType::getAllTypeList();
89
90 $requestParams = [];
91 foreach ($callbackTypes as $callbackType)
92 {
93 if (in_array($callbackType, $typeList, true))
94 {
95 $requestParams[$callbackType] = $callbackUrl;
96 }
97 }
98 if (empty($requestParams))
99 {
100 return (new Result())->addError(new Error('Invalid callback types passed'));
101 }
102
103 if ($subjectId)
104 {
105 $requestParams['subjectId'] = $subjectId;
106 }
107 $this->externalSender->setApiKey($this->optionManager->getOption(InternalOption::API_KEY));
108
109 return $this->externalSender->callExternalMethod('callback/set', $requestParams);
110 }
111
112 public function getActiveChannelList(string $imType): Result
113 {
114 $channelListResult = $this->getChannelList($imType);
115 if (!$channelListResult->isSuccess())
116 {
117 return $channelListResult;
118 }
119
120 $activeChannelList = [];
121 foreach ($channelListResult->getData() as $channel)
122 {
123 if (isset($channel['active'], $channel['subjectId']) && $channel['active'] === true)
124 {
125 $activeChannelList[] = $channel;
126 }
127 }
128
129 if (empty($activeChannelList))
130 {
131 return (new Result())->addError(new Error('There are no active channels'));
132 }
133
134 return (new Result())->setData($activeChannelList);
135 }
136
137 public function checkActiveChannelBySubjectIdList(array $subjectIdList, string $imType): bool
138 {
139 if (empty($subjectIdList))
140 {
141 return false;
142 }
143
144 $channelResult = $this->getChannelList($imType);
145 if (!$channelResult->isSuccess())
146 {
147 return false;
148 }
149
150 $checkedChannels = [];
151 foreach ($channelResult->getData() as $channel)
152 {
153 if (
154 isset($channel['active'], $channel['subjectId'])
155 && $channel['active'] === true
156 && in_array($channel['subjectId'], $subjectIdList, true)
157 )
158 {
159 $checkedChannels[] = $channel['subjectId'];
160 }
161 }
162
163 return count($checkedChannels) === count($subjectIdList);
164 }
165
171 public function getCascadeIdFromSubject($subject, callable $subjectComparator): Result
172 {
173 $apiResult = $this->getCascadeList();
174 if (!$apiResult->isSuccess())
175 {
176 return $apiResult;
177 }
178
179 $apiData = $apiResult->getData();
180 $result = new Result();
181 foreach ($apiData as $cascade)
182 {
183 if ($cascade['status'] !== 'ACTIVE' || $cascade['stagesCount'] > 1)
184 {
185 continue;
186 }
187 if ($subjectComparator($cascade['stages'][0]['subject'], $subject))
188 {
189 $result->setData(['cascadeId' => $cascade['id']]);
190
191 return $result;
192 }
193 }
194
195 $result->addError(new Error('Not cascade'));
196
197 return $result;
198 }
199
200
201 private function gelAllChannelList(): Result
202 {
203 $this->externalSender->setApiKey($this->optionManager->getOption(InternalOption::API_KEY));
204 return $this->externalSender->callExternalMethod(Constants\Method::GET_CHANNELS);
205 }
206
213 public function updateSavedChannelList(string $channelType): array
214 {
215 $fromList = [];
216 $activeChannelListResult = $this->getActiveChannelList($channelType);
217 if ($activeChannelListResult->isSuccess())
218 {
219 $registeredSubjectIdList = $this->optionManager->getOption(Providers\Constants\InternalOption::SENDER_ID, []);
220 $channels = [];
221 foreach ($activeChannelListResult->getData() as $channel)
222 {
223 if (in_array((int)$channel['subjectId'], $registeredSubjectIdList, true))
224 {
225 $fromList[] = [
226 'id' => $channel['subjectId'],
227 'name' => $channel['name'],
228 'channelPhone' => $channel['channelAttribute'] ?? '',
229 ];
230 $channels[] = [
231 'SENDER_ID' => $this->providerId,
232 'EXTERNAL_ID' => $channel['subjectId'],
233 'TYPE' => $channelType,
234 'NAME' => $channel['name'],
235 'ADDITIONAL_PARAMS' => [
236 'channelAttribute' => $channel['channelAttribute'] ?? ''
237 ],
238 ];
239 }
240 }
241
242 if (count($channels) > 0)
243 {
244 ChannelTable::reloadChannels($this->providerId, $channelType, $channels);
245 }
246 else
247 {
248 ChannelTable::deleteByFilter([
249 '=SENDER_ID' => $this->providerId,
250 '=TYPE' => $channelType,
251 ]);
252 }
253 }
254
255 return $fromList;
256 }
257}
addError(Error $error)
Definition result.php:50
setCallback(string $callbackUrl, array $callbackTypes, ?int $subjectId=null)
Definition EdnaUtils.php:86
getCascadeIdFromSubject($subject, callable $subjectComparator)
__construct(string $providerId, Providers\OptionManager $optionManager)
Definition EdnaUtils.php:20
checkActiveChannelBySubjectIdList(array $subjectIdList, string $imType)