|
17 | 17 |
|
18 | 18 | namespace Optimizely\Config; |
19 | 19 |
|
| 20 | +use Exception; |
20 | 21 | use Monolog\Logger; |
21 | 22 | use Optimizely\Entity\Attribute; |
22 | 23 | use Optimizely\Entity\Audience; |
|
37 | 38 | use Optimizely\Exceptions\InvalidFeatureFlagException; |
38 | 39 | use Optimizely\Exceptions\InvalidFeatureVariableException; |
39 | 40 | use Optimizely\Exceptions\InvalidGroupException; |
| 41 | +use Optimizely\Exceptions\InvalidInputException; |
40 | 42 | use Optimizely\Exceptions\InvalidRolloutException; |
41 | 43 | use Optimizely\Exceptions\InvalidVariationException; |
42 | 44 | use Optimizely\Logger\LoggerInterface; |
| 45 | +use Optimizely\Logger\DefaultLogger; |
43 | 46 | use Optimizely\Optimizely; |
44 | 47 | use Optimizely\Utils\ConditionDecoder; |
45 | 48 | use Optimizely\Utils\ConfigParser; |
@@ -305,6 +308,41 @@ public function __construct($datafile, $logger, $errorHandler) |
305 | 308 | } |
306 | 309 | } |
307 | 310 |
|
| 311 | + /** |
| 312 | + * Create ProjectConfig based on datafile string. |
| 313 | + * |
| 314 | + * @param string $datafile JSON string representing the Optimizely project. |
| 315 | + * @param bool $skipJsonValidation boolean representing whether JSON schema validation needs to be performed. |
| 316 | + * @param LoggerInterface $logger Logger instance |
| 317 | + * @param ErrorHandlerInterface $errorHandler ErrorHandler instance. |
| 318 | + * @return ProjectConfig ProjectConfig instance or null; |
| 319 | + */ |
| 320 | + public static function createProjectConfigFromDatafile($datafile, $skipJsonValidation, $logger, $errorHandler) |
| 321 | + { |
| 322 | + if (!$skipJsonValidation) { |
| 323 | + if (!Validator::validateJsonSchema($datafile)) { |
| 324 | + $defaultLogger = new DefaultLogger(); |
| 325 | + $defaultLogger->log(Logger::ERROR, 'Provided "datafile" has invalid schema.'); |
| 326 | + $logger->log(Logger::ERROR, 'Provided "datafile" has invalid schema.'); |
| 327 | + return null; |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + try { |
| 332 | + $config = new DatafileProjectConfig($datafile, $logger, $errorHandler); |
| 333 | + } catch (Exception $exception) { |
| 334 | + $defaultLogger = new DefaultLogger(); |
| 335 | + $errorMsg = $exception->getCode() == InvalidDatafileVersionException::class ? $exception->getMessage() : sprintf(Errors::INVALID_FORMAT, 'datafile'); |
| 336 | + $errorToHandle = $exception->getCode() == InvalidDatafileVersionException::class ? new InvalidDatafileVersionException($errorMsg) : new InvalidInputException($errorMsg); |
| 337 | + $defaultLogger->log(Logger::ERROR, $errorMsg); |
| 338 | + $logger->log(Logger::ERROR, $errorMsg); |
| 339 | + $errorHandler->handleError($errorToHandle); |
| 340 | + return null; |
| 341 | + } |
| 342 | + |
| 343 | + return $config; |
| 344 | + } |
| 345 | + |
308 | 346 | /** |
309 | 347 | * @return string String representing account ID from the datafile. |
310 | 348 | */ |
@@ -501,7 +539,7 @@ public function getAttribute($attributeKey) |
501 | 539 |
|
502 | 540 | $this->_logger->log(Logger::ERROR, sprintf('Attribute key "%s" is not in datafile.', $attributeKey)); |
503 | 541 | $this->_errorHandler->handleError(new InvalidAttributeException('Provided attribute is not in datafile.')); |
504 | | - |
| 542 | + |
505 | 543 | return null; |
506 | 544 | } |
507 | 545 |
|
@@ -593,7 +631,7 @@ public function getFeatureVariableFromKey($featureFlagKey, $variableKey) |
593 | 631 | ); |
594 | 632 | return null; |
595 | 633 | } |
596 | | - |
| 634 | + |
597 | 635 | /** |
598 | 636 | * Determines if given experiment is a feature test. |
599 | 637 | * |
|
0 commit comments