Skip to content

Commit 34d73a4

Browse files
authored
Merge pull request #3 from journy-io/updated-api-endpoints
Update methods
2 parents 697a3d8 + 86c07ff commit 34d73a4

File tree

6 files changed

+282
-119
lines changed

6 files changed

+282
-119
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: 85 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)