Skip to content

Config v2 branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 128 additions & 28 deletions src/Optimizely/Config/DatafileProjectConfig.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright 2019-2021, Optimizely
*
Expand Down Expand Up @@ -93,6 +94,18 @@ class DatafileProjectConfig implements ProjectConfigInterface
*/
private $datafile;

/**
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update header of this file and all others. 2021

* @var string environmentKey of the config.
*/
private $environmentKey;

/**
* @var string sdkKey of the config.
*/
private $sdkKey;



/**
* @var string Revision of the datafile.
*/
Expand Down Expand Up @@ -132,7 +145,7 @@ class DatafileProjectConfig implements ProjectConfigInterface
* @var array Associative array of experiment id to associative array of variation key to variations.
*/
private $_variationKeyMapByExperimentId;

/**
* @var array Associative array of event key to Event(s) in the datafile.
*/
Expand Down Expand Up @@ -172,6 +185,34 @@ class DatafileProjectConfig implements ProjectConfigInterface
*/
private $_rollouts;

/**
* list of Attributes that will be parsed from the datafile
*
* @var [Attribute]
*/
private $attributes;

/**
* list of Audiences that will be parsed from the datafile
*
* @var [Audience]
*/
private $audiences;

/**
* list of Events that will be parsed from the datafile
*
* @var [Event]
*/
private $events;

/**
* list of Typed Audiences that will be parsed from the datafile
*
* @var [typed_audience]
*/
private $typedAudiences;

/**
* internal mapping of feature keys to feature flag models.
*
Expand Down Expand Up @@ -210,10 +251,11 @@ class DatafileProjectConfig implements ProjectConfigInterface
/**
* DatafileProjectConfig constructor to load and set project configuration data.
*
* @param $datafile string JSON string representing the project.
* @param $logger LoggerInterface
* @param $datafile string JSON string representing the project.
* @param $logger LoggerInterface
* @param $errorHandler ErrorHandlerInterface
*/

public function __construct($datafile, $logger, $errorHandler)
{
$supportedVersions = array(self::V2, self::V3, self::V4);
Expand All @@ -222,6 +264,8 @@ public function __construct($datafile, $logger, $errorHandler)
$this->_logger = $logger;
$this->_errorHandler = $errorHandler;
$this->_version = $config['version'];
$this->environmentKey = isset($config['environmentKey']) ? $config['environmentKey'] : '';
$this->sdkKey = isset($config['sdkKey']) ? $config['sdkKey'] : '';
if (!in_array($this->_version, $supportedVersions)) {
throw new InvalidDatafileVersionException(
"This version of the PHP SDK does not support the given datafile version: {$this->_version}."
Expand All @@ -230,19 +274,19 @@ public function __construct($datafile, $logger, $errorHandler)

$this->_accountId = $config['accountId'];
$this->_projectId = $config['projectId'];
$this->_anonymizeIP = isset($config['anonymizeIP'])? $config['anonymizeIP'] : false;
$this->_botFiltering = isset($config['botFiltering'])? $config['botFiltering'] : null;
$this->attributes = isset($config['attributes']) ? $config['attributes'] : [];
$this->audiences = isset($config['audiences']) ? $config['audiences']: [];
$this->events = $config['events'] ?: [];
$this->typedAudiences = isset($config['typedAudiences']) ? $config['typedAudiences'] : [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this can't be $config['typedAudiences'] ?: []

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just followed the existing way to get typedAudiences ealier it was being used as a variable but i made it a class attribute.

$this->_anonymizeIP = isset($config['anonymizeIP']) ? $config['anonymizeIP'] : false;
$this->_botFiltering = isset($config['botFiltering']) ? $config['botFiltering'] : null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the default value, why specifying explicit null.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default value for botFiltering was not mentioned anywhere i did not change this line it is as it was.

$this->_revision = $config['revision'];
$this->_sendFlagDecisions = isset($config['sendFlagDecisions']) ? $config['sendFlagDecisions'] : false;

$groups = $config['groups'] ?: [];
$experiments = $config['experiments'] ?: [];
$events = $config['events'] ?: [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to remove it?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they were being used as variables but i need to get them as attributes so delete the variables and using attributes in further functions

$attributes = $config['attributes'] ?: [];
$audiences = $config['audiences'] ?: [];
$typedAudiences = isset($config['typedAudiences']) ? $config['typedAudiences']: [];
$rollouts = isset($config['rollouts']) ? $config['rollouts'] : [];
$featureFlags = isset($config['featureFlags']) ? $config['featureFlags']: [];
$rollouts = isset($config['rollouts']) ? $config['rollouts']: [];
$featureFlags = isset($config['featureFlags']) ? $config['featureFlags'] : [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid nit changes in parts of code which are not changed. Change it back to what it was.

Copy link
Owner Author

@shujat333 shujat333 Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i started my work before the pr of duplicate exp key issue was merged with master so i just update my branch with the latest master branch to use that instead of doing that task again


// JSON type is represented in datafile as a subtype of string for the sake of backwards compatibility.
// Converting it to a first-class json type while creating Project Config
Expand All @@ -258,10 +302,10 @@ public function __construct($datafile, $logger, $errorHandler)

$this->_groupIdMap = ConfigParser::generateMap($groups, 'id', Group::class);
$this->_experimentIdMap = ConfigParser::generateMap($experiments, 'id', Experiment::class);
$this->_eventKeyMap = ConfigParser::generateMap($events, 'key', Event::class);
$this->_attributeKeyMap = ConfigParser::generateMap($attributes, 'key', Attribute::class);
$typedAudienceIdMap = ConfigParser::generateMap($typedAudiences, 'id', Audience::class);
$this->_audienceIdMap = ConfigParser::generateMap($audiences, 'id', Audience::class);
$this->_eventKeyMap = ConfigParser::generateMap($this->events, 'key', Event::class);
$this->_attributeKeyMap = ConfigParser::generateMap($this->attributes, 'key', Attribute::class);
$typedAudienceIdMap = ConfigParser::generateMap($this->typedAudiences, 'id', Audience::class);
$this->_audienceIdMap = ConfigParser::generateMap($this->audiences, 'id', Audience::class);
$this->_rollouts = ConfigParser::generateMap($rollouts, null, Rollout::class);
$this->_featureFlags = ConfigParser::generateMap($featureFlags, null, FeatureFlag::class);

Expand Down Expand Up @@ -367,10 +411,10 @@ public function __construct($datafile, $logger, $errorHandler)
/**
* Create ProjectConfig based on datafile string.
*
* @param string $datafile JSON string representing the Optimizely project.
* @param bool $skipJsonValidation boolean representing whether JSON schema validation needs to be performed.
* @param LoggerInterface $logger Logger instance
* @param ErrorHandlerInterface $errorHandler ErrorHandler instance.
* @param string $datafile JSON string representing the Optimizely project.
* @param bool $skipJsonValidation boolean representing whether JSON schema validation needs to be performed.
* @param LoggerInterface $logger Logger instance
* @param ErrorHandlerInterface $errorHandler ErrorHandler instance.
* @return ProjectConfig ProjectConfig instance or null;
*/
public static function createProjectConfigFromDatafile($datafile, $skipJsonValidation, $logger, $errorHandler)
Expand Down Expand Up @@ -449,6 +493,22 @@ public function getRevision()
return $this->_revision;
}

/**
* @return string Config environmentKey.
*/
public function getEnvironmentKey()
{
return $this->environmentKey;
}

/**
* @return string Config sdkKey.
*/
public function getSdkKey()
{
return $this->sdkKey;
}

/**
* @return array List of feature flags parsed from the datafile
*/
Expand All @@ -457,6 +517,38 @@ public function getFeatureFlags()
return $this->_featureFlags;
}

/**
* @return array List of attributes parsed from the datafile
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* @return array List of audiences parsed from the datafile
*/
public function getAudiences()
{
return $this->audiences;
}

/**
* @return array List of events parsed from the datafile
*/
public function getEvents()
{
return $this->events;
}

/**
* @return array List of typed audiences parsed from the datafile
*/
public function getTypedAudiences()
{
return $this->typedAudiences;
}

/**
* @return array List of all experiments (including group experiments)
* parsed from the datafile
Expand All @@ -470,9 +562,12 @@ public function getAllExperiments()
$rolloutExperimentIds[] = $experiment->getId();
}
}
return array_filter(array_values($this->_experimentKeyMap), function ($experiment) use ($rolloutExperimentIds) {
return !in_array($experiment->getId(), $rolloutExperimentIds);
});
return array_filter(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't change just for the sake of change. please.

array_values($this->_experimentIdMap),
function ($experiment) use ($rolloutExperimentIds) {
return !in_array($experiment->getId(), $rolloutExperimentIds);
}
);
}

/**
Expand Down Expand Up @@ -634,7 +729,8 @@ public function getAttribute($attributeKey)
*/
public function getVariationFromKey($experimentKey, $variationKey)
{
if (isset($this->_variationKeyMap[$experimentKey])
if (
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change all the extra nit changes you have done back to as it was. make another PR if you want to fix these nits.

isset($this->_variationKeyMap[$experimentKey])
&& isset($this->_variationKeyMap[$experimentKey][$variationKey])
) {
return $this->_variationKeyMap[$experimentKey][$variationKey];
Expand All @@ -654,14 +750,15 @@ public function getVariationFromKey($experimentKey, $variationKey)

/**
* @param $experimentKey string Key for experiment.
* @param $variationId string ID for variation.
* @param $variationId string ID for variation.
*
* @return Variation Entity corresponding to the provided experiment key and variation ID.
* Dummy entity is returned if key or ID is invalid.
*/
public function getVariationFromId($experimentKey, $variationId)
{
if (isset($this->_variationIdMap[$experimentKey])
if (
isset($this->_variationIdMap[$experimentKey])
&& isset($this->_variationIdMap[$experimentKey][$variationId])
) {
return $this->_variationIdMap[$experimentKey][$variationId];
Expand All @@ -688,7 +785,8 @@ public function getVariationFromId($experimentKey, $variationId)
*/
public function getVariationFromIdByExperimentId($experimentId, $variationId)
{
if (isset($this->_variationIdMapByExperimentId[$experimentId])
if (
isset($this->_variationIdMapByExperimentId[$experimentId])
&& isset($this->_variationIdMapByExperimentId[$experimentId][$variationId])
) {
return $this->_variationIdMapByExperimentId[$experimentId][$variationId];
Expand All @@ -715,7 +813,8 @@ public function getVariationFromIdByExperimentId($experimentId, $variationId)
*/
public function getVariationFromKeyByExperimentId($experimentId, $variationKey)
{
if (isset($this->_variationKeyMapByExperimentId[$experimentId])
if (
isset($this->_variationKeyMapByExperimentId[$experimentId])
&& isset($this->_variationKeyMapByExperimentId[$experimentId][$variationKey])
) {
return $this->_variationKeyMapByExperimentId[$experimentId][$variationKey];
Expand Down Expand Up @@ -748,7 +847,8 @@ public function getFeatureVariableFromKey($featureFlagKey, $variableKey)
return null;
}

if (isset($this->_featureFlagVariableMap[$featureFlagKey])
if (
isset($this->_featureFlagVariableMap[$featureFlagKey])
&& isset($this->_featureFlagVariableMap[$featureFlagKey][$variableKey])
) {
return $this->_featureFlagVariableMap[$featureFlagKey][$variableKey];
Expand Down
35 changes: 33 additions & 2 deletions src/Optimizely/Config/ProjectConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,37 @@ public function getBotFiltering();
*/
public function getRevision();

/**
* @return string String represnting environment key of the datafile.
*/
public function getEnvironmentKey();

/**
* @return string String representing sdkkey of the datafile.
*/
public function getSdkKey();

/**
* @return array List of attributes parsed from the datafile
*/
public function getAttributes();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following are not a part of ProjectConfigInterface

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributes, audiences, events, typedAudiences.
check in other sdks, how retrieving arrays.


/**
* @return array List of audiences parsed from the datafile
*/
public function getAudiences();

/**
* @return array List of events parsed from the datafile
*/
public function getEvents();

/**
* @return array List of typed audiences parsed from the datafile
*/
public function getTypedAudiences();


/**
* @return array List of feature flags parsed from the datafile
*/
Expand Down Expand Up @@ -125,7 +156,7 @@ public function getAttribute($attributeKey);

/**
* @param $experimentKey string Key for experiment.
* @param $variationKey string Key for variation.
* @param $variationKey string Key for variation.
*
* @return Variation Entity corresponding to the provided experiment key and variation key.
* Dummy entity is returned if key or ID is invalid.
Expand All @@ -134,7 +165,7 @@ public function getVariationFromKey($experimentKey, $variationKey);

/**
* @param $experimentKey string Key for experiment.
* @param $variationId string ID for variation.
* @param $variationId string ID for variation.
*
* @return Variation Entity corresponding to the provided experiment key and variation ID.
* Dummy entity is returned if key or ID is invalid.
Expand Down
Loading