|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2019, Optimizely Inc and Contributors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Optimizely\ProjectConfigManager; |
| 19 | + |
| 20 | +use Exception; |
| 21 | +use GuzzleHttp\Client as HttpClient; |
| 22 | +use Monolog\Logger; |
| 23 | +use Optimizely\Config\DatafileProjectConfig; |
| 24 | +use Optimizely\Enums\ProjectConfigManagerConstants; |
| 25 | +use Optimizely\ErrorHandler\NoOpErrorHandler; |
| 26 | +use Optimizely\Logger\NoOpLogger; |
| 27 | +use Optimizely\Utils\Validator; |
| 28 | + |
| 29 | +class HTTPProjectConfigManager implements ProjectConfigManagerInterface |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var \GuzzleHttp\Client Guzzle HTTP client to send requests. |
| 33 | + */ |
| 34 | + private $httpClient; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var DatafileProjectConfig |
| 38 | + */ |
| 39 | + private $_config; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var String Datafile URL. |
| 43 | + */ |
| 44 | + private $_url; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var boolean Flag indicates that skip JSON validation of datafile. |
| 48 | + */ |
| 49 | + private $_skipJsonValidation; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var String datafile last modified time. |
| 53 | + */ |
| 54 | + private $_lastModifiedSince; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var LoggerInterface Logger instance. |
| 58 | + */ |
| 59 | + private $_logger; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var ErrorHandlerInterface ErrorHandler instance. |
| 63 | + */ |
| 64 | + private $_errorHandler; |
| 65 | + |
| 66 | + public function __construct( |
| 67 | + $sdkKey = null, |
| 68 | + $url = null, |
| 69 | + $urlTemplate = null, |
| 70 | + $fetchOnInit = true, |
| 71 | + $datafile = null, |
| 72 | + $skipJsonValidation = false, |
| 73 | + $logger = null, |
| 74 | + $errorHandler = null |
| 75 | + ) { |
| 76 | + $this->_skipJsonValidation = $skipJsonValidation; |
| 77 | + $this->_logger = $logger; |
| 78 | + $this->_errorHandler = $errorHandler; |
| 79 | + $this->httpClient = new HttpClient(); |
| 80 | + |
| 81 | + if ($this->_logger === null) { |
| 82 | + $this->_logger = new NoOpLogger(); |
| 83 | + } |
| 84 | + |
| 85 | + if ($this->_errorHandler === null) { |
| 86 | + $this->_errorHandler = new NoOpErrorHandler(); |
| 87 | + } |
| 88 | + |
| 89 | + $this->_url = $this->getUrl($sdkKey, $url, $urlTemplate); |
| 90 | + |
| 91 | + if ($datafile !== null) { |
| 92 | + $this->_config = DatafileProjectConfig::createProjectConfigFromDatafile( |
| 93 | + $datafile, |
| 94 | + $skipJsonValidation, |
| 95 | + $this->_logger, |
| 96 | + $this->_errorHandler |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + // Update config on initialization. |
| 101 | + if ($fetchOnInit === true) { |
| 102 | + $this->fetch(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Helper function to return URL based on params passed. |
| 108 | + * |
| 109 | + * @param $sdkKey string SDK key. |
| 110 | + * @param $url string URL for datafile. |
| 111 | + * @param $urlTemplate string Template to be used with SDK key to fetch datafile. |
| 112 | + * |
| 113 | + * @return string URL for datafile. |
| 114 | + */ |
| 115 | + protected function getUrl($sdkKey, $url, $urlTemplate) |
| 116 | + { |
| 117 | + if (Validator::validateNonEmptyString($url)) { |
| 118 | + return $url; |
| 119 | + } |
| 120 | + |
| 121 | + if (!Validator::validateNonEmptyString($sdkKey)) { |
| 122 | + $exception = new Exception("One of the SDK key or URL must be provided."); |
| 123 | + $this->_errorHandler->handleError($exception); |
| 124 | + throw $exception; |
| 125 | + } |
| 126 | + |
| 127 | + if (!Validator::validateNonEmptyString($urlTemplate)) { |
| 128 | + $urlTemplate = ProjectConfigManagerConstants::DEFAULT_URL_TEMPLATE; |
| 129 | + } |
| 130 | + |
| 131 | + $url = sprintf($urlTemplate, $sdkKey); |
| 132 | + |
| 133 | + return $url; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Function to fetch latest datafile. |
| 138 | + * |
| 139 | + * @return boolean flag to indicate if datafile is updated. |
| 140 | + */ |
| 141 | + public function fetch() |
| 142 | + { |
| 143 | + $datafile = $this->fetchDatafile(); |
| 144 | + |
| 145 | + if ($datafile === null) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Helper function to fetch datafile and handle response if datafile is modified. |
| 154 | + * |
| 155 | + * @return null|datafile. |
| 156 | + */ |
| 157 | + protected function fetchDatafile() |
| 158 | + { |
| 159 | + $headers = null; |
| 160 | + |
| 161 | + // Add If-Modified-Since header. |
| 162 | + if (Validator::validateNonEmptyString($this->_lastModifiedSince)) { |
| 163 | + $headers = array(ProjectConfigManagerConstants::IF_MODIFIED_SINCE => $this->_lastModifiedSince); |
| 164 | + } |
| 165 | + |
| 166 | + $options = [ |
| 167 | + 'headers' => $headers, |
| 168 | + 'timeout' => ProjectConfigManagerConstants::TIMEOUT, |
| 169 | + 'connect_timeout' => ProjectConfigManagerConstants::TIMEOUT |
| 170 | + ]; |
| 171 | + |
| 172 | + try { |
| 173 | + $response = $this->httpClient->get($this->_url, $options); |
| 174 | + } catch (Exception $exception) { |
| 175 | + $this->_logger->log(Logger::ERROR, 'Unexpected response when trying to fetch datafile, status code: ' . $exception->getCode()); |
| 176 | + return null; |
| 177 | + } |
| 178 | + |
| 179 | + $status = $response->getStatusCode(); |
| 180 | + |
| 181 | + // Datafile not updated. |
| 182 | + if ($status === 304) { |
| 183 | + $this->_logger->log(Logger::DEBUG, 'Not updating ProjectConfig as datafile has not updated since ' . $this->_lastModifiedSince); |
| 184 | + return null; |
| 185 | + } |
| 186 | + |
| 187 | + // Datafile retrieved successfully. |
| 188 | + if ($status >= 200 && $status < 300) { |
| 189 | + if ($response->hasHeader(ProjectConfigManagerConstants::LAST_MODIFIED)) { |
| 190 | + $this->_lastModifiedSince = $response->getHeader(ProjectConfigManagerConstants::LAST_MODIFIED)[0]; |
| 191 | + } |
| 192 | + |
| 193 | + $datafile = $response->getBody(); |
| 194 | + |
| 195 | + if ($this->handleResponse($datafile) === true) { |
| 196 | + return $datafile; |
| 197 | + } |
| 198 | + |
| 199 | + return null; |
| 200 | + } |
| 201 | + |
| 202 | + // Failed to retrieve datafile from Url. |
| 203 | + $this->_logger->log(Logger::ERROR, 'Unexpected response when trying to fetch datafile, status code: ' . $status); |
| 204 | + return null; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Helper function to create config from datafile. |
| 209 | + * |
| 210 | + * @param string $datafile |
| 211 | + * @return boolean flag to indicate if config is updated. |
| 212 | + */ |
| 213 | + protected function handleResponse($datafile) |
| 214 | + { |
| 215 | + if ($datafile === null) { |
| 216 | + return false; |
| 217 | + } |
| 218 | + |
| 219 | + $config = DatafileProjectConfig::createProjectConfigFromDatafile($datafile, $this->_skipJsonValidation, $this->_logger, $this->_errorHandler); |
| 220 | + if ($config === null) { |
| 221 | + return false; |
| 222 | + } |
| 223 | + |
| 224 | + $previousRevision = null; |
| 225 | + if ($this->_config !== null) { |
| 226 | + $previousRevision = $this->_config->getRevision(); |
| 227 | + } |
| 228 | + |
| 229 | + if ($previousRevision === $config->getRevision()) { |
| 230 | + return false; |
| 231 | + } |
| 232 | + |
| 233 | + $this->_config = $config; |
| 234 | + return true; |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Returns instance of DatafileProjectConfig. |
| 239 | + * @return null|DatafileProjectConfig DatafileProjectConfig instance. |
| 240 | + */ |
| 241 | + public function getConfig() |
| 242 | + { |
| 243 | + return $this->_config; |
| 244 | + } |
| 245 | +} |
0 commit comments