logger = $logger; $this->contextFactory = $contextFactory; $this->deploymentConfig = $deploymentConfig; $this->envWriter = $envWriter; $this->random = $random; $this->idFactorProviders = $idFactorProviders; } /** * Calculates the value of X-Magento-Cache-Id * * @return string|null */ public function getCacheId(): ?string { if (!$this->idFactorProviders) { return null; } try { $salt = $this->getSalt(); $context = $this->contextFactory->get(); foreach ($this->idFactorProviders as $idFactorProvider) { $keys[$idFactorProvider->getFactorName()] = $idFactorProvider->getFactorValue($context); } ksort($keys); $keysString = strtoupper(implode('|', array_values($keys))) . "|$salt"; return hash('sha256', $keysString); } catch (Exception $e) { $this->logger->warning("Unable to obtain " . self::CACHE_ID_HEADER . " value: " . $e->getMessage()); return null; } } /** * Gets the existing salt from the environment config or creates one if it hasn't been set yet * * @return string * @throws Exception */ private function getSalt(): string { $salt = $this->deploymentConfig->get(self::SALT_CONFIG_PATH); if ($salt) { return $salt; } $salt = $this->random->getRandomString(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE); $config = new ConfigData(ConfigFilePool::APP_ENV); $config->set(self::SALT_CONFIG_PATH, $salt); $this->envWriter->saveConfig([$config->getFileKey() => $config->getData()], false, null, [], true); return $salt; } }