11<?php
22/**
3- * Copyright 2016-2020 , Optimizely
3+ * Copyright 2016-2021 , 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.
@@ -110,23 +110,26 @@ protected function generateBucketValue($bucketingKey)
110110 * @param $parentId mixed ID representing Experiment or Group.
111111 * @param $trafficAllocations array Traffic allocations for variation or experiment.
112112 *
113- * @return string ID representing experiment or variation.
113+ * @return [ string, array ] ID representing experiment or variation and array of log messages representing decision making .
114114 */
115115 private function findBucket ($ bucketingId , $ userId , $ parentId , $ trafficAllocations )
116116 {
117+ $ decideReasons = [];
117118 // Generate the bucketing key based on combination of user ID and experiment ID or group ID.
118119 $ bucketingKey = $ bucketingId .$ parentId ;
119120 $ bucketingNumber = $ this ->generateBucketValue ($ bucketingKey );
120- $ this ->_logger ->log (Logger::DEBUG , sprintf ('Assigned bucket %s to user "%s" with bucketing ID "%s". ' , $ bucketingNumber , $ userId , $ bucketingId ));
121+ $ message = sprintf ('Assigned bucket %s to user "%s" with bucketing ID "%s". ' , $ bucketingNumber , $ userId , $ bucketingId );
122+ $ this ->_logger ->log (Logger::DEBUG , $ message );
123+ $ decideReasons [] = $ message ;
121124
122125 foreach ($ trafficAllocations as $ trafficAllocation ) {
123126 $ currentEnd = $ trafficAllocation ->getEndOfRange ();
124127 if ($ bucketingNumber < $ currentEnd ) {
125- return $ trafficAllocation ->getEntityId ();
128+ return [ $ trafficAllocation ->getEntityId (), $ decideReasons ] ;
126129 }
127130 }
128131
129- return null ;
132+ return [ null , $ decideReasons ] ;
130133 }
131134
132135 /**
@@ -137,12 +140,14 @@ private function findBucket($bucketingId, $userId, $parentId, $trafficAllocation
137140 * @param $bucketingId string A customer-assigned value used to create the key for the murmur hash.
138141 * @param $userId string User identifier.
139142 *
140- * @return Variation Variation which will be shown to the user.
143+ * @return [ Variation, array ] Variation which will be shown to the user and array of log messages representing decision making .
141144 */
142145 public function bucket (ProjectConfigInterface $ config , Experiment $ experiment , $ bucketingId , $ userId )
143146 {
147+ $ decideReasons = [];
148+
144149 if (is_null ($ experiment ->getKey ())) {
145- return null ;
150+ return [ null , $ decideReasons ] ;
146151 }
147152
148153 // Determine if experiment is in a mutually exclusive group.
@@ -151,47 +156,52 @@ public function bucket(ProjectConfigInterface $config, Experiment $experiment, $
151156 $ group = $ config ->getGroup ($ experiment ->getGroupId ());
152157
153158 if (is_null ($ group ->getId ())) {
154- return null ;
159+ return [ null , $ decideReasons ] ;
155160 }
156161
157- $ userExperimentId = $ this ->findBucket ($ bucketingId , $ userId , $ group ->getId (), $ group ->getTrafficAllocation ());
162+ list ($ userExperimentId , $ reasons ) = $ this ->findBucket ($ bucketingId , $ userId , $ group ->getId (), $ group ->getTrafficAllocation ());
163+ $ decideReasons = array_merge ($ decideReasons , $ reasons );
164+
158165 if (empty ($ userExperimentId )) {
159- $ this ->_logger ->log (Logger::INFO , sprintf ('User "%s" is in no experiment. ' , $ userId ));
160- return null ;
166+ $ message = sprintf ('User "%s" is in no experiment. ' , $ userId );
167+ $ this ->_logger ->log (Logger::INFO , $ message );
168+ $ decideReasons [] = $ message ;
169+ return [ null , $ decideReasons ];
161170 }
162171
163172 if ($ userExperimentId != $ experiment ->getId ()) {
164- $ this ->_logger ->log (
165- Logger::INFO ,
166- sprintf (
167- 'User "%s" is not in experiment %s of group %s. ' ,
168- $ userId ,
169- $ experiment ->getKey (),
170- $ experiment ->getGroupId ()
171- )
172- );
173- return null ;
174- }
175-
176- $ this ->_logger ->log (
177- Logger::INFO ,
178- sprintf (
179- 'User "%s" is in experiment %s of group %s. ' ,
173+ $ message = sprintf (
174+ 'User "%s" is not in experiment %s of group %s. ' ,
180175 $ userId ,
181176 $ experiment ->getKey (),
182177 $ experiment ->getGroupId ()
183- )
178+ );
179+
180+ $ this ->_logger ->log (Logger::INFO , $ message );
181+ $ decideReasons [] = $ message ;
182+ return [ null , $ decideReasons ];
183+ }
184+
185+ $ message = sprintf (
186+ 'User "%s" is in experiment %s of group %s. ' ,
187+ $ userId ,
188+ $ experiment ->getKey (),
189+ $ experiment ->getGroupId ()
184190 );
191+
192+ $ this ->_logger ->log (Logger::INFO , $ message );
193+ $ decideReasons [] = $ message ;
185194 }
186195
187196 // Bucket user if not in whitelist and in group (if any).
188- $ variationId = $ this ->findBucket ($ bucketingId , $ userId , $ experiment ->getId (), $ experiment ->getTrafficAllocation ());
197+ list ($ variationId , $ reasons ) = $ this ->findBucket ($ bucketingId , $ userId , $ experiment ->getId (), $ experiment ->getTrafficAllocation ());
198+ $ decideReasons = array_merge ($ decideReasons , $ reasons );
189199 if (!empty ($ variationId )) {
190200 $ variation = $ config ->getVariationFromId ($ experiment ->getKey (), $ variationId );
191201
192- return $ variation ;
202+ return [ $ variation, $ decideReasons ] ;
193203 }
194204
195- return null ;
205+ return [ null , $ decideReasons ] ;
196206 }
197207}
0 commit comments