-
Notifications
You must be signed in to change notification settings - Fork 3
/
Visitor.php
455 lines (403 loc) · 12.9 KB
/
Visitor.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
namespace Flagship;
use Flagship\Enum\FlagshipConstant;
use Flagship\Enum\FlagshipField;
use Flagship\Hit\HitAbstract;
use Flagship\Model\Modification;
use Flagship\Traits\LogTrait;
use Flagship\Traits\ValidatorTrait;
use Flagship\Utils\ConfigManager;
use JsonSerializable;
/**
* Flagship visitor representation.
*
* @package Flagship
*/
class Visitor implements JsonSerializable
{
use LogTrait;
use ValidatorTrait;
/**
* @var FlagshipConfig
*/
private $config;
/**
* @var string
*/
private $visitorId;
/**
* @var array
*/
private $context = [];
/**
* @var Modification[]
*/
private $modifications = [];
/**
* @var ConfigManager
*/
private $configManager;
/**
* Create a new visitor.
*
* @param ConfigManager $configManager
* @param string $visitorId : visitor unique identifier.
* @param array $context : visitor context. e.g: ["age"=>42, "isVip"=>true, "country"=>"UK"]
*/
public function __construct(ConfigManager $configManager, $visitorId, array $context = [])
{
$this->config = $configManager->getConfig();
$this->setVisitorId($visitorId);
$this->updateContextCollection($context);
$this->configManager = $configManager;
}
/**
* @return ConfigManager
*/
public function getConfigManager()
{
return $this->configManager;
}
/**
* @return string
*/
public function getVisitorId()
{
return $this->visitorId;
}
/**
* @param string $visitorId
* @return Visitor
*/
public function setVisitorId($visitorId)
{
if (empty($visitorId)) {
$this->logError(
$this->config,
FlagshipConstant::VISITOR_ID_ERROR,
[FlagshipConstant::TAG => __FUNCTION__]
);
} else {
$this->visitorId = $visitorId;
}
return $this;
}
/**
* @return array
*/
public function getContext()
{
return $this->context;
}
/**
* Clear the current context and set a new context value
*
* @param array $context : collection of keys, values. e.g: ["age"=>42, "vip"=>true, "country"=>"UK"]
* @return Visitor
*/
public function setContext($context)
{
$this->context = [];
$this->updateContextCollection($context);
return $this;
}
/**
* Update the visitor context values, matching the given keys, used for targeting.
*
* A new context value associated with this key will be created if there is no previous matching value.
* Context key must be String, and value type must be one of the following : Number, Boolean, String.
*
* @param string $key : context key.
* @param numeric|string|bool $value : context value.
*/
public function updateContext($key, $value)
{
if (!$this->isKeyValid($key) || !$this->isValueValid($value)) {
$this->logError(
$this->config,
FlagshipConstant::CONTEXT_PARAM_ERROR,
[FlagshipConstant::TAG => FlagshipConstant::TAG_UPDATE_CONTEXT]
);
return;
}
$this->context[$key] = $value;
}
/**
* Update the visitor context values, matching the given keys, used for targeting.
*
* A new context value associated with this key will be created if there is no previous matching value.
* Context keys must be String, and values types must be one of the following : Number, Boolean, String.
*
* @param array $Context collection of keys, values. e.g: ["age"=>42, "IsVip"=>true, "country"=>"UK"]
*/
public function updateContextCollection(array $Context)
{
foreach ($Context as $itemKey => $item) {
$this->updateContext($itemKey, $item);
}
}
/**
* clear the actual visitor context
* @return void
*/
public function clearContext()
{
$this->context = [];
}
/**
* @return array
*/
public function getModifications()
{
return $this->modifications;
}
/**
* @return FlagshipConfig
*/
public function getConfig()
{
return $this->config;
}
/**
* This function return true if panic mode is enabled, otherwise return false
* Note: Return true if decisionManager is null
* @see hasDecisionManager
* @return bool
*/
private function isOnPanicMode($functionName, $process)
{
if (!$this->hasDecisionManager($process)) {
return true;
}
$check = $this->getConfigManager()->getDecisionManager()->getIsPanicMode();
if ($check) {
$this->logError(
$this->config,
sprintf(FlagshipConstant::PANIC_MODE_ERROR, $functionName),
[FlagshipConstant::TAG => $process]
);
}
return $check;
}
/**
* Retrieve a modification value by its key. If no modification match the given
* key or if the stored value type and default value type do not match, default value will be returned.
*
* @param string $key : key associated to the modification.
* @param string|bool|numeric|array $defaultValue : default value to return.
* @param bool $activate : Set this parameter to true to automatically
* report on our server that the
* current visitor has seen this modification. It is possible to call
* activateModification() later.
* @return string|bool|numeric|array : modification value or default value.
*/
public function getModification($key, $defaultValue, $activate = false)
{
if ($this->isOnPanicMode(__FUNCTION__, FlagshipConstant::TAG_GET_MODIFICATION)) {
return $defaultValue;
}
if (!$this->isKeyValid($key)) {
$this->logError(
$this->config,
sprintf(FlagshipConstant::GET_MODIFICATION_KEY_ERROR, $key),
[FlagshipConstant::TAG => FlagshipConstant::TAG_GET_MODIFICATION]
);
return $defaultValue;
}
$modification = $this->getObjetModification($key);
if (!$modification) {
$this->logError(
$this->config,
sprintf(FlagshipConstant::GET_MODIFICATION_MISSING_ERROR, $key),
[FlagshipConstant::TAG => FlagshipConstant::TAG_GET_MODIFICATION]
);
return $defaultValue;
}
if (gettype($modification->getValue()) !== gettype($defaultValue)) {
$this->logError(
$this->config,
sprintf(FlagshipConstant::GET_MODIFICATION_CAST_ERROR, $key),
[FlagshipConstant::TAG => FlagshipConstant::TAG_GET_MODIFICATION]
);
if (is_null($modification->getValue())) {
$this->activateModification($key);
}
return $defaultValue;
}
if ($activate) {
$this->activateModification($key);
}
return $modification->getValue();
}
/**
* Get the campaign modification information value matching the given key.
*
* @param string $key : key which identify the modification.
* @return array|null
*/
public function getModificationInfo($key)
{
if ($this->isOnPanicMode(__FUNCTION__, FlagshipConstant::TAG_GET_MODIFICATION_INFO)) {
return null;
}
if (!$this->isKeyValid($key)) {
$this->logError(
$this->config,
sprintf(FlagshipConstant::GET_MODIFICATION_ERROR, $key),
[FlagshipConstant::TAG => FlagshipConstant::TAG_GET_MODIFICATION_INFO]
);
return null;
}
$modification = $this->getObjetModification($key);
if (!$modification) {
$this->logError(
$this->config,
sprintf(FlagshipConstant::GET_MODIFICATION_ERROR, $key),
[FlagshipConstant::TAG => FlagshipConstant::TAG_GET_MODIFICATION_INFO]
);
return null;
}
return $this->parseToCampaign($modification);
}
/**
* Build the Campaign of Modification
*
* @param Modification $modification Modification containing information
* @return array JSON encoded string
*/
private function parseToCampaign(Modification $modification)
{
return [
FlagshipField::FIELD_CAMPAIGN_ID => $modification->getCampaignId(),
FlagshipField::FIELD_VARIATION_GROUP_ID => $modification->getVariationGroupId(),
FlagshipField::FIELD_VARIATION_ID => $modification->getVariationId(),
FlagshipField::FIELD_IS_REFERENCE => $modification->getIsReference(),
FlagshipField::FIELD_VALUE => $modification->getValue()
];
}
/**
* Return the Modification that matches the key, otherwise return null
*
* @param $key
* @return Modification|null
*/
private function getObjetModification($key)
{
foreach ($this->modifications as $modification) {
if ($modification->getKey() === $key) {
return $modification;
}
}
return null;
}
/**
* This function return true if decisionManager is not null,
* otherwise log an error and return false
*
* @param string $process : Process name
* @return bool
*/
private function hasDecisionManager($process)
{
if (!$this->getConfigManager()->getDecisionManager()) {
$this->logError(
$this->config,
FlagshipConstant::DECISION_MANAGER_MISSING_ERROR,
[FlagshipConstant::TAG => $process]
);
return false;
}
return true;
}
/**
* This function calls the decision api and update all the campaigns modifications
* from the server according to the visitor context.
* @return void
*/
public function synchronizedModifications()
{
if (!$this->hasDecisionManager(FlagshipConstant::TAG_SYNCHRONIZED_MODIFICATION)) {
return;
}
$this->modifications = $this->getConfigManager()->getDecisionManager()->getCampaignModifications($this);
}
/**
* This function return true if trackingManager is not null,
* otherwise log an error and return false
*
* @return bool
*/
private function hasTrackingManager($process)
{
$check = $this->getConfigManager()->getTrackingManager();
if (!$check) {
$this->logError(
$this->config,
FlagshipConstant::TRACKER_MANAGER_MISSING_ERROR,
[FlagshipConstant::TAG => $process]
);
}
return (bool)$check;
}
/**
* Report this user has seen this modification.
*
* @param $key : key which identify the modification to activate.
* @return void
*/
public function activateModification($key)
{
if ($this->isOnPanicMode(__FUNCTION__, FlagshipConstant::TAG_ACTIVE_MODIFICATION)) {
return ;
}
$modification = $this->getObjetModification($key);
if (!$modification) {
$this->logError(
$this->config,
sprintf(FlagshipConstant::GET_MODIFICATION_ERROR, $key),
[FlagshipConstant::TAG => FlagshipConstant::TAG_ACTIVE_MODIFICATION]
);
return ;
}
if (!$this->hasTrackingManager(FlagshipConstant::TAG_ACTIVE_MODIFICATION)) {
return ;
}
$this->getConfigManager()->getTrackingManager()->sendActive($this, $modification);
}
/**
* Send a Hit to Flagship servers for reporting.
* @param HitAbstract $hit
* @return void
*/
public function sendHit(HitAbstract $hit)
{
if ($this->isOnPanicMode("sendHit", FlagshipConstant::TAG_SEND_HIT)) {
return ;
}
if (!$this->hasTrackingManager(FlagshipConstant::TAG_SEND_HIT)) {
return;
}
$hit->setConfig($this->config)
->setVisitorId($this->getVisitorId())
->setDs(FlagshipConstant::SDK_APP);
if (!$hit->isReady()) {
$this->logError(
$this->config,
$hit->getErrorMessage(),
[FlagshipConstant::TAG => FlagshipConstant::TAG_SEND_HIT]
);
return;
}
$this->getConfigManager()->getTrackingManager()->sendHit($hit);
}
/**
* @inheritDoc
*/
public function jsonSerialize()
{
return [
'visitorId' => $this->getVisitorId(),
'context' => $this->getContext(),
];
}
}