Skip to content

Commit 00f3c3a

Browse files
refac: Moves findValidatedForcedDecision method to decision service (#238)
* - removed flagVariationbyKey from optimizely class - Moves findValidatedForcedDecisin method to decision service to remove cyclic dependency * Updated headers Co-authored-by: mnoman09 <[email protected]>
1 parent 34e8e66 commit 00f3c3a

File tree

3 files changed

+34
-38
lines changed

3 files changed

+34
-38
lines changed

src/Optimizely/DecisionService/DecisionService.php

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2017-2021, Optimizely Inc and Contributors
3+
* Copyright 2017-2022, Optimizely Inc and Contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -117,6 +117,34 @@ protected function getBucketingId($userId, $userAttributes)
117117
return [ $userId, $decideReasons ];
118118
}
119119

120+
/**
121+
* Finds a validated forced decision.
122+
*
123+
* @param OptimizelyDecisionContext $context containing flag and rule key.
124+
* @param ProjectConfigInterface $projectConfig Optimizely project config
125+
* @param OptimizelyUserContext $user Optimizely user context object.
126+
*
127+
* @return [ variation, array ] variation and decide reasons.
128+
*/
129+
public function findValidatedForcedDecision(OptimizelyDecisionContext $context, ProjectConfigInterface $projectConfig, OptimizelyUserContext $user)
130+
{
131+
$decideReasons = [];
132+
$flagKey = $context->getFlagKey();
133+
$ruleKey = $context->getRuleKey();
134+
$variationKey = $user->findForcedDecision($context);
135+
$variation = null;
136+
if ($variationKey && $projectConfig) {
137+
$variation = $projectConfig->getFlagVariationByKey($flagKey, $variationKey);
138+
if ($variation) {
139+
array_push($decideReasons, 'Decided by forced decision.');
140+
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()));
141+
} else {
142+
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()));
143+
}
144+
}
145+
return [$variation, $decideReasons];
146+
}
147+
120148
/**
121149
* Determine which variation to show the user.
122150
*
@@ -377,7 +405,7 @@ private function getVariationFromExperimentRule(ProjectConfigInterface $projectC
377405
$decideReasons = [];
378406
// check forced-decision first
379407
$context = new OptimizelyDecisionContext($flagKey, $rule->getKey());
380-
list($decisionResponse, $reasons) = $user->findValidatedForcedDecision($context);
408+
list($decisionResponse, $reasons) = $this->findValidatedForcedDecision($context, $projectConfig, $user);
381409
$decideReasons = array_merge($decideReasons, $reasons);
382410
if ($decisionResponse) {
383411
return [$decisionResponse, $decideReasons];
@@ -410,7 +438,7 @@ public function getVariationFromDeliveryRule(ProjectConfigInterface $projectConf
410438
// check forced-decision first
411439
$rule = $rules[$ruleIndex];
412440
$context = new OptimizelyDecisionContext($flagKey, $rule->getKey());
413-
list($forcedDecisionResponse, $reasons) = $user->findValidatedForcedDecision($context);
441+
list($forcedDecisionResponse, $reasons) = $this->findValidatedForcedDecision($context, $projectConfig, $user);
414442

415443
$decideReasons = array_merge($decideReasons, $reasons);
416444
if ($forcedDecisionResponse) {

src/Optimizely/Optimizely.php

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2021, Optimizely
3+
* Copyright 2016-2022, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -355,7 +355,7 @@ public function decide(OptimizelyUserContext $userContext, $key, array $decideOp
355355
$decision = null;
356356
// check forced-decisions first
357357
$context = new OptimizelyDecisionContext($flagKey, $ruleKey);
358-
list($forcedDecisionResponse, $reasons) = $userContext->findValidatedForcedDecision($context);
358+
list($forcedDecisionResponse, $reasons) = $this->_decisionService->findValidatedForcedDecision($context, $config, $userContext);
359359
if ($forcedDecisionResponse) {
360360
$decision = new FeatureDecision(null, $forcedDecisionResponse, FeatureDecision::DECISION_SOURCE_FEATURE_TEST, $decideReasons);
361361
} else {
@@ -1305,17 +1305,4 @@ protected function validateInputs(array $values, $logLevel = Logger::ERROR)
13051305

13061306
return $isValid;
13071307
}
1308-
1309-
/**
1310-
* Gets the variation associated with experiment or rollout in instance of given feature flag key
1311-
*
1312-
* @param string Feature flag key
1313-
* @param string variation key
1314-
*
1315-
* @return Variation / null
1316-
*/
1317-
public function getFlagVariationByKey($flagKey, $variationKey)
1318-
{
1319-
return $this->getConfig()->getFlagVariationByKey($flagKey, $variationKey);
1320-
}
13211308
}

src/Optimizely/OptimizelyUserContext.php

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2021, Optimizely
3+
* Copyright 2021-2022, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -74,25 +74,6 @@ public function removeAllForcedDecisions()
7474
return true;
7575
}
7676

77-
public function findValidatedForcedDecision($context)
78-
{
79-
$decideReasons = [];
80-
$flagKey = $context->getFlagKey();
81-
$ruleKey = $context->getRuleKey();
82-
$variationKey = $this->findForcedDecision($context);
83-
$variation = null;
84-
if ($variationKey) {
85-
$variation = $this->optimizelyClient->getFlagVariationByKey($flagKey, $variationKey);
86-
if ($variation) {
87-
array_push($decideReasons, 'Decided by forced decision.');
88-
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));
89-
} else {
90-
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));
91-
}
92-
}
93-
return [$variation, $decideReasons];
94-
}
95-
9677
private function findExistingRuleAndFlagKey($context)
9778
{
9879
if ($this->forcedDecisions) {

0 commit comments

Comments
 (0)