Skip to content

refac: Moves findValidatedForcedDecision method to decision service #238

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

Merged
merged 2 commits into from
Jan 6, 2022
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
34 changes: 31 additions & 3 deletions src/Optimizely/DecisionService/DecisionService.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2017-2021, Optimizely Inc and Contributors
* Copyright 2017-2022, Optimizely Inc and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -117,6 +117,34 @@ protected function getBucketingId($userId, $userAttributes)
return [ $userId, $decideReasons ];
}

/**
* Finds a validated forced decision.
*
* @param OptimizelyDecisionContext $context containing flag and rule key.
* @param ProjectConfigInterface $projectConfig Optimizely project config
* @param OptimizelyUserContext $user Optimizely user context object.
*
* @return [ variation, array ] variation and decide reasons.
*/
public function findValidatedForcedDecision(OptimizelyDecisionContext $context, ProjectConfigInterface $projectConfig, OptimizelyUserContext $user)
{
$decideReasons = [];
$flagKey = $context->getFlagKey();
$ruleKey = $context->getRuleKey();
$variationKey = $user->findForcedDecision($context);
$variation = null;
if ($variationKey && $projectConfig) {
$variation = $projectConfig->getFlagVariationByKey($flagKey, $variationKey);
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.')', $user->getUserId()));
} 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.')', $user->getUserId()));
}
}
return [$variation, $decideReasons];
}

/**
* Determine which variation to show the user.
*
Expand Down Expand Up @@ -377,7 +405,7 @@ private function getVariationFromExperimentRule(ProjectConfigInterface $projectC
$decideReasons = [];
// check forced-decision first
$context = new OptimizelyDecisionContext($flagKey, $rule->getKey());
list($decisionResponse, $reasons) = $user->findValidatedForcedDecision($context);
list($decisionResponse, $reasons) = $this->findValidatedForcedDecision($context, $projectConfig, $user);
$decideReasons = array_merge($decideReasons, $reasons);
if ($decisionResponse) {
return [$decisionResponse, $decideReasons];
Expand Down Expand Up @@ -410,7 +438,7 @@ public function getVariationFromDeliveryRule(ProjectConfigInterface $projectConf
// check forced-decision first
$rule = $rules[$ruleIndex];
$context = new OptimizelyDecisionContext($flagKey, $rule->getKey());
list($forcedDecisionResponse, $reasons) = $user->findValidatedForcedDecision($context);
list($forcedDecisionResponse, $reasons) = $this->findValidatedForcedDecision($context, $projectConfig, $user);

$decideReasons = array_merge($decideReasons, $reasons);
if ($forcedDecisionResponse) {
Expand Down
17 changes: 2 additions & 15 deletions src/Optimizely/Optimizely.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2021, Optimizely
* Copyright 2016-2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -355,7 +355,7 @@ public function decide(OptimizelyUserContext $userContext, $key, array $decideOp
$decision = null;
// check forced-decisions first
$context = new OptimizelyDecisionContext($flagKey, $ruleKey);
list($forcedDecisionResponse, $reasons) = $userContext->findValidatedForcedDecision($context);
list($forcedDecisionResponse, $reasons) = $this->_decisionService->findValidatedForcedDecision($context, $config, $userContext);
if ($forcedDecisionResponse) {
$decision = new FeatureDecision(null, $forcedDecisionResponse, FeatureDecision::DECISION_SOURCE_FEATURE_TEST, $decideReasons);
} else {
Expand Down Expand Up @@ -1305,17 +1305,4 @@ protected function validateInputs(array $values, $logLevel = Logger::ERROR)

return $isValid;
}

/**
* Gets the variation associated with experiment or rollout in instance of given feature flag key
*
* @param string Feature flag key
* @param string variation key
*
* @return Variation / null
*/
public function getFlagVariationByKey($flagKey, $variationKey)
{
return $this->getConfig()->getFlagVariationByKey($flagKey, $variationKey);
}
}
21 changes: 1 addition & 20 deletions src/Optimizely/OptimizelyUserContext.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2021, Optimizely
* Copyright 2021-2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -74,25 +74,6 @@ public function removeAllForcedDecisions()
return true;
}

public function findValidatedForcedDecision($context)
{
$decideReasons = [];
$flagKey = $context->getFlagKey();
$ruleKey = $context->getRuleKey();
$variationKey = $this->findForcedDecision($context);
$variation = null;
if ($variationKey) {
$variation = $this->optimizelyClient->getFlagVariationByKey($flagKey, $variationKey);
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 [$variation, $decideReasons];
}

private function findExistingRuleAndFlagKey($context)
{
if ($this->forcedDecisions) {
Expand Down