Skip to content

Commit cf2c0b3

Browse files
oakbanialiabbasrizvi
authored andcommitted
feat: OptimizelyConfigService (#188)
1 parent 6a1eec3 commit cf2c0b3

17 files changed

+1640
-11
lines changed

Diff for: phpcs.xml

+1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
<exclude name="PSR2.Classes.PropertyDeclaration.Underscore"/>
2020
<exclude name="PSR1.Classes.ClassDeclaration.MultipleClasses"/>
2121
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/>
22+
<exclude name="Generic.Files.LineEndings.InvalidEOLChar"/>
2223
</rule >
2324
</ruleset>

Diff for: src/Optimizely/Config/DatafileProjectConfig.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2019, Optimizely
3+
* Copyright 2019-2020, 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.
@@ -393,6 +393,15 @@ public function getFeatureFlags()
393393
return $this->_featureFlags;
394394
}
395395

396+
/**
397+
* @return array List of all experiments (including group experiments)
398+
* parsed from the datafile
399+
*/
400+
public function getAllExperiments()
401+
{
402+
return array_values($this->_experimentKeyMap);
403+
}
404+
396405
/**
397406
* @param $groupId string ID of the group.
398407
*

Diff for: src/Optimizely/Config/ProjectConfigInterface.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016, 2018-2019 Optimizely
3+
* Copyright 2016, 2018-2020 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.
@@ -56,6 +56,11 @@ public function getRevision();
5656
*/
5757
public function getFeatureFlags();
5858

59+
/**
60+
* @return array List of all experiments (including group experiments)
61+
*/
62+
public function getAllExperiments();
63+
5964
/**
6065
* @param $groupId string ID of the group.
6166
*

Diff for: src/Optimizely/Entity/Variation.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2019, Optimizely
3+
* Copyright 2016-2020, 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.
@@ -47,7 +47,7 @@ class Variation
4747
private $_variableIdToVariableUsageInstanceMap;
4848

4949

50-
public function __construct($id = null, $key = null, $featureEnabled = false, $variableUsageInstances = [])
50+
public function __construct($id = null, $key = null, $featureEnabled = null, $variableUsageInstances = [])
5151
{
5252
$this->_id = $id;
5353
$this->_key = $key;

Diff for: src/Optimizely/Optimizely.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2019, Optimizely
3+
* Copyright 2016-2020, 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.
@@ -36,6 +36,7 @@
3636
use Optimizely\Logger\NoOpLogger;
3737
use Optimizely\Notification\NotificationCenter;
3838
use Optimizely\Notification\NotificationType;
39+
use Optimizely\OptimizelyConfig\OptimizelyConfigService;
3940
use Optimizely\ProjectConfigManager\ProjectConfigManagerInterface;
4041
use Optimizely\ProjectConfigManager\StaticProjectConfigManager;
4142
use Optimizely\UserProfile\UserProfileServiceInterface;
@@ -266,6 +267,24 @@ public function activate($experimentKey, $userId, $attributes = null)
266267
return $variationKey;
267268
}
268269

270+
/**
271+
* Gets OptimizelyConfig object for the current ProjectConfig.
272+
*
273+
* @return OptimizelyConfig Representing current ProjectConfig.
274+
*/
275+
public function getOptimizelyConfig()
276+
{
277+
$config = $this->getConfig();
278+
if ($config === null) {
279+
$this->_logger->log(Logger::ERROR, sprintf(Errors::INVALID_DATAFILE, __FUNCTION__));
280+
return null;
281+
}
282+
283+
$optConfigService = new OptimizelyConfigService($config);
284+
285+
return $optConfigService->getConfig();
286+
}
287+
269288
/**
270289
* Send conversion event to Optimizely.
271290
*

Diff for: src/Optimizely/OptimizelyConfig/OptimizelyConfig.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright 2020, Optimizely Inc and Contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Optimizely\OptimizelyConfig;
18+
19+
class OptimizelyConfig implements \JsonSerializable
20+
{
21+
/**
22+
* @var string Revision of the config.
23+
*/
24+
private $revision;
25+
26+
/**
27+
* Map of Experiment Keys to OptimizelyExperiments.
28+
*
29+
* @var <String, OptimizelyExperiment> associative array
30+
*/
31+
private $experimentsMap;
32+
33+
/**
34+
* Map of Feature Keys to OptimizelyFeatures.
35+
*
36+
* @var <String, OptimizelyFeature> associative array
37+
*/
38+
private $featuresMap;
39+
40+
public function __construct($revision, array $experimentsMap, array $featuresMap)
41+
{
42+
$this->revision = $revision;
43+
$this->experimentsMap = $experimentsMap;
44+
$this->featuresMap = $featuresMap;
45+
}
46+
47+
/**
48+
* @return string Config revision.
49+
*/
50+
public function getRevision()
51+
{
52+
return $this->revision;
53+
}
54+
55+
/**
56+
* @return array Map of Experiment Keys to OptimizelyExperiments.
57+
*/
58+
public function getExperimentsMap()
59+
{
60+
return $this->experimentsMap;
61+
}
62+
63+
/**
64+
* @return array Map of Feature Keys to OptimizelyFeatures.
65+
*/
66+
public function getFeaturesMap()
67+
{
68+
return $this->featuresMap;
69+
}
70+
71+
/**
72+
* @return string JSON representation of the object.
73+
*/
74+
public function jsonSerialize()
75+
{
76+
return get_object_vars($this);
77+
}
78+
}

0 commit comments

Comments
 (0)