11<?php
22/**
3- * Copyright 2020-2021, Optimizely Inc and Contributors
3+ * Copyright 2020-2021, 2023 Optimizely Inc and Contributors
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.
77 * You may obtain a copy of the License at
88 *
9- * http ://www.apache.org/licenses/LICENSE-2.0
9+ * https ://www.apache.org/licenses/LICENSE-2.0
1010 *
1111 * Unless required by applicable law or agreed to in writing, software
1212 * distributed under the License is distributed on an "AS IS" BASIS,
1616 */
1717namespace Optimizely \OptimizelyConfig ;
1818
19+ use Monolog \Logger ;
1920use Optimizely \Config \ProjectConfigInterface ;
2021use Optimizely \Entity \Experiment ;
2122use Optimizely \Entity \Variation ;
23+ use Optimizely \Logger \DefaultLogger ;
24+ use Optimizely \Logger \LoggerInterface ;
2225
2326class OptimizelyConfigService
2427{
@@ -73,7 +76,14 @@ class OptimizelyConfigService
7376 */
7477 private $ featKeyOptlyVariableIdVariableMap ;
7578
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 )
7787 {
7888 $ this ->experiments = $ projectConfig ->getAllExperiments ();
7989 $ this ->featureFlags = $ projectConfig ->getFeatureFlags ();
@@ -82,7 +92,8 @@ public function __construct(ProjectConfigInterface $projectConfig)
8292 $ this ->environmentKey = $ projectConfig ->getEnvironmentKey ();
8393 $ this ->sdkKey = $ projectConfig ->getSdkKey ();
8494 $ this ->projectConfig = $ projectConfig ;
85-
95+ $ this ->logger = $ logger ?: new DefaultLogger ();
96+
8697 $ this ->createLookupMaps ();
8798 }
8899
@@ -258,7 +269,7 @@ protected function getVariablesMap(Experiment $experiment, Variation $variation)
258269
259270 // Set default variables for variation.
260271 $ variablesMap = $ this ->featKeyOptlyVariableKeyVariableMap [$ featureKey ];
261-
272+
262273 // Return default variable values if feature is not enabled.
263274 if (!$ variation ->getFeatureEnabled ()) {
264275 return $ variablesMap ;
@@ -267,13 +278,13 @@ protected function getVariablesMap(Experiment $experiment, Variation $variation)
267278 // Set variation specific value if any.
268279 foreach ($ variation ->getVariables () as $ variableUsage ) {
269280 $ id = $ variableUsage ->getId ();
270-
281+
271282 $ optVariable = $ this ->featKeyOptlyVariableIdVariableMap [$ featureKey ][$ id ];
272-
283+
273284 $ key = $ optVariable ->getKey ();
274285 $ value = $ variableUsage ->getValue ();
275286 $ type = $ optVariable ->getType ();
276-
287+
277288 $ modifiedOptVariable = new OptimizelyVariable (
278289 $ id ,
279290 $ key ,
@@ -287,7 +298,7 @@ protected function getVariablesMap(Experiment $experiment, Variation $variation)
287298 return $ variablesMap ;
288299 }
289300
290-
301+
291302 /**
292303 * Generates Variations map for the given Experiment.
293304 *
@@ -301,7 +312,7 @@ protected function getVariationsMap(Experiment $experiment)
301312
302313 foreach ($ experiment ->getVariations () as $ variation ) {
303314 $ variablesMap = $ this ->getVariablesMap ($ experiment , $ variation );
304-
315+
305316 $ variationKey = $ variation ->getKey ();
306317 $ optVariation = new OptimizelyVariation (
307318 $ variation ->getId (),
@@ -401,11 +412,17 @@ protected function getExperimentsMaps()
401412 foreach ($ this ->experiments as $ exp ) {
402413 $ expId = $ exp ->getId ();
403414 $ expKey = $ exp ->getKey ();
415+
404416 $ audiences = '' ;
405417 if ($ exp ->getAudienceConditions () != null ) {
406418 $ audienceConditions = $ exp ->getAudienceConditions ();
407419 $ audiences = $ this ->getSerializedAudiences ($ audienceConditions );
408420 }
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+
409426 $ optExp = new OptimizelyExperiment (
410427 $ expId ,
411428 $ expKey ,
0 commit comments