22
33namespace SecureNative \sdk ;
44
5+ use DateTime ;
6+ use DateTimeZone ;
57use GuzzleHttp \Psr7 \Request ;
68use GuzzleHttp \Exception \RequestException ;
79use phpDocumentor \Reflection \Types \Array_ ;
@@ -23,24 +25,32 @@ public function __construct($apiKey, SecureNativeOptions $secureNativeOptions)
2325
2426 public function buildEvent (EventOptions $ opts )
2527 {
26- $ cookie = Utils:: cookieIdFromRequest () ? Utils:: cookieIdFromRequest () : Utils:: securHeaderFromRequest () ;
28+ $ cookie = $ opts -> context -> clientToken ;
2729 $ cookieDecoded = Utils::decrypt ($ cookie , $ this ->apiKey );
28- $ clientFP = json_decode ($ cookieDecoded );
29- $ eventType = $ opts ->eventType ? $ opts ->eventType : EventTypes::LOG_IN ;
30-
31- $ cid = $ clientFP && $ clientFP ->cid ? $ clientFP ->cid : '' ;
32- $ fp = $ clientFP && $ clientFP ->fp ? $ clientFP ->fp : '' ;
33-
34- $ vid = Utils::generateGuidV4 ();
35- $ ip = $ opts ->ip ? $ opts ->ip : Utils::clientIpFromRequest ();
36- $ remoteIP = $ opts ->remoteIp ? $ opts ->remoteIp : Utils::clientIpFromRequest ();
37- $ userAgent = $ opts ->userAgent ? $ opts ->userAgent : Utils::userAgentFromRequest ();
38- $ user = $ opts ->user ? $ opts ->user : new User ('anonymous ' );
39- $ ts = round (microtime (true ) * 1000 );
40- $ device = $ opts ->device ;
41- $ params = $ opts ->params ;
30+ $ clientToken = json_decode ($ cookieDecoded );
31+
32+ $ rid = Utils::generateGuidV4 ();
33+ $ eventType = $ opts ->event ? $ opts ->event : EventTypes::LOG_IN ;
34+ $ userId = $ opts ->userId ? $ opts ->userId : '' ;
35+ $ userTraits = $ opts ->userTraits ? $ opts ->userTraits : new UserTraits ('anonymous ' );
36+ $ cid = $ clientToken && $ clientToken ->cid ? $ clientToken ->cid : '' ;
37+ $ vid = $ clientToken && $ clientToken ->vid ? $ clientToken ->vid : '' ;
38+ $ fp = $ clientToken && $ clientToken ->fp ? $ clientToken ->fp : '' ;
39+ $ ip = $ opts ->context ->ip ? $ opts ->context ->ip : '' ;
40+ $ remoteIp = $ opts ->context ->remoteIp ? $ opts ->context ->remoteIp : '' ;
41+ $ method = $ opts ->context ->method ? $ opts ->context ->method : '' ;
42+ $ url = $ opts ->context ->url ? $ opts ->context ->url : '' ;
43+ $ headers =$ opts ->context ->headers ? $ opts ->context ->headers : null ;
44+
45+ $ reqCtx = new RequestContext ($ cid , $ vid , $ fp , $ ip , $ remoteIp , $ method , $ url , $ headers );
46+
47+ $ properties = $ opts ->properties ;
48+ $ timestamp = null ;
49+ try {
50+ $ timestamp = $ opts ->timestamp ? $ opts ->timestamp : (new DateTime ("now " , new DateTimeZone ("UTC " )))->format (DateTime::ISO8601 );
51+ } catch (\Exception $ e ) {}
4252
43- $ event = new SecurenativeEvent ($ eventType , $ cid , $ vid , $ fp , $ ip , $ remoteIP , $ userAgent , $ user , $ ts , $ device , $ params );
53+ $ event = new SecurenativeEvent ($ rid , $ eventType , $ userId , $ userTraits , $ reqCtx , $ properties , $ timestamp );
4454
4555 Logger::debug ('Created event ' , $ event );
4656
@@ -56,7 +66,7 @@ public function sendSync(SecurenativeEvent $event, $requestUrl)
5666 Logger::debug ('Successfully sent event ' , $ event );
5767 return json_decode ($ body );
5868 } catch (RequestException $ e ) {
59- Logger::debug ('Failed to send event ' , $ e ->getMessage ());
69+ Logger::error ('Failed to send event ' , $ e ->getMessage ());
6070 return null ;
6171 }
6272 }
@@ -75,7 +85,7 @@ public function sendAsync(SecurenativeEvent $event, $requestUrl)
7585 try {
7686 $ this ::sendEvents ();
7787 } catch (Exception $ e ) {
78- Logger::debug ("Failed to send queue events " , $ e ->getMessage ());
88+ Logger::error ("Failed to send queue events " , $ e ->getMessage ());
7989 return ;
8090 }
8191 }
@@ -90,10 +100,10 @@ private function sendEvents()
90100 if (($ key = array_search ($ request , $ this ->eventsQueue )) !== false ) {
91101 unset($ this ->eventsQueue [$ key ]);
92102 }
93- }, function (RequestException $ e ) {
94- Logger::debug ("Failed to send event request " , $ e ->getMessage ());
103+ }, function (Exception $ e ) {
104+ Logger::error ("Failed to send event request " , $ e ->getMessage ());
95105 });
96- $ promise ->wait ();
106+ $ promise ->wait (false );
97107 }
98108 }
99109
0 commit comments