2020use Monolog \Logger ;
2121use Optimizely \Bucketer ;
2222use Optimizely \Entity \Experiment ;
23+ use Optimizely \Entity \FeatureFlag ;
24+ use Optimizely \Entity \Rollout ;
2325use Optimizely \Entity \Variation ;
2426use Optimizely \Logger \LoggerInterface ;
2527use Optimizely \ProjectConfig ;
26- use Optimizely \UserProfile \Decision ;
2728use Optimizely \UserProfile \UserProfileServiceInterface ;
2829use Optimizely \UserProfile \UserProfile ;
2930use Optimizely \UserProfile \UserProfileUtils ;
3031use Optimizely \Utils \Validator ;
31- use Optimizely \Entity \FeatureFlag ;
32- use Optimizely \Entity \Rollout ;
3332
3433// This value was decided between App Backend, Audience, and Oasis teams, but may possibly change.
3534// We decided to prefix the reserved keyword with '$' because it is a symbol that is not
4241 *
4342 * The decision service contains all logic around how a user decision is made. This includes all of the following (in order):
4443 * 1. Checking experiment status.
45- * 2. Checking whitelisting.
46- * 3. Check sticky bucketing.
47- * 4. Checking audience targeting.
48- * 5. Using Murmurhash3 to bucket the user.
44+ * 2. Checking force bucketing
45+ * 3. Checking whitelisting.
46+ * 4. Check sticky bucketing.
47+ * 5. Checking audience targeting.
48+ * 6. Using Murmurhash3 to bucket the user.
4949 *
5050 * @package Optimizely
5151 */
@@ -144,13 +144,14 @@ public function getVariation(Experiment $experiment, $userId, $attributes = null
144144 }
145145
146146 /**
147- * Gets the Bucketing Id for Bucketing
147+ * Gets the Bucketing ID for Bucketing
148148 * @param string $userId
149- * @param array $userAttributes
149+ * @param array $userAttributes
150+ *
150151 * @return string
151152 */
152153 private function getBucketingId ($ userId , $ userAttributes ){
153- // by default, the bucketing ID should be the user ID
154+ // By default, the bucketing ID should be the user ID
154155 $ bucketingId = $ userId ;
155156
156157 // If the bucketing ID key is defined in userAttributes, then use that in place of the userID for the murmur hash key
@@ -167,28 +168,26 @@ private function getBucketingId($userId, $userAttributes){
167168 * @param FeatureFlag $featureFlag The feature flag the user wants to access
168169 * @param string $userId user id
169170 * @param array $userAttributes user attributes
170- * @return array/null {"experiment" : Experiment, "variation": Variation } / null
171+ * @return Decision / null
171172 */
172173 public function getVariationForFeature (FeatureFlag $ featureFlag , $ userId , $ userAttributes ){
173174 //Evaluate in this order:
174- //1. Attempt to bucket user into all experiments in the feature flag.
175- //2. Attempt to bucket user into rollout in the feature flag.
175+ //1. Attempt to bucket user into experiment using feature flag.
176+ //2. Attempt to bucket user into rollout using the feature flag.
176177
177178 // Check if the feature flag is under an experiment and the the user is bucketed into one of these experiments
178- $ result = $ this ->getVariationForFeatureExperiment ($ featureFlag , $ userId , $ userAttributes );
179- if ($ result )
180- return $ result ;
179+ $ decision = $ this ->getVariationForFeatureExperiment ($ featureFlag , $ userId , $ userAttributes );
180+ if ($ decision )
181+ return $ decision ;
181182
182183 // Check if the feature flag has rollout and the user is bucketed into one of it's rules
183- $ variation = $ this ->getVariationForFeatureRollout ($ featureFlag , $ userId , $ userAttributes );
184- if ($ variation ){
184+ $ decision = $ this ->getVariationForFeatureRollout ($ featureFlag , $ userId , $ userAttributes );
185+ if ($ decision ){
185186 $ this ->_logger ->log (Logger::INFO ,
186187 "User ' {$ userId }' is bucketed into a rollout for feature flag ' {$ featureFlag ->getKey ()}'. "
187188 );
188189
189- return array (
190- "experiment " => null ,
191- "variation " => $ variation );
190+ return $ decision ;
192191
193192 } else {
194193 $ this ->_logger ->log (Logger::INFO ,
@@ -204,7 +203,7 @@ public function getVariationForFeature(FeatureFlag $featureFlag, $userId, $userA
204203 * @param FeatureFlag $featureFlag The feature flag the user wants to access
205204 * @param string $userId user id
206205 * @param array $userAttributes user userAttributes
207- * @return array/null {"experiment" : Experiment, "variation": Variation } / null
206+ * @return Decision / null
208207 */
209208 public function getVariationForFeatureExperiment (FeatureFlag $ featureFlag , $ userId , $ userAttributes ){
210209 $ feature_flag_key = $ featureFlag ->getKey ();
@@ -229,10 +228,8 @@ public function getVariationForFeatureExperiment(FeatureFlag $featureFlag, $user
229228 if ($ variation instanceof Variation && $ variation != new Variation ){
230229 $ this ->_logger ->log (Logger::INFO ,
231230 "The user ' {$ userId }' is bucketed into experiment ' {$ experiment ->getKey ()}' of feature ' {$ feature_flag_key }'. " );
232- return array (
233- "experiment " => $ experiment ,
234- "variation " => $ variation
235- );
231+
232+ return new Decision ($ experiment ->getId (), $ variation ->getId (), DECISION ::DECISION_SOURCE_EXPERIMENT );
236233 }
237234 }
238235
@@ -249,7 +246,7 @@ public function getVariationForFeatureExperiment(FeatureFlag $featureFlag, $user
249246 * @param FeatureFlag $featureFlag The feature flag the user wants to access
250247 * @param string $userId user id
251248 * @param array $userAttributes user userAttributes
252- * @return Variation/ null
249+ * @return Decision/ null
253250 */
254251 public function getVariationForFeatureRollout (FeatureFlag $ featureFlag , $ userId , $ userAttributes ){
255252 $ bucketing_id = $ this ->getBucketingId ($ userId , $ userAttributes );
@@ -290,7 +287,7 @@ public function getVariationForFeatureRollout(FeatureFlag $featureFlag, $userId
290287 // Evaluate if user satisfies the traffic allocation for this rollout rule
291288 $ variation = $ this ->_bucketer ->bucket ($ this ->_projectConfig , $ experiment , $ bucketing_id , $ userId );
292289 if ($ variation && $ variation != new Variation ()){
293- return $ variation ;
290+ return new Decision ( $ experiment -> getId (), $ variation-> getId (), DECISION :: DECISION_SOURCE_ROLLOUT ) ;
294291 } else {
295292 $ this ->_logger ->log (Logger::DEBUG ,
296293 "User ' {$ userId }' was excluded due to traffic allocation. Checking 'Eveyrone Else' rule now. " );
@@ -302,7 +299,7 @@ public function getVariationForFeatureRollout(FeatureFlag $featureFlag, $userId
302299 $ experiment = $ rolloutRules [sizeof ($ rolloutRules )-1 ];
303300 $ variation = $ this ->_bucketer ->bucket ($ this ->_projectConfig , $ experiment , $ bucketing_id , $ userId );
304301 if ($ variation && $ variation != new Variation ()){
305- return $ variation ;
302+ return new Decision ( $ experiment -> getId (), $ variation-> getId (), DECISION :: DECISION_SOURCE_ROLLOUT ) ;
306303 } else {
307304 $ this ->_logger ->log (Logger::DEBUG ,
308305 "User ' {$ userId }' was excluded from the 'Everyone Else' rule for feature flag " );
@@ -431,7 +428,7 @@ private function saveVariation(Experiment $experiment, Variation $variation, Use
431428 $ decision = $ userProfile ->getDecisionForExperiment ($ experimentId );
432429 $ variationId = $ variation ->getId ();
433430 if (is_null ($ decision )) {
434- $ decision = new Decision ($ variationId );
431+ $ decision = new \ Optimizely \ UserProfile \ Decision ($ variationId );
435432 } else {
436433 $ decision ->setVariationId ($ variationId );
437434 }
0 commit comments