@@ -113,8 +113,10 @@ class ProjectConfig
113
113
private $ _errorHandler ;
114
114
115
115
/**
116
- * @var array Associative array of user IDs to an associative array
117
- * of experiments to variations.
116
+ * @var array Associative array of user IDs to an associative array
117
+ * of experiments to variations. This contains all the forced variations
118
+ * set by the user by calling setForcedVariation (it is not the same as the
119
+ * whitelisting forcedVariations data structure in the Experiments class).
118
120
*/
119
121
private $ _forcedVariationMap ;
120
122
@@ -348,23 +350,31 @@ public function isVariationIdValid($experimentKey, $variationId)
348
350
}
349
351
350
352
/**
351
- * Gets the forced variation key for the given user and experiment.
352
- *
353
+ * Gets the forced variation key for the given user and experiment.
354
+ *
353
355
* @param $experimentKey string Key for experiment.
354
- * @param $userId string The user Id.
356
+ * @param $userId string The user Id.
355
357
*
356
- * @return Variation The variation which the given user and experiment should be forced into.
358
+ * @return Variation The variation which the given user and experiment should be forced into.
357
359
*/
358
360
public function getForcedVariation ($ experimentKey , $ userId )
359
361
{
362
+
363
+ // check for null and empty string user ID
364
+ if (strlen ($ userId ) == 0 ) {
365
+ $ this ->_logger ->log (Logger::DEBUG , 'User ID is invalid ' );
366
+ return null ;
367
+ }
368
+
360
369
if (!isset ($ this ->_forcedVariationMap [$ userId ])) {
361
370
$ this ->_logger ->log (Logger::DEBUG , sprintf ('User "%s" is not in the forced variation map. ' , $ userId ));
362
371
return null ;
363
372
}
364
373
365
374
$ experimentToVariationMap = $ this ->_forcedVariationMap [$ userId ];
366
375
$ experimentId = $ this ->getExperimentFromKey ($ experimentKey )->getId ();
367
- if (empty ($ experimentId )) {
376
+ // check for null and empty string experiment ID
377
+ if (strlen ($ experimentId ) == 0 ) {
368
378
// this case is logged in getExperimentFromKey
369
379
return null ;
370
380
}
@@ -375,58 +385,70 @@ public function getForcedVariation($experimentKey, $userId)
375
385
}
376
386
377
387
$ variationId = $ experimentToVariationMap [$ experimentId ];
378
- if (empty ($ variationId )) {
388
+ // check for null and empty string variation ID
389
+ if (strlen ($ variationId ) == 0 ) {
379
390
$ this ->_logger ->log (Logger::DEBUG , sprintf ('No variation mapped to experiment "%s" in the forced variation map. ' , $ experimentKey ));
380
391
return null ;
381
392
}
382
393
383
- $ variationKey = $ this ->getVariationFromId ($ experimentKey , $ variationId )->getKey ();
384
- if (empty ($ variationKey )) {
385
- // this case is logged in getVariationFromKey
394
+ $ variation = $ this ->getVariationFromId ($ experimentKey , $ variationId );
395
+ $ variationKey = $ variation ->getKey ();
396
+ // check if the variation exists in the datafile (a new variation is returned if it is not in the datafile)
397
+ if (strlen ($ variationKey ) == 0 ) {
398
+ // this case is logged in getVariationFromId
386
399
return null ;
387
400
}
388
401
389
402
$ this ->_logger ->log (Logger::DEBUG , sprintf ('Variation "%s" is mapped to experiment "%s" and user "%s" in the forced variation map ' , $ variationKey , $ experimentKey , $ userId ));
390
403
391
- $ variation = $ this ->getVariationFromKey ($ experimentKey , $ variationKey );
392
404
return $ variation ;
393
405
}
394
406
395
407
/**
396
- * Sets an associative array of user IDs to an associative array of experiments
397
- * to forced variations.
398
- *
408
+ * Sets an associative array of user IDs to an associative array of experiments
409
+ * to forced variations.
410
+ *
399
411
* @param $experimentKey string Key for experiment.
400
412
* @param $userId string The user Id.
401
413
* @param $variationKey string Key for variation. If null, then clear the existing experiment-to-variation mapping.
402
414
*
403
- * @return boolean A boolean value that indicates if the set completed successfully.
415
+ * @return boolean A boolean value that indicates if the set completed successfully.
404
416
*/
405
417
public function setForcedVariation ($ experimentKey , $ userId , $ variationKey )
406
418
{
407
- $ experimentId = $ this ->getExperimentFromKey ($ experimentKey )->getId ();
408
- if (empty ($ experimentId )) {
419
+ // check for null and empty string user ID
420
+ if (strlen ($ userId ) == 0 ) {
421
+ $ this ->_logger ->log (Logger::DEBUG , 'User ID is invalid ' );
422
+ return FALSE ;
423
+ }
424
+
425
+ $ experiment = $ this ->getExperimentFromKey ($ experimentKey );
426
+ $ experimentId = $ experiment ->getId ();
427
+ // check if the experiment exists in the datafile (a new experiment is returned if it is not in the datafile)
428
+ if (strlen ($ experimentId ) == 0 ) {
409
429
// this case is logged in getExperimentFromKey
410
430
return FALSE ;
411
431
}
412
432
413
- if (empty ($ variationKey )) {
433
+ // clear the forced variation if the variation key is null
434
+ if (is_null ($ variationKey )) {
414
435
unset($ this ->_forcedVariationMap [$ userId ][$ experimentId ]);
415
436
$ this ->_logger ->log (Logger::DEBUG , sprintf ('Variation mapped to experiment "%s" has been removed for user "%s". ' , $ experimentKey , $ userId ));
416
437
return TRUE ;
417
438
}
418
439
419
- $ variationId = $ this ->getVariationFromKey ($ experimentKey , $ variationKey )->getId ();
420
- if (empty ($ variationId )) {
440
+ $ variation = $ this ->getVariationFromKey ($ experimentKey , $ variationKey );
441
+ $ variationId = $ variation ->getId ();
442
+ // check if the variation exists in the datafile (a new variation is returned if it is not in the datafile)
443
+ if (strlen ($ variationId ) == 0 ) {
421
444
// this case is logged in getVariationFromKey
422
445
return FALSE ;
423
446
}
424
447
425
448
$ this ->_forcedVariationMap [$ userId ][$ experimentId ] = $ variationId ;
426
-
427
449
$ this ->_logger ->log (Logger::DEBUG , sprintf ('Set variation "%s" for experiment "%s" and user "%s" in the forced variation map. ' , $ variationId , $ experimentId , $ userId ));
428
450
429
- return TRUE ;
451
+ return TRUE ;
430
452
}
431
453
432
454
}
0 commit comments