@@ -63,14 +63,14 @@ private function getMaxRequests(ResponseInterface $response): int
63
63
{
64
64
$ values = $ response ->getHeader ("x-ratelimit-limit " );
65
65
66
- return count ($ values ) > 0 ? (int ) $ values [0 ] : 0 ;
66
+ return count ($ values ) > 0 ? (int )$ values [0 ] : 0 ;
67
67
}
68
68
69
69
private function getRemainingRequests (ResponseInterface $ response ): int
70
70
{
71
71
$ values = $ response ->getHeader ("x-ratelimit-remaining " );
72
72
73
- return count ($ values ) > 0 ? (int ) $ values [0 ] : 0 ;
73
+ return count ($ values ) > 0 ? (int )$ values [0 ] : 0 ;
74
74
}
75
75
76
76
private function check (ResponseInterface $ response )
@@ -191,6 +191,17 @@ public function getTrackingSnippet(string $domain): CallResult
191
191
);
192
192
}
193
193
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
+
194
205
return new CallResult (
195
206
false ,
196
207
false ,
@@ -201,16 +212,50 @@ public function getTrackingSnippet(string $domain): CallResult
201
212
);
202
213
}
203
214
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
+
204
249
public function addEvent (Event $ event ): CallResult
205
250
{
206
251
$ identification = [];
207
252
208
- if ($ event ->getUserId () ) {
209
- $ identification ["userId " ] = $ event ->getUserId ( );
253
+ if ($ event ->getUser () instanceof UserIdentified ) {
254
+ $ identification ["user " ] = $ this -> userIdentifiersToArray ( $ event ->getUser () );
210
255
}
211
256
212
- if ($ event ->getAccountId () ) {
213
- $ identification ["accountId " ] = $ event ->getAccountId ( );
257
+ if ($ event ->getAccount () instanceof AccountIdentified ) {
258
+ $ identification ["account " ] = $ this -> accountIdentifiersToArray ( $ event ->getAccount () );
214
259
}
215
260
216
261
$ payload = [
@@ -272,19 +317,20 @@ public function addEvent(Event $event): CallResult
272
317
);
273
318
}
274
319
275
- public function link (string $ deviceId , string $ userId ): CallResult
320
+ public function link (array $ arguments ): CallResult
276
321
{
277
- if (empty ($ deviceId )) {
322
+ if (isset ( $ arguments [ " deviceId " ]) === false || empty ($ arguments [ " deviceId " ] )) {
278
323
throw new InvalidArgumentException ("Device ID cannot be empty! " );
279
324
}
280
325
281
- if (empty ($ userId )) {
282
- throw new InvalidArgumentException ("User ID cannot be empty! " );
283
- }
284
-
285
326
$ 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
+ ),
288
334
];
289
335
290
336
$ body = $ this ->streamFactory ->createStream (json_encode ($ payload ));
@@ -331,13 +377,13 @@ public function link(string $deviceId, string $userId): CallResult
331
377
);
332
378
}
333
379
334
- private function formatMetadata (array $ metadata )
380
+ private function formatMetadata (array $ metadata ): array
335
381
{
336
382
$ formatted = array ();
337
383
338
384
foreach ($ metadata as $ name => $ value ) {
339
385
if (is_int ($ value ) || is_float ($ value ) || is_string ($ value )) {
340
- $ formatted [$ name ] = (string ) $ value ;
386
+ $ formatted [$ name ] = (string )$ value ;
341
387
}
342
388
343
389
if (is_bool ($ value )) {
@@ -352,13 +398,13 @@ private function formatMetadata(array $metadata)
352
398
return $ formatted ;
353
399
}
354
400
355
- private function formatProperties (array $ properties )
401
+ private function formatProperties (array $ properties ): array
356
402
{
357
403
$ formatted = array ();
358
404
359
405
foreach ($ properties as $ name => $ value ) {
360
406
if (is_int ($ value ) || is_float ($ value ) || is_string ($ value )) {
361
- $ formatted [$ name ] = (string ) $ value ;
407
+ $ formatted [$ name ] = (string )$ value ;
362
408
}
363
409
364
410
if (is_bool ($ value )) {
@@ -375,17 +421,13 @@ private function formatProperties(array $properties)
375
421
376
422
public function upsertUser (array $ user ): CallResult
377
423
{
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
-
386
424
$ 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
+ ),
389
431
];
390
432
391
433
if (isset ($ user ["properties " ]) && is_array ($ user ["properties " ])) {
@@ -438,17 +480,13 @@ public function upsertUser(array $user): CallResult
438
480
439
481
public function upsertAccount (array $ account ): CallResult
440
482
{
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
-
449
483
$ 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
+ ),
452
490
];
453
491
454
492
if (isset ($ account ["properties " ]) && is_array ($ account ["properties " ])) {
@@ -457,8 +495,15 @@ public function upsertAccount(array $account): CallResult
457
495
458
496
if (isset ($ account ["members " ]) && is_array ($ account ["members " ])) {
459
497
$ 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
+ ];
462
507
},
463
508
$ account ["members " ]
464
509
);
0 commit comments