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
26 changes: 26 additions & 0 deletions src/Optimizely/Optimizely.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@ public function getVariation($experimentKey, $userId, $attributes = null)
*/
public function setForcedVariation($experimentKey, $userId, $variationKey)
{
if (!$this->validateInputs(
[
self::EXPERIMENT_KEY =>$experimentKey,
self::USER_ID => $userId
]
)) {
return false;
}
return $this->_config->setForcedVariation($experimentKey, $userId, $variationKey);
}

Expand All @@ -483,6 +491,15 @@ public function setForcedVariation($experimentKey, $userId, $variationKey)
*/
public function getForcedVariation($experimentKey, $userId)
{
if (!$this->validateInputs(
[
self::EXPERIMENT_KEY =>$experimentKey,
self::USER_ID => $userId
]
)) {
return null;
}

$forcedVariation = $this->_config->getForcedVariation($experimentKey, $userId);
if (isset($forcedVariation)) {
return $forcedVariation->getKey();
Expand Down Expand Up @@ -798,6 +815,15 @@ public function isValid()
protected function validateInputs(array $values, $logLevel = Logger::ERROR)
{
$isValid = true;
if (array_key_exists(self::USER_ID, $values)) {
// Empty str is a valid user ID
if (!is_string($values[self::USER_ID])) {
$this->_logger->log(Logger::ERROR, sprintf(Errors::INVALID_FORMAT, self::USER_ID));
$isValid = false;
}
unset($values[self::USER_ID]);
}

foreach ($values as $key => $value) {
if (!Validator::validateNonEmptyString($value)) {
$isValid = false;
Expand Down
11 changes: 0 additions & 11 deletions src/Optimizely/ProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,12 +598,6 @@ public function isVariationIdValid($experimentKey, $variationId)
public function getForcedVariation($experimentKey, $userId)
{

// check for null and empty string user ID
if (strlen($userId) == 0) {
$this->_logger->log(Logger::DEBUG, 'User ID is invalid');
return null;
}

if (!isset($this->_forcedVariationMap[$userId])) {
$this->_logger->log(Logger::DEBUG, sprintf('User "%s" is not in the forced variation map.', $userId));
return null;
Expand Down Expand Up @@ -643,11 +637,6 @@ public function getForcedVariation($experimentKey, $userId)
*/
public function setForcedVariation($experimentKey, $userId, $variationKey)
{
// check for null and empty string user ID
if (strlen($userId) == 0) {
$this->_logger->log(Logger::DEBUG, 'User ID is invalid');
return false;
}

// check for empty string Variation key
if (!is_null($variationKey) && !Validator::validateNonEmptyString($variationKey)) {
Expand Down
Loading