@@ -63,14 +63,14 @@ private function getMaxRequests(ResponseInterface $response): int
6363 {
6464 $ values = $ response ->getHeader ("x-ratelimit-limit " );
6565
66- return count ($ values ) > 0 ? (int ) $ values [0 ] : 0 ;
66+ return count ($ values ) > 0 ? (int )$ values [0 ] : 0 ;
6767 }
6868
6969 private function getRemainingRequests (ResponseInterface $ response ): int
7070 {
7171 $ values = $ response ->getHeader ("x-ratelimit-remaining " );
7272
73- return count ($ values ) > 0 ? (int ) $ values [0 ] : 0 ;
73+ return count ($ values ) > 0 ? (int )$ values [0 ] : 0 ;
7474 }
7575
7676 private function check (ResponseInterface $ response )
@@ -191,6 +191,17 @@ public function getTrackingSnippet(string $domain): CallResult
191191 );
192192 }
193193
194+ if ($ response ->getStatusCode () === 404 ) {
195+ return new CallResult (
196+ false ,
197+ false ,
198+ $ this ->getRemainingRequests ($ response ),
199+ $ this ->getMaxRequests ($ response ),
200+ $ json ['message ' ] ? [$ json ['message ' ]] : [],
201+ null
202+ );
203+ }
204+
194205 return new CallResult (
195206 false ,
196207 false ,
@@ -201,16 +212,50 @@ public function getTrackingSnippet(string $domain): CallResult
201212 );
202213 }
203214
215+ private function userIdentifiersToArray (UserIdentified $ user ): array
216+ {
217+ $ result = [];
218+
219+ $ userId = $ user ->getUserId ();
220+ if ($ userId ) {
221+ $ result ["userId " ] = $ userId ;
222+ }
223+
224+ $ email = $ user ->getEmail ();
225+ if ($ email ) {
226+ $ result ["email " ] = $ email ;
227+ }
228+
229+ return $ result ;
230+ }
231+
232+ private function accountIdentifiersToArray (AccountIdentified $ account ): array
233+ {
234+ $ result = [];
235+
236+ $ accountId = $ account ->getAccountId ();
237+ if ($ accountId ) {
238+ $ result ["accountId " ] = $ accountId ;
239+ }
240+
241+ $ domain = $ account ->getDomain ();
242+ if ($ domain ) {
243+ $ result ["domain " ] = $ domain ;
244+ }
245+
246+ return $ result ;
247+ }
248+
204249 public function addEvent (Event $ event ): CallResult
205250 {
206251 $ identification = [];
207252
208- if ($ event ->getUserId () ) {
209- $ identification ["userId " ] = $ event ->getUserId ( );
253+ if ($ event ->getUser () instanceof UserIdentified ) {
254+ $ identification ["user " ] = $ this -> userIdentifiersToArray ( $ event ->getUser () );
210255 }
211256
212- if ($ event ->getAccountId () ) {
213- $ identification ["accountId " ] = $ event ->getAccountId ( );
257+ if ($ event ->getAccount () instanceof AccountIdentified ) {
258+ $ identification ["account " ] = $ this -> accountIdentifiersToArray ( $ event ->getAccount () );
214259 }
215260
216261 $ payload = [
@@ -272,19 +317,20 @@ public function addEvent(Event $event): CallResult
272317 );
273318 }
274319
275- public function link (string $ deviceId , string $ userId ): CallResult
320+ public function link (array $ arguments ): CallResult
276321 {
277- if (empty ($ deviceId )) {
322+ if (isset ( $ arguments [ " deviceId " ]) === false || empty ($ arguments [ " deviceId " ] )) {
278323 throw new InvalidArgumentException ("Device ID cannot be empty! " );
279324 }
280325
281- if (empty ($ userId )) {
282- throw new InvalidArgumentException ("User ID cannot be empty! " );
283- }
284-
285326 $ payload = [
286- "deviceId " => $ deviceId ,
287- "userId " => $ userId ,
327+ "deviceId " => $ arguments ["deviceId " ],
328+ "identification " => $ this ->userIdentifiersToArray (
329+ new UserIdentified (
330+ $ arguments ["userId " ] ?? null ,
331+ $ arguments ["email " ] ?? null
332+ )
333+ ),
288334 ];
289335
290336 $ body = $ this ->streamFactory ->createStream (json_encode ($ payload ));
@@ -331,13 +377,13 @@ public function link(string $deviceId, string $userId): CallResult
331377 );
332378 }
333379
334- private function formatMetadata (array $ metadata )
380+ private function formatMetadata (array $ metadata ): array
335381 {
336382 $ formatted = array ();
337383
338384 foreach ($ metadata as $ name => $ value ) {
339385 if (is_int ($ value ) || is_float ($ value ) || is_string ($ value )) {
340- $ formatted [$ name ] = (string ) $ value ;
386+ $ formatted [$ name ] = (string )$ value ;
341387 }
342388
343389 if (is_bool ($ value )) {
@@ -352,13 +398,13 @@ private function formatMetadata(array $metadata)
352398 return $ formatted ;
353399 }
354400
355- private function formatProperties (array $ properties )
401+ private function formatProperties (array $ properties ): array
356402 {
357403 $ formatted = array ();
358404
359405 foreach ($ properties as $ name => $ value ) {
360406 if (is_int ($ value ) || is_float ($ value ) || is_string ($ value )) {
361- $ formatted [$ name ] = (string ) $ value ;
407+ $ formatted [$ name ] = (string )$ value ;
362408 }
363409
364410 if (is_bool ($ value )) {
@@ -375,17 +421,13 @@ private function formatProperties(array $properties)
375421
376422 public function upsertUser (array $ user ): CallResult
377423 {
378- if (!isset ($ user ["userId " ]) || empty ($ user ["userId " ])) {
379- throw new InvalidArgumentException ("User ID cannot be empty! " );
380- }
381-
382- if (!isset ($ user ["email " ]) || empty ($ user ["email " ])) {
383- throw new InvalidArgumentException ("User email cannot be empty! " );
384- }
385-
386424 $ payload = [
387- "userId " => (string ) $ user ["userId " ],
388- "email " => (string ) $ user ["email " ],
425+ "identification " => $ this ->userIdentifiersToArray (
426+ new UserIdentified (
427+ $ user ["userId " ] ?? null ,
428+ $ user ["email " ] ?? null
429+ )
430+ ),
389431 ];
390432
391433 if (isset ($ user ["properties " ]) && is_array ($ user ["properties " ])) {
@@ -438,17 +480,13 @@ public function upsertUser(array $user): CallResult
438480
439481 public function upsertAccount (array $ account ): CallResult
440482 {
441- if (!isset ($ account ["accountId " ]) || empty ($ account ["accountId " ])) {
442- throw new InvalidArgumentException ("Account ID cannot be empty! " );
443- }
444-
445- if (!isset ($ account ["name " ]) || empty ($ account ["name " ])) {
446- throw new InvalidArgumentException ("Account name cannot be empty! " );
447- }
448-
449483 $ payload = [
450- "accountId " => (string ) $ account ["accountId " ],
451- "name " => (string ) $ account ["name " ],
484+ "identification " => $ this ->accountIdentifiersToArray (
485+ new AccountIdentified (
486+ $ account ["accountId " ] ?? null ,
487+ $ account ["domain " ] ?? null
488+ )
489+ ),
452490 ];
453491
454492 if (isset ($ account ["properties " ]) && is_array ($ account ["properties " ])) {
@@ -457,8 +495,15 @@ public function upsertAccount(array $account): CallResult
457495
458496 if (isset ($ account ["members " ]) && is_array ($ account ["members " ])) {
459497 $ payload ["members " ] = array_map (
460- function ($ value ) {
461- return (string ) $ value ;
498+ function (array $ user ) {
499+ return [
500+ "identification " => $ this ->userIdentifiersToArray (
501+ new UserIdentified (
502+ $ user ["userId " ] ?? null ,
503+ $ user ["email " ] ?? null
504+ )
505+ ),
506+ ];
462507 },
463508 $ account ["members " ]
464509 );
0 commit comments