-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathUnleashContext.php
executable file
·198 lines (164 loc) · 5.04 KB
/
UnleashContext.php
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
<?php
namespace Unleash\Client\Configuration;
use DateTimeImmutable;
use DateTimeInterface;
use Override;
use Unleash\Client\Enum\ContextField;
use Unleash\Client\Enum\Stickiness;
use Unleash\Client\Exception\InvalidValueException;
final class UnleashContext implements Context
{
/**
* @param array<string,string> $customContext
*/
public function __construct(
private ?string $currentUserId = null,
private ?string $ipAddress = null,
private ?string $sessionId = null,
private array $customContext = [],
?string $hostname = null,
private ?string $environment = null,
DateTimeInterface|string|null $currentTime = null,
) {
$this->setHostname($hostname);
$this->setCurrentTime($currentTime);
}
#[Override]
public function getCurrentUserId(): ?string
{
return $this->currentUserId;
}
#[Override]
public function getEnvironment(): ?string
{
return $this->environment;
}
#[Override]
public function getIpAddress(): ?string
{
return $this->ipAddress ?? $_SERVER['REMOTE_ADDR'] ?? null;
}
#[Override]
public function getSessionId(): ?string
{
return $this->sessionId ?? (session_id() ?: null);
}
#[Override]
public function getCustomProperty(string $name): string
{
if (!array_key_exists($name, $this->customContext)) {
throw new InvalidValueException("The custom context value '{$name}' does not exist");
}
return $this->customContext[$name];
}
#[Override]
public function setCustomProperty(string $name, ?string $value): self
{
$this->customContext[$name] = $value ?? '';
return $this;
}
#[Override]
public function hasCustomProperty(string $name): bool
{
return array_key_exists($name, $this->customContext);
}
#[Override]
public function removeCustomProperty(string $name, bool $silent = true): self
{
if (!$this->hasCustomProperty($name) && !$silent) {
throw new InvalidValueException("The custom context value '{$name}' does not exist");
}
unset($this->customContext[$name]);
return $this;
}
#[Override]
public function setCurrentUserId(?string $currentUserId): self
{
$this->currentUserId = $currentUserId;
return $this;
}
#[Override]
public function setIpAddress(?string $ipAddress): self
{
$this->ipAddress = $ipAddress;
return $this;
}
#[Override]
public function setSessionId(?string $sessionId): self
{
$this->sessionId = $sessionId;
return $this;
}
#[Override]
public function setEnvironment(?string $environment): self
{
$this->environment = $environment;
return $this;
}
#[Override]
public function getHostname(): ?string
{
return $this->findContextValue(ContextField::HOSTNAME) ?? (gethostname() ?: null);
}
#[Override]
public function setHostname(?string $hostname): self
{
if ($hostname === null) {
$this->removeCustomProperty(ContextField::HOSTNAME);
} else {
$this->setCustomProperty(ContextField::HOSTNAME, $hostname);
}
return $this;
}
/**
* @param array<string> $values
*/
#[Override]
public function hasMatchingFieldValue(string $fieldName, array $values): bool
{
$fieldValue = $this->findContextValue($fieldName);
if ($fieldValue === null) {
return false;
}
return in_array($fieldValue, $values, true);
}
#[Override]
public function findContextValue(string $fieldName): ?string
{
return match ($fieldName) {
ContextField::USER_ID, Stickiness::USER_ID => $this->getCurrentUserId(),
ContextField::SESSION_ID, Stickiness::SESSION_ID => $this->getSessionId(),
ContextField::IP_ADDRESS => $this->getIpAddress(),
ContextField::ENVIRONMENT => $this->getEnvironment(),
ContextField::CURRENT_TIME => $this->getCurrentTime()->format(DateTimeInterface::ISO8601),
default => $this->customContext[$fieldName] ?? null,
};
}
#[Override]
public function getCurrentTime(): DateTimeInterface
{
if (!$this->hasCustomProperty('currentTime')) {
return new DateTimeImmutable();
}
return new DateTimeImmutable($this->getCustomProperty('currentTime'));
}
#[Override]
public function setCurrentTime(DateTimeInterface|string|null $time): self
{
if ($time === null) {
$this->removeCustomProperty('currentTime');
} else {
$value = is_string($time) ? $time : $time->format(DateTimeInterface::ISO8601);
$this->setCustomProperty('currentTime', $value);
}
return $this;
}
/**
* @return array<string, string>
*/
#[Override]
public function getCustomProperties(): array
{
return $this->customContext;
}
}