1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SegmentIO\Filters; |
4
|
|
|
|
5
|
|
|
use SegmentIO\Client; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* EnrichmentFilters Class |
9
|
|
|
* |
10
|
|
|
* @author Keith Kirk <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
abstract class EnrichmentFilters |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Generates a UUID v4 as a Message Id |
16
|
|
|
* |
17
|
|
|
* @see https://gist.github.com/dahnielson/508447#file-uuid-php-L74 |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public static function generateMessageId() |
22
|
|
|
{ |
23
|
|
|
return sprintf("%04x%04x-%04x-%04x-%04x-%04x%04x%04x", |
24
|
|
|
// 32 bits for "time_low" |
|
|
|
|
25
|
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
26
|
|
|
|
27
|
|
|
// 16 bits for "time_mid" |
|
|
|
|
28
|
|
|
mt_rand(0, 0xffff), |
29
|
|
|
|
30
|
|
|
// 16 bits for "time_hi_and_version", |
|
|
|
|
31
|
|
|
// four most significant bits holds version number 4 |
32
|
|
|
mt_rand(0, 0x0fff) | 0x4000, |
33
|
|
|
|
34
|
|
|
// 16 bits, 8 bits for "clk_seq_hi_res", |
|
|
|
|
35
|
|
|
// 8 bits for "clk_seq_low", |
|
|
|
|
36
|
|
|
// two most significant bits holds zero and one for variant DCE1.1 |
37
|
|
|
mt_rand(0, 0x3fff) | 0x8000, |
38
|
|
|
|
39
|
|
|
// 48 bits for "node" |
|
|
|
|
40
|
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Generates an ISO 8601 datetime string |
46
|
|
|
* |
47
|
|
|
* @param integer $timestamp Time the Event Occurred (Optional) |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public static function generateISODate($timestamp = null) |
52
|
|
|
{ |
53
|
|
|
$timestamp = !is_null($timestamp) && is_numeric($timestamp) |
54
|
|
|
? $timestamp |
55
|
|
|
: time(); |
56
|
|
|
|
57
|
|
|
return date('c', $timestamp); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Adds MessageIds and ISO 8601 formatted datetime strings to all batched operations |
62
|
|
|
* |
63
|
|
|
* @param array $operations The Batch of operations |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public static function enrichBatchOperations(array $operations = []) |
68
|
|
|
{ |
69
|
|
|
foreach ($operations as &$op) { |
70
|
|
|
$timestamp = isset($op['timestamp']) ? $op['timestamp'] : null; |
71
|
|
|
|
72
|
|
|
$op = array_merge($op, [ |
73
|
|
|
'messageId' => self::generateMessageId(), |
74
|
|
|
'timestamp' => self::generateISODate($timestamp) |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $operations; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the Default Context properties |
83
|
|
|
* |
84
|
|
|
* @param array $context |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public static function generateDefaultContext(array $context = []) |
89
|
|
|
{ |
90
|
|
|
return array_merge($context, [ |
91
|
|
|
'library' => [ |
92
|
|
|
'name' => 'analytics-php-guzzle', |
93
|
|
|
'version' => Client::VERSION |
94
|
|
|
] |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.