From 7f8392e2182d618aa7c5a6c16f7bd726298fda0d Mon Sep 17 00:00:00 2001 From: mnoman09 Date: Wed, 22 Dec 2021 18:22:06 +0500 Subject: [PATCH 1/2] - removed flagVariationbyKey from optimizely class - Moves findValidatedForcedDecisin method to decision service to remove cyclic dependency --- .../DecisionService/DecisionService.php | 32 +++++++++++++++++-- src/Optimizely/Optimizely.php | 15 +-------- src/Optimizely/OptimizelyUserContext.php | 19 ----------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/Optimizely/DecisionService/DecisionService.php b/src/Optimizely/DecisionService/DecisionService.php index c8c8a3e0..73dd8154 100644 --- a/src/Optimizely/DecisionService/DecisionService.php +++ b/src/Optimizely/DecisionService/DecisionService.php @@ -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. * @@ -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]; @@ -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) { diff --git a/src/Optimizely/Optimizely.php b/src/Optimizely/Optimizely.php index 179360de..beaf7b5b 100644 --- a/src/Optimizely/Optimizely.php +++ b/src/Optimizely/Optimizely.php @@ -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 { @@ -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); - } } diff --git a/src/Optimizely/OptimizelyUserContext.php b/src/Optimizely/OptimizelyUserContext.php index 2533b029..efad3376 100644 --- a/src/Optimizely/OptimizelyUserContext.php +++ b/src/Optimizely/OptimizelyUserContext.php @@ -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) { From c848d5647bc52ad8075eaecfe6ede0df4123d2c4 Mon Sep 17 00:00:00 2001 From: muhammadnoman Date: Tue, 4 Jan 2022 17:21:07 +0500 Subject: [PATCH 2/2] Updated headers --- src/Optimizely/DecisionService/DecisionService.php | 2 +- src/Optimizely/Optimizely.php | 2 +- src/Optimizely/OptimizelyUserContext.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Optimizely/DecisionService/DecisionService.php b/src/Optimizely/DecisionService/DecisionService.php index 73dd8154..34f86db6 100644 --- a/src/Optimizely/DecisionService/DecisionService.php +++ b/src/Optimizely/DecisionService/DecisionService.php @@ -1,6 +1,6 @@