Skip to content

Commit 713a8fa

Browse files
committed
✨ Adjust to latest spec
1 parent aed1e8f commit 713a8fa

File tree

6 files changed

+232
-161
lines changed

6 files changed

+232
-161
lines changed

README.md

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ $call = $client->upsertUser([
110110

111111
// optional
112112
"properties" => [
113+
"first_name" => "John",
114+
"last_name" => "Doe",
113115
"plan" => "Pro",
114116
"age" => 26,
115117
"is_developer" => true,
@@ -125,21 +127,24 @@ _Note: when sending an empty value (`""`) as value for a property, the property
125127

126128
```php
127129
$call = $client->upsertAccount([
128-
// required
129130
"accountId" => "accountId", // Unique identifier for the account in your database
130-
"name" => "Name",
131+
"domain" => "acme-inc.com",
131132

132133
// optional
133134
"properties" => [
135+
"name" => "Acme, Inc",
134136
"plan" => "Pro",
135-
"age" => 26,
136-
"is_developer" => true,
137+
"is_paying_account" => true,
138+
"amount_of_users" => 3,
137139
"registered_at" => new \DateTimeImmutable("..."),
138140
"this_property_will_be_deleted" => "",
139141
],
140142

141143
// optional
142-
"members" => ["userId", "userId"], // Unique identifier for the user in your database
144+
"members" => [
145+
["userId" => "1"], // Unique identifier for the user in your database
146+
["userId" => "2"]
147+
],
143148
]);
144149
```
145150

@@ -159,23 +164,40 @@ $deviceId = $request->cookie("__journey");
159164
$deviceId = $request->cookies->get("__journey");
160165

161166
if ($deviceId) {
162-
$call = $client->link($deviceId, "userId");
167+
$call = $client->link([
168+
"deviceId" => "deviceId",
169+
"userId" => "userId",
170+
"email" => "email",
171+
]);
163172
}
164173
```
165174

166175
#### Add event
167176

168177
```php
169178
use JournyIO\SDK\Event;
170-
171-
$event = Event::forUser("login", "userId");
172-
$event = Event::forUser("some_historic_event", "userId")->happenedAt(new \DateTimeImmutable("now"));
173-
$event = Event::forAccount("reached_monthly_volume", "accountId")->withMetadata([
174-
"number" => 13313,
175-
"string" => "string",
176-
"boolean" => true,
177-
]);
178-
$event = Event::forUserInAccount("updated_settings", "userId", "accountId");
179+
use JournyIO\SDK\UserIdentified;
180+
use JournyIO\SDK\AccountIdentified;
181+
182+
$event = Event::forUser("login", UserIdentified::byUserId("userId"));
183+
184+
$event = Event::forUser("some_historic_event", UserIdentified::byUserId("userId"))
185+
->happenedAt(new \DateTimeImmutable("now"))
186+
;
187+
188+
$event = Event::forAccount("reached_monthly_volume", AccountIdentified::byAccountId("accountId"))
189+
->withMetadata([
190+
"number" => 13313,
191+
"string" => "string",
192+
"boolean" => true,
193+
])
194+
;
195+
196+
$event = Event::forUserInAccount(
197+
"updated_settings",
198+
UserIdentified::byUserId("userId"),
199+
AccountIdentified::byAccountId("accountId")
200+
);
179201

180202
$call = $client->addEvent($event);
181203
```

src/AccountIdentified.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JournyIO\SDK;
4+
5+
use InvalidArgumentException;
6+
7+
final class AccountIdentified
8+
{
9+
private $accountId;
10+
private $domain;
11+
12+
public function __construct(string $accountId = null, string $domain = null)
13+
{
14+
if (empty($accountId) && empty($domain)) {
15+
throw new InvalidArgumentException("Account ID or domain needs to set or both");
16+
}
17+
18+
$this->accountId = $accountId;
19+
$this->domain = $domain;
20+
}
21+
22+
public static function byAccountId(string $accountId)
23+
{
24+
return new AccountIdentified($accountId, null);
25+
}
26+
27+
public static function byDomain(string $domain)
28+
{
29+
return new AccountIdentified(null, $domain);
30+
}
31+
32+
public function getAccountId()
33+
{
34+
return $this->accountId;
35+
}
36+
37+
public function getDomain()
38+
{
39+
return $this->domain;
40+
}
41+
}

src/Client.php

Lines changed: 66 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -201,35 +201,50 @@ public function getTrackingSnippet(string $domain): CallResult
201201
);
202202
}
203203

204-
public function addEvent(Event $event): CallResult
204+
private function userIdentifiersToArray(UserIdentified $user): array
205205
{
206-
$identification = [];
207-
$userIdentification = [];
208-
$accountIdentification = [];
209-
$identifiedUser = false;
210-
$identifiedAccount = false;
206+
$result = [];
211207

212-
if ($event->getUserId()) {
213-
$userIdentification["userId"] = $event->getUserId();
214-
$identifiedUser = true;
208+
$userId = $user->getUserId();
209+
if ($userId) {
210+
$result["userId"] = $userId;
215211
}
216212

217-
if ($event->getEmail()) {
218-
$userIdentification["email"] = $event->getEmail();
219-
$identifiedUser = true;
213+
$email = $user->getEmail();
214+
if ($email) {
215+
$result["email"] = $email;
220216
}
221217

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;
225228
}
226229

227-
if ($identifiedUser) {
228-
$identification["user"] = $userIdentification;
230+
$domain = $account->getDomain();
231+
if ($domain) {
232+
$result["domain"] = $domain;
229233
}
230234

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());
233248
}
234249

235250
$payload = [
@@ -291,29 +306,20 @@ public function addEvent(Event $event): CallResult
291306
);
292307
}
293308

294-
public function link(string $deviceId, string $userId = null, string $email = null): CallResult
309+
public function link(array $arguments): CallResult
295310
{
296-
if (empty($deviceId)) {
311+
if (isset($arguments["deviceId"]) === false || empty($arguments["deviceId"])) {
297312
throw new InvalidArgumentException("Device ID cannot be empty!");
298313
}
299314

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-
314315
$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+
),
317323
];
318324

319325
$body = $this->streamFactory->createStream(json_encode($payload));
@@ -360,7 +366,7 @@ public function link(string $deviceId, string $userId = null, string $email = nu
360366
);
361367
}
362368

363-
private function formatMetadata(array $metadata)
369+
private function formatMetadata(array $metadata): array
364370
{
365371
$formatted = array();
366372

@@ -381,7 +387,7 @@ private function formatMetadata(array $metadata)
381387
return $formatted;
382388
}
383389

384-
private function formatProperties(array $properties)
390+
private function formatProperties(array $properties): array
385391
{
386392
$formatted = array();
387393

@@ -404,24 +410,13 @@ private function formatProperties(array $properties)
404410

405411
public function upsertUser(array $user): CallResult
406412
{
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-
423413
$payload = [
424-
"identification" => $identification
414+
"identification" => $this->userIdentifiersToArray(
415+
new UserIdentified(
416+
$user["userId"] ?? null,
417+
$user["email"] ?? null
418+
)
419+
),
425420
];
426421

427422
if (isset($user["properties"]) && is_array($user["properties"])) {
@@ -474,22 +469,13 @@ public function upsertUser(array $user): CallResult
474469

475470
public function upsertAccount(array $account): CallResult
476471
{
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-
490472
$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+
),
493479
];
494480

495481
if (isset($account["properties"]) && is_array($account["properties"])) {
@@ -498,8 +484,15 @@ public function upsertAccount(array $account): CallResult
498484

499485
if (isset($account["members"]) && is_array($account["members"])) {
500486
$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+
];
503496
},
504497
$account["members"]
505498
);

0 commit comments

Comments
 (0)