@@ -201,35 +201,50 @@ public function getTrackingSnippet(string $domain): CallResult
201
201
);
202
202
}
203
203
204
- public function addEvent ( Event $ event ): CallResult
204
+ private function userIdentifiersToArray ( UserIdentified $ user ): array
205
205
{
206
- $ identification = [];
207
- $ userIdentification = [];
208
- $ accountIdentification = [];
209
- $ identifiedUser = false ;
210
- $ identifiedAccount = false ;
206
+ $ result = [];
211
207
212
- if ( $ event ->getUserId ()) {
213
- $ userIdentification [ " userId " ] = $ event -> getUserId ();
214
- $ identifiedUser = true ;
208
+ $ userId = $ user ->getUserId ();
209
+ if ( $ userId) {
210
+ $ result [ " userId " ] = $ userId ;
215
211
}
216
212
217
- if ( $ event ->getEmail ()) {
218
- $ userIdentification [ " email " ] = $ event -> getEmail ();
219
- $ identifiedUser = true ;
213
+ $ email = $ user ->getEmail ();
214
+ if ( $ email) {
215
+ $ result [ " email " ] = $ email ;
220
216
}
221
217
222
- if ($ event ->getAccountId ()) {
223
- $ accountIdentification ["accountId " ] = $ event ->getAccountId ();
224
- $ identifiedAccount = true ;
218
+ return $ result ;
219
+ }
220
+
221
+ private function accountIdentifiersToArray (AccountIdentified $ account ): array
222
+ {
223
+ $ result = [];
224
+
225
+ $ accountId = $ account ->getAccountId ();
226
+ if ($ accountId ) {
227
+ $ result ["accountId " ] = $ accountId ;
225
228
}
226
229
227
- if ($ identifiedUser ) {
228
- $ identification ["user " ] = $ userIdentification ;
230
+ $ domain = $ account ->getDomain ();
231
+ if ($ domain ) {
232
+ $ result ["domain " ] = $ domain ;
229
233
}
230
234
231
- if ($ identifiedAccount ) {
232
- $ identification ["account " ] = $ accountIdentification ;
235
+ return $ result ;
236
+ }
237
+
238
+ public function addEvent (Event $ event ): CallResult
239
+ {
240
+ $ identification = [];
241
+
242
+ if ($ event ->getUser () instanceof UserIdentified) {
243
+ $ identification ["user " ] = $ this ->userIdentifiersToArray ($ event ->getUser ());
244
+ }
245
+
246
+ if ($ event ->getAccount () instanceof AccountIdentified) {
247
+ $ identification ["account " ] = $ this ->accountIdentifiersToArray ($ event ->getAccount ());
233
248
}
234
249
235
250
$ payload = [
@@ -291,29 +306,20 @@ public function addEvent(Event $event): CallResult
291
306
);
292
307
}
293
308
294
- public function link (string $ deviceId , string $ userId = null , string $ email = null ): CallResult
309
+ public function link (array $ arguments ): CallResult
295
310
{
296
- if (empty ($ deviceId )) {
311
+ if (isset ( $ arguments [ " deviceId " ]) === false || empty ($ arguments [ " deviceId " ] )) {
297
312
throw new InvalidArgumentException ("Device ID cannot be empty! " );
298
313
}
299
314
300
- if (empty ($ userId ) && empty ($ email )) {
301
- throw new InvalidArgumentException ("User ID and email cannot both be empty! " );
302
- }
303
-
304
- $ identification = [];
305
-
306
- if (!empty ($ userId )) {
307
- $ identification ["userId " ] = $ userId ;
308
- }
309
-
310
- if (!empty ($ email )) {
311
- $ identification ["email " ] = $ email ;
312
- }
313
-
314
315
$ payload = [
315
- "deviceId " => $ deviceId ,
316
- "identification " => $ identification ,
316
+ "deviceId " => $ arguments ["deviceId " ],
317
+ "identification " => $ this ->userIdentifiersToArray (
318
+ new UserIdentified (
319
+ $ arguments ["userId " ] ?? null ,
320
+ $ arguments ["email " ] ?? null
321
+ )
322
+ ),
317
323
];
318
324
319
325
$ body = $ this ->streamFactory ->createStream (json_encode ($ payload ));
@@ -360,7 +366,7 @@ public function link(string $deviceId, string $userId = null, string $email = nu
360
366
);
361
367
}
362
368
363
- private function formatMetadata (array $ metadata )
369
+ private function formatMetadata (array $ metadata ): array
364
370
{
365
371
$ formatted = array ();
366
372
@@ -381,7 +387,7 @@ private function formatMetadata(array $metadata)
381
387
return $ formatted ;
382
388
}
383
389
384
- private function formatProperties (array $ properties )
390
+ private function formatProperties (array $ properties ): array
385
391
{
386
392
$ formatted = array ();
387
393
@@ -404,24 +410,13 @@ private function formatProperties(array $properties)
404
410
405
411
public function upsertUser (array $ user ): CallResult
406
412
{
407
- if ((!isset ($ user ["userId " ]) || empty ($ user ["userId " ]))
408
- && (!isset ($ user ["email " ]) || empty ($ user ["email " ]))) {
409
- throw new InvalidArgumentException ("User ID and User email cannot both be empty! " );
410
- }
411
-
412
-
413
- $ identification = [];
414
-
415
- if (isset ($ user ["userId " ])) {
416
- $ identification ["userId " ] = (string )$ user ["userId " ];
417
- }
418
-
419
- if (isset ($ user ["email " ])) {
420
- $ identification ["email " ] = (string )$ user ["email " ];
421
- }
422
-
423
413
$ payload = [
424
- "identification " => $ identification
414
+ "identification " => $ this ->userIdentifiersToArray (
415
+ new UserIdentified (
416
+ $ user ["userId " ] ?? null ,
417
+ $ user ["email " ] ?? null
418
+ )
419
+ ),
425
420
];
426
421
427
422
if (isset ($ user ["properties " ]) && is_array ($ user ["properties " ])) {
@@ -474,22 +469,13 @@ public function upsertUser(array $user): CallResult
474
469
475
470
public function upsertAccount (array $ account ): CallResult
476
471
{
477
- if (!isset ($ account ["accountId " ]) || empty ($ account ["accountId " ])) {
478
- throw new InvalidArgumentException ("Account ID cannot be empty! " );
479
- }
480
-
481
- if (!isset ($ account ["name " ]) || empty ($ account ["name " ])) {
482
- throw new InvalidArgumentException ("Account name cannot be empty! " );
483
- }
484
-
485
- $ identification = [
486
- "accountId " => (string )$ account ["accountId " ]
487
- ];
488
-
489
-
490
472
$ payload = [
491
- "identification " => $ identification ,
492
- "name " => (string )$ account ["name " ],
473
+ "identification " => $ this ->accountIdentifiersToArray (
474
+ new AccountIdentified (
475
+ $ account ["accountId " ] ?? null ,
476
+ $ account ["domain " ] ?? null
477
+ )
478
+ ),
493
479
];
494
480
495
481
if (isset ($ account ["properties " ]) && is_array ($ account ["properties " ])) {
@@ -498,8 +484,15 @@ public function upsertAccount(array $account): CallResult
498
484
499
485
if (isset ($ account ["members " ]) && is_array ($ account ["members " ])) {
500
486
$ payload ["members " ] = array_map (
501
- function ($ value ) {
502
- return (string )$ value ;
487
+ function (array $ user ) {
488
+ return [
489
+ "identification " => $ this ->userIdentifiersToArray (
490
+ new UserIdentified (
491
+ $ user ["userId " ] ?? null ,
492
+ $ user ["email " ] ?? null
493
+ )
494
+ ),
495
+ ];
503
496
},
504
497
$ account ["members " ]
505
498
);
0 commit comments