1
1
<?php
2
2
/**
3
- * Copyright 2020-2021, Optimizely Inc and Contributors
3
+ * Copyright 2020-2021, 2023 Optimizely Inc and Contributors
4
4
*
5
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
6
* you may not use this file except in compliance with the License.
7
7
* You may obtain a copy of the License at
8
8
*
9
- * http ://www.apache.org/licenses/LICENSE-2.0
9
+ * https ://www.apache.org/licenses/LICENSE-2.0
10
10
*
11
11
* Unless required by applicable law or agreed to in writing, software
12
12
* distributed under the License is distributed on an "AS IS" BASIS,
16
16
*/
17
17
namespace Optimizely \OptimizelyConfig ;
18
18
19
+ use Monolog \Logger ;
19
20
use Optimizely \Config \ProjectConfigInterface ;
20
21
use Optimizely \Entity \Experiment ;
21
22
use Optimizely \Entity \Variation ;
23
+ use Optimizely \Logger \DefaultLogger ;
24
+ use Optimizely \Logger \LoggerInterface ;
22
25
23
26
class OptimizelyConfigService
24
27
{
@@ -73,7 +76,14 @@ class OptimizelyConfigService
73
76
*/
74
77
private $ featKeyOptlyVariableIdVariableMap ;
75
78
76
- public function __construct (ProjectConfigInterface $ projectConfig )
79
+ /**
80
+ * Provided or default logger for logging.
81
+ *
82
+ * @var LoggerInterface $logger
83
+ */
84
+ private readonly LoggerInterface $ logger ;
85
+
86
+ public function __construct (ProjectConfigInterface $ projectConfig , LoggerInterface $ logger = null )
77
87
{
78
88
$ this ->experiments = $ projectConfig ->getAllExperiments ();
79
89
$ this ->featureFlags = $ projectConfig ->getFeatureFlags ();
@@ -82,7 +92,8 @@ public function __construct(ProjectConfigInterface $projectConfig)
82
92
$ this ->environmentKey = $ projectConfig ->getEnvironmentKey ();
83
93
$ this ->sdkKey = $ projectConfig ->getSdkKey ();
84
94
$ this ->projectConfig = $ projectConfig ;
85
-
95
+ $ this ->logger = $ logger ?: new DefaultLogger ();
96
+
86
97
$ this ->createLookupMaps ();
87
98
}
88
99
@@ -258,7 +269,7 @@ protected function getVariablesMap(Experiment $experiment, Variation $variation)
258
269
259
270
// Set default variables for variation.
260
271
$ variablesMap = $ this ->featKeyOptlyVariableKeyVariableMap [$ featureKey ];
261
-
272
+
262
273
// Return default variable values if feature is not enabled.
263
274
if (!$ variation ->getFeatureEnabled ()) {
264
275
return $ variablesMap ;
@@ -267,13 +278,13 @@ protected function getVariablesMap(Experiment $experiment, Variation $variation)
267
278
// Set variation specific value if any.
268
279
foreach ($ variation ->getVariables () as $ variableUsage ) {
269
280
$ id = $ variableUsage ->getId ();
270
-
281
+
271
282
$ optVariable = $ this ->featKeyOptlyVariableIdVariableMap [$ featureKey ][$ id ];
272
-
283
+
273
284
$ key = $ optVariable ->getKey ();
274
285
$ value = $ variableUsage ->getValue ();
275
286
$ type = $ optVariable ->getType ();
276
-
287
+
277
288
$ modifiedOptVariable = new OptimizelyVariable (
278
289
$ id ,
279
290
$ key ,
@@ -287,7 +298,7 @@ protected function getVariablesMap(Experiment $experiment, Variation $variation)
287
298
return $ variablesMap ;
288
299
}
289
300
290
-
301
+
291
302
/**
292
303
* Generates Variations map for the given Experiment.
293
304
*
@@ -301,7 +312,7 @@ protected function getVariationsMap(Experiment $experiment)
301
312
302
313
foreach ($ experiment ->getVariations () as $ variation ) {
303
314
$ variablesMap = $ this ->getVariablesMap ($ experiment , $ variation );
304
-
315
+
305
316
$ variationKey = $ variation ->getKey ();
306
317
$ optVariation = new OptimizelyVariation (
307
318
$ variation ->getId (),
@@ -401,11 +412,17 @@ protected function getExperimentsMaps()
401
412
foreach ($ this ->experiments as $ exp ) {
402
413
$ expId = $ exp ->getId ();
403
414
$ expKey = $ exp ->getKey ();
415
+
404
416
$ audiences = '' ;
405
417
if ($ exp ->getAudienceConditions () != null ) {
406
418
$ audienceConditions = $ exp ->getAudienceConditions ();
407
419
$ audiences = $ this ->getSerializedAudiences ($ audienceConditions );
408
420
}
421
+
422
+ if (array_key_exists ($ expKey , $ experimentsKeyMap )) {
423
+ $ this ->logger ->log (Logger::WARNING , sprintf ('Duplicate experiment keys found in datafile: %s ' , $ expKey ));
424
+ }
425
+
409
426
$ optExp = new OptimizelyExperiment (
410
427
$ expId ,
411
428
$ expKey ,
0 commit comments