Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions src/Optimizely/Config/DatafileProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Optimizely\Config;

use Exception;
use Monolog\Logger;
use Optimizely\Entity\Attribute;
use Optimizely\Entity\Audience;
Expand All @@ -37,9 +38,11 @@
use Optimizely\Exceptions\InvalidFeatureFlagException;
use Optimizely\Exceptions\InvalidFeatureVariableException;
use Optimizely\Exceptions\InvalidGroupException;
use Optimizely\Exceptions\InvalidInputException;
use Optimizely\Exceptions\InvalidRolloutException;
use Optimizely\Exceptions\InvalidVariationException;
use Optimizely\Logger\LoggerInterface;
use Optimizely\Logger\DefaultLogger;
use Optimizely\Optimizely;
use Optimizely\Utils\ConditionDecoder;
use Optimizely\Utils\ConfigParser;
Expand Down Expand Up @@ -305,6 +308,41 @@ public function __construct($datafile, $logger, $errorHandler)
}
}

/**
* Create ProjectConfig based on datafile string.
*
* @param string $datafile JSON string representing the Optimizely project.
* @param bool $skipJsonValidation boolean representing whether JSON schema validation needs to be performed.
* @param LoggerInterface $logger Logger instance
* @param ErrorHandlerInterface $errorHandler ErrorHandler instance.
* @return ProjectConfig ProjectConfig instance or null;
*/
public static function createProjectConfigFromDatafile($datafile, $skipJsonValidation, $logger, $errorHandler)
{
if (!$skipJsonValidation) {
if (!Validator::validateJsonSchema($datafile)) {
$defaultLogger = new DefaultLogger();
$defaultLogger->log(Logger::ERROR, 'Provided "datafile" has invalid schema.');
$logger->log(Logger::ERROR, 'Provided "datafile" has invalid schema.');
return null;
}
}

try {
$config = new DatafileProjectConfig($datafile, $logger, $errorHandler);
} catch (Exception $exception) {
$defaultLogger = new DefaultLogger();
$errorMsg = $exception->getCode() == InvalidDatafileVersionException::class ? $exception->getMessage() : sprintf(Errors::INVALID_FORMAT, 'datafile');
$errorToHandle = $exception->getCode() == InvalidDatafileVersionException::class ? new InvalidDatafileVersionException($errorMsg) : new InvalidInputException($errorMsg);
$defaultLogger->log(Logger::ERROR, $errorMsg);
$logger->log(Logger::ERROR, $errorMsg);
$errorHandler->handleError($errorToHandle);
return null;
}

return $config;
}

/**
* @return string String representing account ID from the datafile.
*/
Expand Down Expand Up @@ -501,7 +539,7 @@ public function getAttribute($attributeKey)

$this->_logger->log(Logger::ERROR, sprintf('Attribute key "%s" is not in datafile.', $attributeKey));
$this->_errorHandler->handleError(new InvalidAttributeException('Provided attribute is not in datafile.'));

return null;
}

Expand Down Expand Up @@ -593,7 +631,7 @@ public function getFeatureVariableFromKey($featureFlagKey, $variableKey)
);
return null;
}

/**
* Determines if given experiment is a feature test.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Optimizely/Entity/FeatureVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,26 @@ public function setDefaultValue($value)
{
$this->_defaultValue = $value;
}

/**
* Returns feature variable method name based on
* feature variable type.
*
* @param String $type Feature variable type.
*/
public static function getFeatureVariableMethodName($type)
{
switch ($type) {
case FeatureVariable::BOOLEAN_TYPE:
return "getFeatureVariableBoolean";
case FeatureVariable::INTEGER_TYPE:
return "getFeatureVariableInteger";
case FeatureVariable::DOUBLE_TYPE:
return "getFeatureVariableDouble";
case FeatureVariable::STRING_TYPE:
return "getFeatureVariableString";
default:
return null;
}
}
}
Loading