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';
23 private const CACHE_TABLE =
'/bx/osmgateway/license';
24 private const CACHE_TTL = 86400;
31 $this->source = $source;
38 $token = $this->getFromConfig();
46 $token = $this->requestNewToken();
49 $this->updateConfigToken($token);
56 private function updateConfigToken(
Token $token): void
58 $config = $this->source->getConfig();
62 $this->source->setConfig($config);
69 private function getFromConfig(): ?Token
71 $config = $this->source->getConfig();
77 $tokenArray = $config->getValue(
'TOKEN');
83 if (!CheckSerializedData($tokenArray))
99 if ($token->
getExpiry() <= time() + self::SAFE_BUFFER_TIME_SECONDS)
107 private function requestNewToken(): ?Token
110 if (!$result->isSuccess())
112 $this->checkLicenseIssueByResult($result);
116 $tokenData = $result->getData();
118 !isset($tokenData[
'token'])
119 || !isset($tokenData[
'expire'])
124 print_r($result,
true),
132 (
string)$tokenData[
'token'],
133 (
int)$tokenData[
'expire']
139 $serviceUrl = $this->source->getOsmServiceUrl();
141 return $serviceUrl ??
'';
147 'socketTimeout' => 5,
148 'streamTimeout' => 10,
150 'Bx-Location-Osm-Host' => $this->source->getOsmHostName()
155 private function checkLicenseIssueByResult(
Result $result): void
157 $licenseIssueErrorCodes = [
158 self::ERROR_LICENSE_NOT_FOUND,
159 self::ERROR_WRONG_SIGN,
160 self::ERROR_LICENSE_DEMO,
161 self::ERROR_LICENSE_NOT_ACTIVE,
165 foreach ($errors as $error)
167 if (in_array($error->getCode(), $licenseIssueErrorCodes,
true))
169 $this->cacheManager->set(self::getCacheId(),
true);
176 if ($this->cacheManager->read(self::CACHE_TTL, self::getCacheId(), self::CACHE_TABLE))
178 return (
bool)$this->cacheManager->get(self::getCacheId());
184 private static function getCacheId(): string
186 return md5(serialize([
performRequest($action, array $parameters=[])