|
1 | 1 | <?php
|
2 | 2 | /**
|
3 |
| - * Copyright 2017-2021, Optimizely Inc and Contributors |
| 3 | + * Copyright 2017-2022, Optimizely Inc and Contributors |
4 | 4 | *
|
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | 6 | * you may not use this file except in compliance with the License.
|
@@ -117,6 +117,34 @@ protected function getBucketingId($userId, $userAttributes)
|
117 | 117 | return [ $userId, $decideReasons ];
|
118 | 118 | }
|
119 | 119 |
|
| 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 | + |
120 | 148 | /**
|
121 | 149 | * Determine which variation to show the user.
|
122 | 150 | *
|
@@ -377,7 +405,7 @@ private function getVariationFromExperimentRule(ProjectConfigInterface $projectC
|
377 | 405 | $decideReasons = [];
|
378 | 406 | // check forced-decision first
|
379 | 407 | $context = new OptimizelyDecisionContext($flagKey, $rule->getKey());
|
380 |
| - list($decisionResponse, $reasons) = $user->findValidatedForcedDecision($context); |
| 408 | + list($decisionResponse, $reasons) = $this->findValidatedForcedDecision($context, $projectConfig, $user); |
381 | 409 | $decideReasons = array_merge($decideReasons, $reasons);
|
382 | 410 | if ($decisionResponse) {
|
383 | 411 | return [$decisionResponse, $decideReasons];
|
@@ -410,7 +438,7 @@ public function getVariationFromDeliveryRule(ProjectConfigInterface $projectConf
|
410 | 438 | // check forced-decision first
|
411 | 439 | $rule = $rules[$ruleIndex];
|
412 | 440 | $context = new OptimizelyDecisionContext($flagKey, $rule->getKey());
|
413 |
| - list($forcedDecisionResponse, $reasons) = $user->findValidatedForcedDecision($context); |
| 441 | + list($forcedDecisionResponse, $reasons) = $this->findValidatedForcedDecision($context, $projectConfig, $user); |
414 | 442 |
|
415 | 443 | $decideReasons = array_merge($decideReasons, $reasons);
|
416 | 444 | if ($forcedDecisionResponse) {
|
|
0 commit comments