-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ForcedDecisions): add forced-decisions APIs to OptimizelyUserContext #233
Changes from 3 commits
8607ff2
e013fea
0b1f517
cd4176c
5c753aa
0fd717b
7e2400f
9cb1d03
ae48a61
6a9a5f0
60ae438
df1c82f
80e5026
979a0e7
d22ad11
a5843c2
f387f89
3557793
8797195
83ef326
b7b980d
280371c
9168430
2bb8937
0df9732
bc3bb03
7566d55
32122b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,77 +35,76 @@ public function __construct(Optimizely $optimizelyClient, $userId, array $attrib | |
$this->forcedDecisions = $forcedDecisions; | ||
} | ||
|
||
public function removeForcedDecision($flagKey, $ruleKey = null) | ||
public function setForcedDecision($flagKey, $ruleKey, $variationKey) | ||
{ | ||
// check if SDK is ready | ||
if (!$this->optimizelyClient->isValid()) { | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add error log message here and below for sdkNotReady? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can not add logging in optimizelyUsercontext in order to do so we have to pass logger over here. |
||
} | ||
if (empty($flagKey)) { | ||
return false; | ||
} | ||
$index = $this->findExistingRuleAndFlagKey($flagKey, $ruleKey); | ||
if ($index != -1) { | ||
array_splice($this->forcedDecisions, $index, 1); | ||
return true; | ||
$this->forcedDecisions[$index]->setVariationKey($variationKey); | ||
} else { | ||
if (!$this->forcedDecisions) { | ||
$this->forcedDecisions = array(); | ||
} | ||
array_push($this->forcedDecisions, new ForcedDecision($flagKey, $ruleKey, $variationKey)); | ||
} | ||
return false; | ||
return true; | ||
} | ||
|
||
public function removeAllForcedDecisions() | ||
public function getForcedDecision($flagKey, $ruleKey = null) | ||
{ | ||
// check if SDK is ready | ||
if (!$this->optimizelyClient->isValid()) { | ||
return false; | ||
return null; | ||
} | ||
|
||
$this->forcedDecisions = []; | ||
return true; | ||
return $this->findForcedDecision($flagKey, $ruleKey); | ||
} | ||
|
||
public function setForcedDecision($flagKey, $variationKey, $ruleKey = null) | ||
public function removeForcedDecision($flagKey, $ruleKey = null) | ||
{ | ||
// check if SDK is ready | ||
if (!$this->optimizelyClient->isValid()) { | ||
return false; | ||
} | ||
if (empty($flagKey)) { | ||
return false; | ||
} | ||
$index = $this->findExistingRuleAndFlagKey($flagKey, $ruleKey); | ||
if ($index != -1) { | ||
$this->forcedDecisions[$index]->setVariationKey($variationKey); | ||
} else { | ||
if (!$this->forcedDecisions) { | ||
$this->forcedDecisions = array(); | ||
} | ||
array_push($this->forcedDecisions, new ForcedDecision($flagKey, $ruleKey, $variationKey)); | ||
array_splice($this->forcedDecisions, $index, 1); | ||
return true; | ||
} | ||
return true; | ||
return false; | ||
} | ||
|
||
public function getForcedDecision($flagKey, $ruleKey = null) | ||
public function removeAllForcedDecisions() | ||
{ | ||
// check if SDK is ready | ||
if (!$this->optimizelyClient->isValid()) { | ||
return null; | ||
return false; | ||
} | ||
return $this->findForcedDecision($flagKey, $ruleKey); | ||
|
||
$this->forcedDecisions = []; | ||
return true; | ||
} | ||
|
||
public function findValidatedForcedDecision($flagKey, $ruleKey, array $options = []) | ||
public function findValidatedForcedDecision($flagKey, $ruleKey) | ||
{ | ||
$decideReasons = []; | ||
$variationKey = $this->findForcedDecision($flagKey, $ruleKey); | ||
$variation = null; | ||
if ($variationKey) { | ||
$variation = $this->optimizelyClient->getFlagVariationByKey($flagKey, $variationKey); | ||
$message = sprintf('Variation "%s" is mapped to %s and user "%s" in the forced decision map.', $variationKey, $ruleKey? 'flag "'.$flagKey.'", rule "'.$ruleKey.'"': 'flag "'.$flagKey.'"', $this->userId); | ||
|
||
$decideReasons[] = $message; | ||
return [$variation, $decideReasons]; | ||
} else { | ||
$message = sprintf('Invalid variation is mapped to %s and user "%s" in the forced decision map.', $ruleKey? 'flag "'.$flagKey.'", rule "'.$ruleKey.'"': 'flag "'.$flagKey.'"', $this->userId); | ||
|
||
$decideReasons[] = $message; | ||
if ($variation) { | ||
array_push($decideReasons, 'Decided by forced decision.'); | ||
array_push($decideReasons, sprintf('Variation "%s" is mapped to %s and user "%s" in the forced decision map.', $variationKey, $ruleKey? 'flag "'.$flagKey.'", rule "'.$ruleKey.'"': 'flag "'.$flagKey.'"', $this->userId)); | ||
} else { | ||
array_push($decideReasons, sprintf('Invalid variation is mapped to %s and user "%s" in the forced decision map.', $ruleKey? 'flag "'.$flagKey.'", rule "'.$ruleKey.'"': 'flag "'.$flagKey.'"', $this->userId)); | ||
} | ||
} | ||
return [null, $decideReasons]; | ||
return [$variation, $decideReasons]; | ||
} | ||
|
||
private function findExistingRuleAndFlagKey($flagKey, $ruleKey) | ||
|
@@ -123,7 +122,10 @@ private function findExistingRuleAndFlagKey($flagKey, $ruleKey) | |
public function findForcedDecision($flagKey, $ruleKey) | ||
{ | ||
$foundVariationKey = null; | ||
if ($this->forcedDecisions && count($this->forcedDecisions) == 0) { | ||
if (!$this->forcedDecisions) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to do this vs. isset? |
||
return null; | ||
} | ||
if (count($this->forcedDecisions) == 0) { | ||
return null; | ||
} | ||
$index = $this->findExistingRuleAndFlagKey($flagKey, $ruleKey); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a static message for this that can be referenced?