|
10 | 10 |
|
11 | 11 | class EventManager |
12 | 12 | { |
13 | | - private $apiKey; |
14 | | - private $httpClient; |
15 | | - private $options; |
16 | | - private $eventsQueue; |
17 | | - |
18 | | - public function __construct($apiKey, SecureNativeOptions $secureNativeOptions) |
19 | | - { |
20 | | - $this->apiKey = $apiKey; |
21 | | - $this->options = $secureNativeOptions; |
22 | | - $this->httpClient = new HttpClient($this->apiKey, $this->options); |
23 | | - $this->eventsQueue = array(); |
24 | | - } |
25 | | - |
26 | | - public function buildEvent(EventOptions $opts) |
27 | | - { |
28 | | - $cookie = Utils::cookieIdFromRequest() || Utils::securHeaderFromRequest(); |
29 | | - $cookieDecoded = Utils::decrypt($cookie, $this->apiKey); |
30 | | - $clientFP = json_decode($cookieDecoded); |
31 | | - $eventType = $opts->eventType || EventTypes::LOG_IN; |
32 | | - |
33 | | - $cid = $clientFP['cid'] || ''; |
34 | | - $vid = Utils::generateGuidV4(); |
35 | | - $fp = $clientFP['fp'] || ''; |
36 | | - $ip = $opts->ip || Utils::clientIpFromRequest(); |
37 | | - $remoteIP = $opts->remoteIp || Utils::clientIpFromRequest(); |
38 | | - $userAgent = $opts->userAgent || Utils::userAgentFromRequest(); |
39 | | - $user = $opts->user || new User('anonymous'); |
40 | | - $ts = round(microtime(true) * 1000); |
41 | | - $device = $opts->device || null; |
42 | | - $params =$opts->params; |
43 | | - |
44 | | - return new SecurenativeEvent($eventType, $cid, $vid, $fp, $ip, $remoteIP, $userAgent, $user, $ts, $device, $params); |
45 | | - } |
46 | | - |
47 | | - public function sendSync(SecurenativeEvent $event, $requestUrl) |
48 | | - { |
49 | | - try { |
50 | | - $request = new Request('POST', $requestUrl, [], Utils::serialize($event)); |
51 | | - $response = $this->httpClient->send($request); |
52 | | - return json_decode($response>getBody()); |
53 | | - } catch (RequestException $e) { |
54 | | - return null; |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - public function sendAsync(SecurenativeEvent $event, $requestUrl) |
59 | | - { |
60 | | - if (count($this->eventsQueue) >= $this->options->getMaxEvents()) { |
61 | | - array_shift($$this->eventsQueue); |
| 13 | + private $apiKey; |
| 14 | + private $httpClient; |
| 15 | + private $options; |
| 16 | + private $eventsQueue; |
| 17 | + |
| 18 | + public function __construct($apiKey, SecureNativeOptions $secureNativeOptions) |
| 19 | + { |
| 20 | + $this->apiKey = $apiKey; |
| 21 | + $this->options = $secureNativeOptions; |
| 22 | + $this->httpClient = new HttpClient($this->apiKey, $this->options); |
| 23 | + $this->eventsQueue = array(); |
| 24 | + } |
| 25 | + |
| 26 | + public function buildEvent(EventOptions $opts) |
| 27 | + { |
| 28 | + $cookie = Utils::cookieIdFromRequest() ? Utils::cookieIdFromRequest() : Utils::securHeaderFromRequest(); |
| 29 | + $cookieDecoded = Utils::decrypt($cookie, $this->apiKey); |
| 30 | + $clientFP = json_decode($cookieDecoded); |
| 31 | + $eventType = $opts->eventType ? $opts->eventType : EventTypes::LOG_IN; |
| 32 | + |
| 33 | + $cid = $clientFP['cid'] ? $clientFP['cid'] : ''; |
| 34 | + $vid = Utils::generateGuidV4(); |
| 35 | + $fp = $clientFP['fp'] ? $clientFP['fp'] : ''; |
| 36 | + $ip = $opts->ip ? $opts->ip : Utils::clientIpFromRequest(); |
| 37 | + $remoteIP = $opts->remoteIp ? $opts->remoteIp : Utils::clientIpFromRequest(); |
| 38 | + $userAgent = $opts->userAgent ? $opts->userAgent : Utils::userAgentFromRequest(); |
| 39 | + $user = $opts->user ? $opts->user : new User('anonymous'); |
| 40 | + $ts = round(microtime(true) * 1000); |
| 41 | + $device = $opts->device; |
| 42 | + $params = $opts->params; |
| 43 | + |
| 44 | + return new SecurenativeEvent($eventType, $cid, $vid, $fp, $ip, $remoteIP, $userAgent, $user, $ts, $device, $params); |
| 45 | + } |
| 46 | + |
| 47 | + public function sendSync(SecurenativeEvent $event, $requestUrl) |
| 48 | + { |
| 49 | + try { |
| 50 | + $request = new Request('POST', $requestUrl, [], Utils::serialize($event)); |
| 51 | + $response = $this->httpClient->send($request); |
| 52 | + return json_decode($response > getBody()); |
| 53 | + } catch (RequestException $e) { |
| 54 | + return null; |
| 55 | + } |
62 | 56 | } |
63 | 57 |
|
64 | | - // TODO: Is json_encode() correct here? |
65 | | - $request = new Request('POST', $requestUrl, [], json_encode(Utils::serialize($event))); |
| 58 | + public function sendAsync(SecurenativeEvent $event, $requestUrl) |
| 59 | + { |
| 60 | + if (count($this->eventsQueue) >= $this->options->getMaxEvents()) { |
| 61 | + array_shift($$this->eventsQueue); |
| 62 | + } |
| 63 | + |
| 64 | + // TODO: Is json_encode() correct here? |
| 65 | + $request = new Request('POST', $requestUrl, [], Utils::serialize($event)); |
66 | 66 | // $request = new Request('POST', $requestUrl, [], $event); |
67 | 67 |
|
68 | | - array_push($this->eventsQueue, $request); |
69 | | - |
70 | | - $this::sendEvents(); |
71 | | - } |
72 | | - |
73 | | - private function sendEvents() |
74 | | - { |
75 | | - for ($i = 0; $i < count($this->eventsQueue); $i++) { |
76 | | - $request = $this->eventsQueue[$i]; |
77 | | - |
78 | | - $promise = $this->httpClient->sendAsync($request); |
79 | | - $promise->then( |
80 | | - function (ResponseInterface $res) use ($request) { |
81 | | - if (($key = array_search($request, $this->eventsQueue)) !== false) { |
82 | | - unset($this->eventsQueue[$key]); |
83 | | - } |
84 | | - }, |
85 | | - function (RequestException $e) { |
86 | | - |
87 | | - }); |
88 | | - } |
89 | | - } |
90 | | - |
| 68 | + array_push($this->eventsQueue, $request); |
| 69 | + |
| 70 | + $this::sendEvents(); |
| 71 | + } |
| 72 | + |
| 73 | + private function sendEvents() |
| 74 | + { |
| 75 | + for ($i = 0; $i < count($this->eventsQueue); $i++) { |
| 76 | + $request = $this->eventsQueue[$i]; |
| 77 | + |
| 78 | + $promise = $this->httpClient->sendAsync($request); |
| 79 | + $promise->then(function ($res) use ($request) { |
| 80 | + if (($key = array_search($request, $this->eventsQueue)) !== false) { |
| 81 | + unset($this->eventsQueue[$key]); |
| 82 | + } |
| 83 | + }, function (RequestException $e) { |
| 84 | + echo $e->getMessage() . "\n"; |
| 85 | + }); |
| 86 | + $promise->wait(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
91 | 90 | } |
0 commit comments