Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
tokenrequester.php
1<?php
2
4
14
15final class TokenRequester extends BaseSender
16{
17 private const SAFE_BUFFER_TIME_SECONDS = 60;
18 private const ERROR_LICENSE_NOT_FOUND = 'LICENSE_NOT_FOUND';
19 private const ERROR_WRONG_SIGN = 'WRONG_SIGN';
20 private const ERROR_LICENSE_DEMO = 'LICENSE_DEMO';
21 private const ERROR_LICENSE_NOT_ACTIVE = 'LICENSE_NOT_ACTIVE';
22
23 private const CACHE_TABLE = '/bx/osmgateway/license';
24 private const CACHE_TTL = 86400;
25
26 private OsmSource $source;
27 private ManagedCache $cacheManager;
28
29 public function setSource(OsmSource $source): TokenRequester
30 {
31 $this->source = $source;
32 $this->cacheManager = Application::getInstance()->getManagedCache();
33 return $this;
34 }
35
36 public function getToken(): ?Token
37 {
38 $token = $this->getFromConfig();
39 if ($token)
40 {
41 return $token;
42 }
43
44 if (!$this->hasLicenseIssues())
45 {
46 $token = $this->requestNewToken();
47 if ($token)
48 {
49 $this->updateConfigToken($token);
50 }
51 }
52
53 return $token;
54 }
55
56 private function updateConfigToken(Token $token): void
57 {
58 $config = $this->source->getConfig();
59 if (!$config)
60 {
61 $config = new Config();
62 $this->source->setConfig($config);
63 }
64 $config->setValue('TOKEN', serialize($token->convertToArray()));
65
66 (new SourceRepository(new OrmConverter()))->save($this->source);
67 }
68
69 private function getFromConfig(): ?Token
70 {
71 $config = $this->source->getConfig();
72 if ($config === null)
73 {
74 return null;
75 }
76
77 $tokenArray = $config->getValue('TOKEN');
78 if (!$tokenArray)
79 {
80 return null;
81 }
82
83 if (!CheckSerializedData($tokenArray))
84 {
85 return null;
86 }
87
88 $token = Token::makeFromArray(unserialize($tokenArray, ['allowed_classes' => false]));
89 if(!$token)
90 {
91 return null;
92 }
93
94 if ($token->getToken() === '')
95 {
96 return null;
97 }
98
99 if ($token->getExpiry() <= time() + self::SAFE_BUFFER_TIME_SECONDS)
100 {
101 $token = null;
102 }
103
104 return $token;
105 }
106
107 private function requestNewToken(): ?Token
108 {
109 $result = $this->performRequest('osmgateway.token.get');
110 if (!$result->isSuccess())
111 {
112 $this->checkLicenseIssueByResult($result);
113 return null;
114 }
115
116 $tokenData = $result->getData();
117 if (
118 !isset($tokenData['token'])
119 || !isset($tokenData['expire'])
120 )
121 {
123 LoggerService\LogLevel::ERROR,
124 print_r($result, true),
126 );
127
128 return null;
129 }
130
131 return new Token(
132 (string)$tokenData['token'],
133 (int)$tokenData['expire']
134 );
135 }
136
137 protected function getServiceUrl(): string
138 {
139 $serviceUrl = $this->source->getOsmServiceUrl();
140
141 return $serviceUrl ?? '';
142 }
143
144 public function getHttpClientParameters(): array
145 {
146 return [
147 'socketTimeout' => 5,
148 'streamTimeout' => 10,
149 'headers' => [
150 'Bx-Location-Osm-Host' => $this->source->getOsmHostName()
151 ]
152 ];
153 }
154
155 private function checkLicenseIssueByResult(Result $result): void
156 {
157 $licenseIssueErrorCodes = [
158 self::ERROR_LICENSE_NOT_FOUND,
159 self::ERROR_WRONG_SIGN,
160 self::ERROR_LICENSE_DEMO,
161 self::ERROR_LICENSE_NOT_ACTIVE,
162 ];
163
164 $errors = $result->getErrors();
165 foreach ($errors as $error)
166 {
167 if (in_array($error->getCode(), $licenseIssueErrorCodes, true))
168 {
169 $this->cacheManager->set(self::getCacheId(), true);
170 }
171 }
172 }
173
174 public function hasLicenseIssues(): bool
175 {
176 if ($this->cacheManager->read(self::CACHE_TTL, self::getCacheId(), self::CACHE_TABLE))
177 {
178 return (bool)$this->cacheManager->get(self::getCacheId());
179 }
180
181 return false;
182 }
183
184 private static function getCacheId(): string
185 {
186 return md5(serialize([
187 'BX_TYPE' => Client::getPortalType(),
188 'BX_LICENCE' => Client::getLicenseCode(),
189 ]));
190 }
191}
static makeFromArray(array $token)
Definition token.php:60
performRequest($action, array $parameters=[])