-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathClient.php
More file actions
207 lines (171 loc) · 5.92 KB
/
Copy pathClient.php
File metadata and controls
207 lines (171 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace DDTrace\FeatureFlags;
use DDTrace\FeatureFlags\Internal\NativeEvaluator;
use DDTrace\Log\Logger as GlobalLogger;
use DDTrace\Log\LoggerInterface;
use DDTrace\Log\NonThrowingLogger;
final class Client
{
private $evaluator;
/** @var LoggerInterface */
private $logger;
private $warnedAboutRuntimeNotReady = false;
public function __construct($logger = null)
{
if ($logger !== null && !$logger instanceof LoggerInterface) {
throw new \InvalidArgumentException('Expected a LoggerInterface instance');
}
$this->evaluator = NativeEvaluator::create();
$this->logger = new NonThrowingLogger($logger ?: GlobalLogger::get());
}
/**
* @return bool
*/
public function getBooleanValue($flagKey, $defaultValue, array $context = array())
{
return $this->getBooleanDetails($flagKey, $defaultValue, $context)->getValue();
}
/**
* @return string
*/
public function getStringValue($flagKey, $defaultValue, array $context = array())
{
return $this->getStringDetails($flagKey, $defaultValue, $context)->getValue();
}
/**
* @return int
*/
public function getIntegerValue($flagKey, $defaultValue, array $context = array())
{
return $this->getIntegerDetails($flagKey, $defaultValue, $context)->getValue();
}
/**
* @return float
*/
public function getFloatValue($flagKey, $defaultValue, array $context = array())
{
return $this->getFloatDetails($flagKey, $defaultValue, $context)->getValue();
}
/**
* @return array<string, mixed>
*/
public function getObjectValue($flagKey, array $defaultValue, array $context = array())
{
return $this->getObjectDetails($flagKey, $defaultValue, $context)->getValue();
}
/**
* @return EvaluationDetails
*/
public function getBooleanDetails($flagKey, $defaultValue, array $context = array())
{
return $this->evaluate($flagKey, EvaluationType::BOOLEAN, $this->expectBoolean($defaultValue), $context);
}
/**
* @return EvaluationDetails
*/
public function getStringDetails($flagKey, $defaultValue, array $context = array())
{
return $this->evaluate($flagKey, EvaluationType::STRING, $this->expectString($defaultValue), $context);
}
/**
* @return EvaluationDetails
*/
public function getIntegerDetails($flagKey, $defaultValue, array $context = array())
{
return $this->evaluate($flagKey, EvaluationType::INTEGER, $this->expectInteger($defaultValue), $context);
}
/**
* @return EvaluationDetails
*/
public function getFloatDetails($flagKey, $defaultValue, array $context = array())
{
return $this->evaluate($flagKey, EvaluationType::FLOAT, $this->expectFloat($defaultValue), $context);
}
/**
* @return EvaluationDetails
*/
public function getObjectDetails($flagKey, array $defaultValue, array $context = array())
{
return $this->evaluate($flagKey, EvaluationType::OBJECT, $defaultValue, $context);
}
private function evaluate($flagKey, $expectedType, $defaultValue, array $context)
{
$flagKey = $this->expectFlagKey($flagKey);
list($targetingKey, $attributes) = $this->normalizeContext($context);
$details = $this->evaluator->evaluate(
$flagKey,
$expectedType,
$defaultValue,
$targetingKey,
$attributes
);
$this->warnIfRuntimeNotReady($details);
return $details;
}
private function normalizeContext(array $context)
{
$targetingKey = null;
if (array_key_exists('targetingKey', $context) && $context['targetingKey'] !== null) {
$targetingKey = (string) $context['targetingKey'];
}
$attributes = array();
if (isset($context['attributes']) && is_array($context['attributes'])) {
foreach ($context['attributes'] as $key => $value) {
if (is_bool($value) || is_int($value) || is_float($value) || is_string($value)) {
$attributes[(string) $key] = $value;
}
}
}
return array($targetingKey, $attributes);
}
private function warnIfRuntimeNotReady(EvaluationDetails $details)
{
if ($this->warnedAboutRuntimeNotReady) {
return;
}
if ($details->getErrorCode() !== EvaluationErrorCode::PROVIDER_NOT_READY) {
return;
}
$message = $details->getErrorMessage();
if (!is_string($message) || $message === '') {
$message = 'Datadog-backed PHP feature flag evaluation is not ready. Returning the default value.';
}
$this->warnedAboutRuntimeNotReady = true;
$this->logger->warning($message);
}
private function expectFlagKey($flagKey)
{
if (!is_string($flagKey) || $flagKey === '') {
throw new \InvalidArgumentException('Feature flag key must be a non-empty string');
}
return $flagKey;
}
private function expectBoolean($value)
{
if (!is_bool($value)) {
throw new \InvalidArgumentException('Boolean flag default value must be a bool');
}
return $value;
}
private function expectString($value)
{
if (!is_string($value)) {
throw new \InvalidArgumentException('String flag default value must be a string');
}
return $value;
}
private function expectInteger($value)
{
if (!is_int($value)) {
throw new \InvalidArgumentException('Integer flag default value must be an int');
}
return $value;
}
private function expectFloat($value)
{
if (!is_int($value) && !is_float($value)) {
throw new \InvalidArgumentException('Float flag default value must be a number');
}
return (float) $value;
}
}