Skip to content

Commit 5efdd3f

Browse files
rashidspmikeproeng37
authored andcommitted
refact(API): Validates empty user ID as valid. (#141)
1 parent 6fa57fd commit 5efdd3f

File tree

3 files changed

+472
-49
lines changed

3 files changed

+472
-49
lines changed

src/Optimizely/Optimizely.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@ public function getVariation($experimentKey, $userId, $attributes = null)
470470
*/
471471
public function setForcedVariation($experimentKey, $userId, $variationKey)
472472
{
473+
if (!$this->validateInputs(
474+
[
475+
self::EXPERIMENT_KEY =>$experimentKey,
476+
self::USER_ID => $userId
477+
]
478+
)) {
479+
return false;
480+
}
473481
return $this->_config->setForcedVariation($experimentKey, $userId, $variationKey);
474482
}
475483

@@ -483,6 +491,15 @@ public function setForcedVariation($experimentKey, $userId, $variationKey)
483491
*/
484492
public function getForcedVariation($experimentKey, $userId)
485493
{
494+
if (!$this->validateInputs(
495+
[
496+
self::EXPERIMENT_KEY =>$experimentKey,
497+
self::USER_ID => $userId
498+
]
499+
)) {
500+
return null;
501+
}
502+
486503
$forcedVariation = $this->_config->getForcedVariation($experimentKey, $userId);
487504
if (isset($forcedVariation)) {
488505
return $forcedVariation->getKey();
@@ -798,6 +815,15 @@ public function isValid()
798815
protected function validateInputs(array $values, $logLevel = Logger::ERROR)
799816
{
800817
$isValid = true;
818+
if (array_key_exists(self::USER_ID, $values)) {
819+
// Empty str is a valid user ID
820+
if (!is_string($values[self::USER_ID])) {
821+
$this->_logger->log(Logger::ERROR, sprintf(Errors::INVALID_FORMAT, self::USER_ID));
822+
$isValid = false;
823+
}
824+
unset($values[self::USER_ID]);
825+
}
826+
801827
foreach ($values as $key => $value) {
802828
if (!Validator::validateNonEmptyString($value)) {
803829
$isValid = false;

src/Optimizely/ProjectConfig.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,6 @@ public function isVariationIdValid($experimentKey, $variationId)
598598
public function getForcedVariation($experimentKey, $userId)
599599
{
600600

601-
// check for null and empty string user ID
602-
if (strlen($userId) == 0) {
603-
$this->_logger->log(Logger::DEBUG, 'User ID is invalid');
604-
return null;
605-
}
606-
607601
if (!isset($this->_forcedVariationMap[$userId])) {
608602
$this->_logger->log(Logger::DEBUG, sprintf('User "%s" is not in the forced variation map.', $userId));
609603
return null;
@@ -643,11 +637,6 @@ public function getForcedVariation($experimentKey, $userId)
643637
*/
644638
public function setForcedVariation($experimentKey, $userId, $variationKey)
645639
{
646-
// check for null and empty string user ID
647-
if (strlen($userId) == 0) {
648-
$this->_logger->log(Logger::DEBUG, 'User ID is invalid');
649-
return false;
650-
}
651640

652641
// check for empty string Variation key
653642
if (!is_null($variationKey) && !Validator::validateNonEmptyString($variationKey)) {

0 commit comments

Comments
 (0)