Skip to content

Commit 330e496

Browse files
oakbanimikeproeng37
authored andcommitted
Add logger to getNumericValue (#95)
1 parent f982e8d commit 330e496

File tree

7 files changed

+46
-33
lines changed

7 files changed

+46
-33
lines changed

src/Optimizely/Event/Builder/EventBuilder.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2017, Optimizely
3+
* Copyright 2016-2018, 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,20 @@ class EventBuilder
5656
'Content-Type' => 'application/json'
5757
];
5858

59+
/**
60+
* @var LoggerInterface Logger for logging messages.
61+
*/
62+
private $_logger;
63+
64+
/**
65+
* Event Builder constructor to set logger
66+
* @param $logger LoggerInterface
67+
*/
68+
public function __construct($logger)
69+
{
70+
$this->_logger = $logger;
71+
}
72+
5973
/**
6074
* Helper function to get parameters common to impression and conversion events.
6175
*
@@ -194,7 +208,7 @@ private function getConversionParams($config, $eventKey, $experimentVariationMap
194208
$singleSnapshot[EVENTS][0][EventTagUtils::REVENUE_EVENT_METRIC_NAME] = $revenue;
195209
}
196210

197-
$eventValue = EventTagUtils::getNumericValue($eventTags);
211+
$eventValue = EventTagUtils::getNumericValue($eventTags, $this->_logger);
198212
if (!is_null($eventValue)) {
199213
$singleSnapshot[EVENTS][0][EventTagUtils::NUMERIC_EVENT_METRIC_NAME] = $eventValue;
200214
}

src/Optimizely/Optimizely.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function __construct(
135135
return;
136136
}
137137

138-
$this->_eventBuilder = new EventBuilder();
138+
$this->_eventBuilder = new EventBuilder($this->_logger);
139139
$this->_decisionService = new DecisionService($this->_logger, $this->_config, $userProfileService);
140140
$this->notificationCenter = new NotificationCenter($this->_logger, $this->_errorHandler);
141141
}

src/Optimizely/Utils/EventTagUtils.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2017, Optimizely
3+
* Copyright 2017-2018, 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.
@@ -62,42 +62,40 @@ public static function getRevenueValue($eventTags)
6262
* The value of 'value' can be a float or a numeric string
6363
*
6464
* @param $eventTags array Representing metadata associated with the event.
65+
* @param $logger instance of LoggerInterface
66+
*
6567
* @return float value of 'value' or null
6668
*/
67-
public static function getNumericValue($eventTags, LoggerInterface $logger = null)
69+
public static function getNumericValue($eventTags, $logger)
6870
{
6971
if (!$eventTags) {
70-
if ($logger) {
71-
$logger->log(Logger::DEBUG, "Event tags is undefined.");
72-
}
72+
$logger->log(Logger::DEBUG, "Event tags is undefined.");
7373
return null;
74-
} elseif (!is_array($eventTags)) {
75-
if ($logger) {
76-
$logger->log(Logger::DEBUG, "Event tags is not a dictionary.");
77-
}
74+
}
75+
76+
if (!is_array($eventTags)) {
77+
$logger->log(Logger::DEBUG, "Event tags is not a dictionary.");
7878
return null;
79-
} elseif (!isset($eventTags[self::NUMERIC_EVENT_METRIC_NAME])) {
80-
if ($logger) {
81-
$logger->log(Logger::DEBUG, "The numeric metric key is not defined in the event tags or is null.");
82-
}
79+
}
80+
81+
if (!isset($eventTags[self::NUMERIC_EVENT_METRIC_NAME])) {
82+
$logger->log(Logger::DEBUG, "The numeric metric key is not defined in the event tags or is null.");
8383
return null;
84-
} elseif (!is_numeric($eventTags[self::NUMERIC_EVENT_METRIC_NAME])) {
85-
if ($logger) {
86-
$logger->log(Logger::DEBUG, "Numeric metric value is not an integer or float, or is not a numeric string.");
87-
}
84+
}
85+
86+
if (!is_numeric($eventTags[self::NUMERIC_EVENT_METRIC_NAME])) {
87+
$logger->log(Logger::DEBUG, "Numeric metric value is not an integer or float, or is not a numeric string.");
8888
return null;
89-
} elseif (is_nan($eventTags[self::NUMERIC_EVENT_METRIC_NAME]) || is_infinite(floatval($eventTags[self::NUMERIC_EVENT_METRIC_NAME]))) {
90-
if ($logger) {
91-
$logger->log(Logger::DEBUG, "Provided numeric value is in an invalid format.");
92-
}
89+
}
90+
91+
if (is_nan($eventTags[self::NUMERIC_EVENT_METRIC_NAME]) || is_infinite(floatval($eventTags[self::NUMERIC_EVENT_METRIC_NAME]))) {
92+
$logger->log(Logger::DEBUG, "Provided numeric value is in an invalid format.");
9393
return null;
9494
}
9595

9696
$rawValue = $eventTags[self::NUMERIC_EVENT_METRIC_NAME];
9797
// # Log the final numeric metric value
98-
if ($logger) {
99-
$logger->log(Logger::INFO, "The numeric metric value {$rawValue} will be sent to results.");
100-
}
98+
$logger->log(Logger::INFO, "The numeric metric value {$rawValue} will be sent to results.");
10199

102100
return floatval($rawValue);
103101
}

tests/EventTests/EventBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function setUp()
4040
$this->testUserId = 'testUserId';
4141
$logger = new NoOpLogger();
4242
$this->config = new ProjectConfig(DATAFILE, $logger, new NoOpErrorHandler());
43-
$this->eventBuilder = new EventBuilder();
43+
$this->eventBuilder = new EventBuilder($logger);
4444
$this->timestamp = time()*1000;
4545
$this->uuid = 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c';
4646
$this->differ = new Differ();

tests/NotificationTests/NotificationCenterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2017, Optimizely
3+
* Copyright 2017-2018, 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.
@@ -125,7 +125,7 @@ function () {
125125
///////////////////////////////////////////////////////////////////////////////////////////////////////
126126
// === should add, log and return notification ID when an object method is passed as an argument === //
127127
///////////////////////////////////////////////////////////////////////////////////////////////////////
128-
$eBuilder = new EventBuilder;
128+
$eBuilder = new EventBuilder(new NoOpLogger());
129129
$callbackInput = array($eBuilder, 'createImpressionEvent');
130130

131131
$this->loggerMock->expects($this->at(0))
@@ -289,7 +289,7 @@ public function testAddNotificationThatAlreadyAddedCallbackIsNotReAdded()
289289
/////////////////////////////////////////////////////////////////////////
290290
// ===== verify that an object method with same body isn't re-added ===== //
291291
/////////////////////////////////////////////////////////////////////////
292-
$eBuilder = new EventBuilder;
292+
$eBuilder = new EventBuilder(new NoOpLogger());
293293
$callbackInput = array($eBuilder, 'createImpressionEvent');
294294

295295
$this->loggerMock->expects($this->at(0))

tests/OptimizelyTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function setUp()
6464

6565
// Mock EventBuilder
6666
$this->eventBuilderMock = $this->getMockBuilder(EventBuilder::class)
67+
->setConstructorArgs(array($this->loggerMock))
6768
->setMethods(array('createImpressionEvent', 'createConversionEvent'))
6869
->getMock();
6970

tests/UtilsTests/EventTagUtilsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2017, Optimizely
3+
* Copyright 2017-2018, 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.
@@ -112,7 +112,7 @@ public function testGetNumericValueWithNonNumericValueTag()
112112

113113
$this->assertNull(EventTagUtils::getNumericValue(array('value' => 'abcd'), $this->loggerMock));
114114
$this->assertNull(EventTagUtils::getNumericValue(array('value' => true), $this->loggerMock));
115-
$this->assertNull(EventTagUtils::getNumericValue(array('value' => array(1, 2, 3),$this->loggerMock)));
115+
$this->assertNull(EventTagUtils::getNumericValue(array('value' => array(1, 2, 3)),$this->loggerMock));
116116
$this->assertNull(EventTagUtils::getNumericValue(array('value' => false), $this->loggerMock));
117117
$this->assertNull(EventTagUtils::getNumericValue(array('value' => array()), $this->loggerMock));
118118
$this->assertNull(EventTagUtils::getNumericValue(array('value' => '1,234'), $this->loggerMock));

0 commit comments

Comments
 (0)