Skip to content

Commit 1e4c4a5

Browse files
mfahadahmedaliabbasrizvi
authored andcommitted
feat(datafile-management): Adds StaticProjectConfigManager and integrated it with Optimizely class. (#179)
1 parent 8c35714 commit 1e4c4a5

File tree

8 files changed

+339
-128
lines changed

8 files changed

+339
-128
lines changed

src/Optimizely/Config/DatafileProjectConfig.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Optimizely\Config;
1919

20+
use Exception;
2021
use Monolog\Logger;
2122
use Optimizely\Entity\Attribute;
2223
use Optimizely\Entity\Audience;
@@ -37,9 +38,11 @@
3738
use Optimizely\Exceptions\InvalidFeatureFlagException;
3839
use Optimizely\Exceptions\InvalidFeatureVariableException;
3940
use Optimizely\Exceptions\InvalidGroupException;
41+
use Optimizely\Exceptions\InvalidInputException;
4042
use Optimizely\Exceptions\InvalidRolloutException;
4143
use Optimizely\Exceptions\InvalidVariationException;
4244
use Optimizely\Logger\LoggerInterface;
45+
use Optimizely\Logger\DefaultLogger;
4346
use Optimizely\Optimizely;
4447
use Optimizely\Utils\ConditionDecoder;
4548
use Optimizely\Utils\ConfigParser;
@@ -305,6 +308,41 @@ public function __construct($datafile, $logger, $errorHandler)
305308
}
306309
}
307310

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+
308346
/**
309347
* @return string String representing account ID from the datafile.
310348
*/
@@ -501,7 +539,7 @@ public function getAttribute($attributeKey)
501539

502540
$this->_logger->log(Logger::ERROR, sprintf('Attribute key "%s" is not in datafile.', $attributeKey));
503541
$this->_errorHandler->handleError(new InvalidAttributeException('Provided attribute is not in datafile.'));
504-
542+
505543
return null;
506544
}
507545

@@ -593,7 +631,7 @@ public function getFeatureVariableFromKey($featureFlagKey, $variableKey)
593631
);
594632
return null;
595633
}
596-
634+
597635
/**
598636
* Determines if given experiment is a feature test.
599637
*

src/Optimizely/Entity/FeatureVariable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,26 @@ public function setDefaultValue($value)
132132
{
133133
$this->_defaultValue = $value;
134134
}
135+
136+
/**
137+
* Returns feature variable method name based on
138+
* feature variable type.
139+
*
140+
* @param String $type Feature variable type.
141+
*/
142+
public static function getFeatureVariableMethodName($type)
143+
{
144+
switch ($type) {
145+
case FeatureVariable::BOOLEAN_TYPE:
146+
return "getFeatureVariableBoolean";
147+
case FeatureVariable::INTEGER_TYPE:
148+
return "getFeatureVariableInteger";
149+
case FeatureVariable::DOUBLE_TYPE:
150+
return "getFeatureVariableDouble";
151+
case FeatureVariable::STRING_TYPE:
152+
return "getFeatureVariableString";
153+
default:
154+
return null;
155+
}
156+
}
135157
}

0 commit comments

Comments
 (0)