Skip to content
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

feat(OptimizelyConfig): Add new fields to OptimizelyConfig #230

Merged
merged 9 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
106 changes: 97 additions & 9 deletions src/Optimizely/Config/DatafileProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ class DatafileProjectConfig implements ProjectConfigInterface
*/
private $datafile;

/**
* @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 @@ -172,6 +182,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 [Audience]
*/
private $typedAudiences;

/**
* internal mapping of feature keys to feature flag models.
*
Expand Down Expand Up @@ -222,6 +260,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 @@ -237,10 +277,10 @@ public function __construct($datafile, $logger, $errorHandler)

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

Choose a reason for hiding this comment

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

don't understand why these are removed from here. all three.

$attributes = $config['attributes'] ?: [];
$audiences = $config['audiences'] ?: [];
$typedAudiences = isset($config['typedAudiences']) ? $config['typedAudiences']: [];
$this->attributes = isset($config['attributes']) ? $config['attributes'] : [];
$this->audiences = isset($config['audiences']) ? $config['audiences'] : [];
$this->events = isset($config['events']) ? $config['events'] : [];
$this->typedAudiences = isset($config['typedAudiences']) ? $config['typedAudiences'] : [];
$rollouts = isset($config['rollouts']) ? $config['rollouts'] : [];
$featureFlags = isset($config['featureFlags']) ? $config['featureFlags']: [];

Expand All @@ -258,10 +298,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 @@ -449,6 +489,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 +513,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,7 +558,7 @@ public function getAllExperiments()
$rolloutExperimentIds[] = $experiment->getId();
}
}
return array_filter(array_values($this->_experimentKeyMap), function ($experiment) use ($rolloutExperimentIds) {
return array_filter(array_values($this->_experimentIdMap), function ($experiment) use ($rolloutExperimentIds) {
return !in_array($experiment->getId(), $rolloutExperimentIds);
});
}
Expand Down
31 changes: 31 additions & 0 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 representing 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();

/**
* @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
60 changes: 60 additions & 0 deletions src/Optimizely/OptimizelyConfig/OptimizelyAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright 2021, Optimizely Inc and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Optimizely\OptimizelyConfig;

class OptimizelyAttribute implements \JsonSerializable
{
/**
* @var string id representing attribute.
*/
private $id;

/**
* @var string key representing attribute.
*/
private $key;

public function __construct($id, $key)
{
$this->id = $id;
$this->key = $key;
}

/**
* @return string attribute id.
*/
public function getId()
{
return $this->id;
}

/**
* @return string attribute key.
*/
public function getKey()
{
return $this->key;
}

/**
* @return string JSON representation of the object.
*/
public function jsonSerialize()
{
return get_object_vars($this);
}
}
75 changes: 75 additions & 0 deletions src/Optimizely/OptimizelyConfig/OptimizelyAudience.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright 2021, Optimizely Inc and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Optimizely\OptimizelyConfig;

class OptimizelyAudience implements \JsonSerializable
{
/**
* @var string representing audience id.
*/
private $id;

/**
* @var string representing audience name .
*/
private $name;

/**
* @var string conditions representing audience conditions.
*/
private $conditions;


public function __construct($id, $name, $conditions)
{
$this->id = $id;
$this->name = $name;
$this->conditions = $conditions;
}

/**
* @return string audience id.
*/
public function getId()
{
return $this->id;
}

/**
* @return string audience name.
*/
public function getName()
{
return $this->name;
}

/**
* @return string audience conditions.
*/
public function getConditions()
{
return $this->conditions;
}

/**
* @return string JSON representation of the object.
*/
public function jsonSerialize()
{
return get_object_vars($this);
}
}
Loading