Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Option.php
1<?php
2
4
7use Bitrix\MessageService\Providers\Encryptor;
9
10class Option implements OptionManager
11{
12 use Encryptor;
13
14 protected ?array $options;
15
16 protected string $providerType;
17 protected string $providerId;
18 protected string $dbOptionName;
19
20 protected int $socketTimeout = 10;
21 protected int $streamTimeout = 30;
22
23 public function __construct(string $providerType, string $providerId)
24 {
25 $this->options = null;
26
27 $this->providerType = mb_strtolower($providerType);
28 $this->providerId = $providerId;
29
30 $this->dbOptionName = 'sender.' . $this->providerType . '.' . $this->providerId;
31 }
32
36 public function getSocketTimeout(): int
37 {
38 return $this->socketTimeout;
39 }
40
45 public function setSocketTimeout(int $socketTimeout): OptionManager
46 {
47 $this->socketTimeout = $socketTimeout;
48 return $this;
49 }
50
54 public function getStreamTimeout(): int
55 {
56 return $this->streamTimeout;
57 }
58
63 public function setStreamTimeout(int $streamTimeout): OptionManager
64 {
65 $this->streamTimeout = $streamTimeout;
66 return $this;
67 }
68
69 public function setOptions(array $options): OptionManager
70 {
71 $this->options = $options;
72 $data = serialize($options);
73
74 $encryptedData = [
75 'crypto' => 'Y',
76 'data' => self::encrypt($data, $this->providerType . '-' . $this->providerId)
77 ];
78
79 Main\Config\Option::set('messageservice', $this->dbOptionName, serialize($encryptedData));
80
81 return $this;
82 }
83
84 public function setOption(string $optionName, $optionValue): OptionManager
85 {
86 $options = $this->getOptions();
87
88 if (!isset($options[$optionName]) || $options[$optionName] !== $optionValue)
89 {
90 $options[$optionName] = $optionValue;
91 $this->setOptions($options);
92 }
93
94 return $this;
95 }
96
97 public function getOptions(): array
98 {
99 $this->options ??= $this->loadOptions();
100
101 return $this->options;
102 }
103
104 public function getOption(string $optionName, $defaultValue = null)
105 {
106 $this->getOptions();
107
108 return $this->options[$optionName] ?? $defaultValue;
109 }
110
111 public function clearOptions(): OptionManager
112 {
113 $this->options = [];
114 Main\Config\Option::delete('messageservice', array('name' => $this->dbOptionName));
115
116 return $this;
117 }
118
119 protected function loadOptions(): array
120 {
121 $data = Main\Config\Option::get('messageservice', $this->dbOptionName);
122 $data = unserialize($data, ['allowed_classes' => false]);
123
124 if (!isset($data['crypto']) && !isset($data['data']))
125 {
126 return is_array($data) ? $data : [];
127 }
128
129 $decryptedData = self::decrypt($data['data'], $this->providerType . '-' . $this->providerId);
130 $options = unserialize($decryptedData, ['allowed_classes' => false]);
131
132 return is_array($options) ? $options : [];
133 }
134
135 public function getProviderId(): string
136 {
137 return $this->providerId;
138 }
139}
getOption(string $optionName, $defaultValue=null)
Definition Option.php:104
setOption(string $optionName, $optionValue)
Definition Option.php:84
__construct(string $providerType, string $providerId)
Definition Option.php:23