Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Registrar.php
1<?php
2
4
12use Bitrix\MessageService\Sender\Traits\RussianProvider;
13
15{
16
17 use RussianProvider { RussianProvider::isSupported as isRussianRegion; }
18
19 protected EdnaRu $utils;
20 protected string $channelType = '';
21
22 abstract protected function getCallbackTypeList(): array;
23
25 {
26 parent::__construct($providerId, $optionManager);
27
28 $this->utils = $utils;
29 }
30
31 public function isRegistered(): bool
32 {
33 return
34 !is_null($this->optionManager->getOption(InternalOption::API_KEY))
35 && !is_null($this->optionManager->getOption(InternalOption::SENDER_ID));
36 }
37
38 public function register(array $fields): Result
39 {
40 if (isset($fields['subject_id']))
41 {
42 $fields[InternalOption::SENDER_ID] = $fields['subject_id'];
43 }
44
45 if (!isset($fields[InternalOption::API_KEY], $fields[InternalOption::SENDER_ID]))
46 {
47 return (new Result())->addError(new Error(Loc::getMessage('MESSAGESERVICE_SENDER_SMS_EDNARU_EMPTY_REQUIRED_FIELDS')));
48 }
49 $this->optionManager->setOption(InternalOption::API_KEY, (string)$fields[InternalOption::API_KEY]);
50
51 $subjectIdList = [];
52 foreach (explode(';', (string)$fields[InternalOption::SENDER_ID]) as $senderId)
53 {
54 $senderId = trim($senderId);
55 if ($senderId !== '')
56 {
57 $subjectIdList[] = (int)$senderId;
58 }
59 }
60 if (!$this->utils->checkActiveChannelBySubjectIdList($subjectIdList, $this->channelType))
61 {
62 $this->optionManager->clearOptions();
63
64 return (new Result())->addError(new Error(Loc::getMessage('MESSAGESERVICE_EDNARU_INACTIVE_CHANNEL_ERROR')));
65 }
66
67 foreach ($subjectIdList as $subjectId)
68 {
69 $setCallbackResult = $this->utils->setCallback(
70 $this->getCallbackUrl(),
71 $this->getCallbackTypeList(),
72 $subjectId
73 );
74 if (!$setCallbackResult->isSuccess())
75 {
76 $this->optionManager->clearOptions();
77
78 $errorData = $setCallbackResult->getData();
79
80 if (isset($errorData['detail']))
81 {
82 return (new Result())->addError(new Error($errorData['detail']));
83 }
84
85 return $setCallbackResult;
86 }
87 }
88
89 $this->optionManager->setOption(InternalOption::SENDER_ID, $subjectIdList);
90 $this->optionManager->setOption(InternalOption::MIGRATED_TO_STANDART_SETTING_NAMES, 'Y');
91
92 return new Result();
93 }
94
98 public function getOwnerInfo(): array
99 {
100 return [
101 InternalOption::API_KEY => $this->optionManager->getOption(InternalOption::API_KEY),
102 InternalOption::SENDER_ID => $this->optionManager->getOption(InternalOption::SENDER_ID),
103 ];
104 }
105
106 public function isSupported(): bool
107 {
108 return self::isRussianRegion();
109 }
110
111 public function canUse(): bool
112 {
113 return ($this->isRegistered() && $this->isConfirmed());
114 }
115
116 public function getExternalManageUrl(): string
117 {
119 {
120 return 'https://app.edna.io/';
121 }
122
123 return 'https://app.edna.ru/';
124 }
125}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
addError(Error $error)
Definition result.php:50
__construct(string $providerId, Providers\OptionManager $optionManager, EdnaRu $utils)
Definition Registrar.php:24