Skip to content

Commit ee38453

Browse files
committed
🖊️ Indentation/Styling
1 parent 50bf45a commit ee38453

File tree

7 files changed

+331
-196
lines changed

7 files changed

+331
-196
lines changed

src/Optimizely/Optimizely.php

Lines changed: 98 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -380,56 +380,56 @@ public function getForcedVariation($experimentKey, $userId)
380380
* @param string Feature flag key
381381
* @param string User ID
382382
* @param array Associative array of user attributes
383-
*
383+
*
384384
* @return boolean
385385
*/
386-
public function isFeatureEnabled($featureFlagKey, $userId, $attributes = null){
387-
386+
public function isFeatureEnabled($featureFlagKey, $userId, $attributes = null)
387+
{
388388
if (!$this->_isValid) {
389389
$this->_logger->log(Logger::ERROR, "Datafile has invalid format. Failing '".__FUNCTION__."'.");
390390
return null;
391391
}
392392

393-
if(!$featureFlagKey){
393+
if (!$featureFlagKey) {
394394
$this->_logger->log(Logger::ERROR, "Feature Flag key cannot be empty.");
395395
return null;
396396
}
397397

398-
if(!$userId){
398+
if (!$userId) {
399399
$this->_logger->log(Logger::ERROR, "User ID cannot be empty.");
400400
return null;
401401
}
402402

403403
$feature_flag = $this->_config->getFeatureFlagFromKey($featureFlagKey);
404-
if($feature_flag == new FeatureFlag){
404+
if ($feature_flag == new FeatureFlag) {
405405
// Error logged in ProjectConfig - getFeatureFlagFromKey
406406
return null;
407407
}
408408

409409
//validate feature flag
410-
if(!Validator::isFeatureFlagValid($this->_config, $feature_flag)){
410+
if (!Validator::isFeatureFlagValid($this->_config, $feature_flag)) {
411411
return null;
412412
}
413413

414414
$decision = $this->_decisionService->getVariationForFeature($feature_flag, $userId, $attributes);
415-
if(!$decision){
416-
$this->_logger->log(Logger::INFO,"Feature Flag '{$featureFlagKey}' is not enabled for user '{$userId}'.");
415+
if (!$decision) {
416+
$this->_logger->log(Logger::INFO, "Feature Flag '{$featureFlagKey}' is not enabled for user '{$userId}'.");
417417
return false;
418418
}
419419

420-
if($decision->getSource() == Decision::DECISION_SOURCE_EXPERIMENT){
420+
if ($decision->getSource() == Decision::DECISION_SOURCE_EXPERIMENT) {
421421
$experiment_id = $decision->getExperimentId();
422422
$variation_id = $decision->getVariationId();
423423
$experiment = $this->_config->getExperimentFromId($experiment_id);
424424
$variation = $this->_config->getVariationFromId($experiment->getKey(), $variation_id);
425425

426426
$this->sendImpressionEvent($experiment->getKey(), $variation->getKey(), $userId, $attributes);
427427
} else {
428-
$this->_logger->log(Logger::INFO,"The user '{$userId}' is not being experimented on Feature Flag '{$featureFlagKey}'.");
428+
$this->_logger->log(Logger::INFO, "The user '{$userId}' is not being experimented on Feature Flag '{$featureFlagKey}'.");
429429
}
430430

431-
$this->_logger->log(Logger::INFO,"Feature Flag '{$featureFlagKey}' is enabled for user '{$userId}'.");
432-
return true;
431+
$this->_logger->log(Logger::INFO, "Feature Flag '{$featureFlagKey}' is enabled for user '{$userId}'.");
432+
return true;
433433
}
434434

435435
/**
@@ -439,66 +439,76 @@ public function isFeatureEnabled($featureFlagKey, $userId, $attributes = null){
439439
* @param string User ID
440440
* @param array Associative array of user attributes
441441
* @param string Variable type
442-
*
442+
*
443443
* @return string Feature variable value / null
444444
*/
445-
public function getFeatureVariableValueForType($featureFlagKey, $variableKey, $userId,
446-
$attributes = null, $variableType = null)
447-
{
448-
if(!$featureFlagKey){
445+
public function getFeatureVariableValueForType(
446+
$featureFlagKey,
447+
$variableKey,
448+
$userId,
449+
$attributes = null,
450+
$variableType = null
451+
) {
452+
if (!$featureFlagKey) {
449453
$this->_logger->log(Logger::ERROR, "Feature Flag key cannot be empty.");
450454
return null;
451455
}
452456

453-
if(!$variableKey){
457+
if (!$variableKey) {
454458
$this->_logger->log(Logger::ERROR, "Variable key cannot be empty.");
455459
return null;
456460
}
457461

458-
if(!$userId){
462+
if (!$userId) {
459463
$this->_logger->log(Logger::ERROR, "User ID cannot be empty.");
460464
return null;
461465
}
462466

463467
$feature_flag = $this->_config->getFeatureFlagFromKey($featureFlagKey);
464-
if($feature_flag == new FeatureFlag){
468+
if ($feature_flag == new FeatureFlag) {
465469
// Error logged in ProjectConfig - getFeatureFlagFromKey
466470
return null;
467471
}
468472

469473
$variable = $this->_config->getFeatureVariableFromKey($featureFlagKey, $variableKey);
470-
if(!$variable){
474+
if (!$variable) {
471475
// Error message logged in ProjectConfig- getFeatureVariableFromKey
472476
return null;
473477
}
474478

475-
if($variableType != $variable->getType()){
479+
if ($variableType != $variable->getType()) {
476480
$this->_logger->log(
477-
Logger::ERROR,"Variable is of type '{$variable->getType()}', but you requested it as type '{$variableType}'.");
481+
Logger::ERROR,
482+
"Variable is of type '{$variable->getType()}', but you requested it as type '{$variableType}'."
483+
);
478484
return null;
479485
}
480486

481487
$decision = $this->_decisionService->getVariationForFeature($feature_flag, $userId, $attributes);
482488
$variable_value = $variable->getDefaultValue();
483489

484-
if(!$decision){
485-
$this->_logger->log(Logger::INFO,"User '{$userId}'is not in any variation, ".
490+
if (!$decision) {
491+
$this->_logger->log(Logger::INFO, "User '{$userId}'is not in any variation, ".
486492
"returning default value '{$variable_value}'.");
487493
} else {
488494
$experiment_id = $decision->getExperimentId();
489495
$variation_id = $decision->getVariationId();
490496
$experiment = $this->_config->getExperimentFromId($experiment_id);
491497
$variation = $this->_config->getVariationFromId($experiment->getKey(), $variation_id);
492498
$variable_usage = $variation->getVariableUsageById($variable->getId());
493-
if($variable_usage){
499+
if ($variable_usage) {
494500
$variable_value = $variable_usage->getValue();
495-
$this->_logger->log(Logger::INFO,
501+
$this->_logger->log(
502+
Logger::INFO,
496503
"Returning variable value '{$variable_value}' for variation '{$variation->getKey()}' ".
497-
"of feature flag '{$featureFlagKey}'");
504+
"of feature flag '{$featureFlagKey}'"
505+
);
498506
} else {
499-
$this->_logger->log(Logger::INFO,
500-
"Variable '{$variableKey}' is not used in variation '{$variation->getKey()}' ".
501-
"returning default value '{$variable_value}'.");
507+
$this->_logger->log(
508+
Logger::INFO,
509+
"Variable '{$variableKey}' is not used in variation '{$variation->getKey()}' ".
510+
"returning default value '{$variable_value}'."
511+
);
502512
}
503513
}
504514

@@ -511,15 +521,22 @@ public function getFeatureVariableValueForType($featureFlagKey, $variableKey, $u
511521
* @param string Variable key
512522
* @param string User ID
513523
* @param array Associative array of user attributes
514-
*
524+
*
515525
* @return string boolean variable value / null
516526
*/
517-
public function getFeatureVariableBoolean($featureFlagKey, $variableKey, $userId, $attributes = null){
527+
public function getFeatureVariableBoolean($featureFlagKey, $variableKey, $userId, $attributes = null)
528+
{
518529
$variable_value = $this->getFeatureVariableValueForType(
519-
$featureFlagKey, $variableKey, $userId, $attributes, FeatureVariable::BOOLEAN_TYPE);
530+
$featureFlagKey,
531+
$variableKey,
532+
$userId,
533+
$attributes,
534+
FeatureVariable::BOOLEAN_TYPE
535+
);
520536

521-
if(!is_null($variable_value))
537+
if (!is_null($variable_value)) {
522538
return VariableTypeUtils::castStringToType($variable_value, FeatureVariable::BOOLEAN_TYPE, $this->_logger);
539+
}
523540

524541
return $variable_value;
525542
}
@@ -530,15 +547,22 @@ public function getFeatureVariableBoolean($featureFlagKey, $variableKey, $userId
530547
* @param string Variable key
531548
* @param string User ID
532549
* @param array Associative array of user attributes
533-
*
550+
*
534551
* @return string integer variable value / null
535552
*/
536-
public function getFeatureVariableInteger($featureFlagKey, $variableKey, $userId, $attributes = null){
553+
public function getFeatureVariableInteger($featureFlagKey, $variableKey, $userId, $attributes = null)
554+
{
537555
$variable_value = $this->getFeatureVariableValueForType(
538-
$featureFlagKey, $variableKey, $userId, $attributes, FeatureVariable::INTEGER_TYPE);
556+
$featureFlagKey,
557+
$variableKey,
558+
$userId,
559+
$attributes,
560+
FeatureVariable::INTEGER_TYPE
561+
);
539562

540-
if(!is_null($variable_value))
563+
if (!is_null($variable_value)) {
541564
return VariableTypeUtils::castStringToType($variable_value, FeatureVariable::INTEGER_TYPE, $this->_logger);
565+
}
542566

543567
return $variable_value;
544568
}
@@ -549,15 +573,22 @@ public function getFeatureVariableInteger($featureFlagKey, $variableKey, $userId
549573
* @param string Variable key
550574
* @param string User ID
551575
* @param array Associative array of user attributes
552-
*
576+
*
553577
* @return string double variable value / null
554578
*/
555-
public function getFeatureVariableDouble($featureFlagKey, $variableKey, $userId, $attributes = null){
579+
public function getFeatureVariableDouble($featureFlagKey, $variableKey, $userId, $attributes = null)
580+
{
556581
$variable_value = $this->getFeatureVariableValueForType(
557-
$featureFlagKey, $variableKey, $userId, $attributes, FeatureVariable::DOUBLE_TYPE);
582+
$featureFlagKey,
583+
$variableKey,
584+
$userId,
585+
$attributes,
586+
FeatureVariable::DOUBLE_TYPE
587+
);
558588

559-
if(!is_null($variable_value))
589+
if (!is_null($variable_value)) {
560590
return VariableTypeUtils::castStringToType($variable_value, FeatureVariable::DOUBLE_TYPE, $this->_logger);
591+
}
561592

562593
return $variable_value;
563594
}
@@ -568,12 +599,18 @@ public function getFeatureVariableDouble($featureFlagKey, $variableKey, $userId,
568599
* @param string Variable key
569600
* @param string User ID
570601
* @param array Associative array of user attributes
571-
*
602+
*
572603
* @return string variable value / null
573604
*/
574-
public function getFeatureVariableString($featureFlagKey, $variableKey, $userId, $attributes = null){
605+
public function getFeatureVariableString($featureFlagKey, $variableKey, $userId, $attributes = null)
606+
{
575607
$variable_value = $this->getFeatureVariableValueForType(
576-
$featureFlagKey, $variableKey, $userId, $attributes, FeatureVariable::STRING_TYPE);
608+
$featureFlagKey,
609+
$variableKey,
610+
$userId,
611+
$attributes,
612+
FeatureVariable::STRING_TYPE
613+
);
577614

578615
return $variable_value;
579616
}
@@ -584,27 +621,32 @@ public function getFeatureVariableString($featureFlagKey, $variableKey, $userId,
584621
* @param string User ID
585622
* @param array Associative array of user attributes
586623
*/
587-
public function sendImpressionEvent($experimentKey, $variationKey, $userId, $attributes){
624+
public function sendImpressionEvent($experimentKey, $variationKey, $userId, $attributes)
625+
{
588626
$impressionEvent = $this->_eventBuilder
589627
->createImpressionEvent($this->_config, $experimentKey, $variationKey, $userId, $attributes);
590628
$this->_logger->log(Logger::INFO, sprintf('Activating user "%s" in experiment "%s".', $userId, $experimentKey));
591629
$this->_logger->log(
592630
Logger::DEBUG,
593-
sprintf('Dispatching impression event to URL %s with params %s.',
594-
$impressionEvent->getUrl(), http_build_query($impressionEvent->getParams())
631+
sprintf(
632+
'Dispatching impression event to URL %s with params %s.',
633+
$impressionEvent->getUrl(),
634+
http_build_query($impressionEvent->getParams())
595635
)
596636
);
597637

598638
try {
599639
$this->_eventDispatcher->dispatchEvent($impressionEvent);
600-
}
601-
catch (Throwable $exception) {
640+
} catch (Throwable $exception) {
602641
$this->_logger->log(Logger::ERROR, sprintf(
603-
'Unable to dispatch impression event. Error %s', $exception->getMessage()));
604-
}
605-
catch (Exception $exception) {
642+
'Unable to dispatch impression event. Error %s',
643+
$exception->getMessage()
644+
));
645+
} catch (Exception $exception) {
606646
$this->_logger->log(Logger::ERROR, sprintf(
607-
'Unable to dispatch impression event. Error %s', $exception->getMessage()));
647+
'Unable to dispatch impression event. Error %s',
648+
$exception->getMessage()
649+
));
608650
}
609651
}
610652
}

src/Optimizely/ProjectConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public function getVariationFromId($experimentKey, $variationId)
457457
public function getFeatureVariableFromKey($featureFlagKey, $variableKey)
458458
{
459459
$feature_flag = $this->getFeatureFlagFromKey($featureFlagKey);
460-
if($feature_flag == new FeatureFlag())
460+
if($feature_flag && !($feature_flag->getKey()))
461461
return null;
462462

463463
if(isset($this->_featureFlagVariableMap[$featureFlagKey]) &&

src/Optimizely/Utils/Validator.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,30 @@ public static function isUserInExperiment($config, $experiment, $userAttributes)
109109
/**
110110
* Checks that if there are more than one experiment IDs
111111
* in the feature flag, they must belong to the same mutex group
112-
*
113-
* @param ProjectConfig $config The project config to verify against
112+
*
113+
* @param ProjectConfig $config The project config to verify against
114114
* @param FeatureFlag $featureFlag The feature to validate
115-
*
115+
*
116116
* @return boolean True if feature flag is valid
117117
*/
118118
public static function isFeatureFlagValid($config, $featureFlag)
119119
{
120120
$experimentIds = $featureFlag->getExperimentIds();
121121

122-
if(empty($experimentIds))
122+
if (empty($experimentIds)) {
123123
return true;
124-
if(sizeof($experimentIds) == 1)
124+
}
125+
if (sizeof($experimentIds) == 1) {
125126
return true;
127+
}
126128

127129
$groupId = $config->getExperimentFromId($experimentIds[0])->getGroupId();
128-
foreach($experimentIds as $id){
130+
foreach ($experimentIds as $id) {
129131
$experiment = $config->getExperimentFromId($id);
130132
$grpId = $experiment->getGroupId();
131-
if($groupId != $grpId)
133+
if ($groupId != $grpId) {
132134
return false;
135+
}
133136
}
134137

135138
return true;

0 commit comments

Comments
 (0)