Skip to content

Commit

Permalink
Merge pull request #3 from journy-io/updated-api-endpoints
Browse files Browse the repository at this point in the history
Update methods
  • Loading branch information
hansott authored Mar 29, 2021
2 parents 697a3d8 + 86c07ff commit 34d73a4
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 119 deletions.
52 changes: 37 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ $call = $client->upsertUser([

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

```php
$call = $client->upsertAccount([
// required
"accountId" => "accountId", // Unique identifier for the account in your database
"name" => "Name",
"domain" => "acme-inc.com",

// optional
"properties" => [
"name" => "Acme, Inc",
"plan" => "Pro",
"age" => 26,
"is_developer" => true,
"is_paying_account" => true,
"amount_of_users" => 3,
"registered_at" => new \DateTimeImmutable("..."),
"this_property_will_be_deleted" => "",
],

// optional
"members" => ["userId", "userId"], // Unique identifier for the user in your database
"members" => [
["userId" => "1"], // Unique identifier for the user in your database
["userId" => "2"]
],
]);
```

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

if ($deviceId) {
$call = $client->link($deviceId, "userId");
$call = $client->link([
"deviceId" => "deviceId",
"userId" => "userId",
"email" => "email",
]);
}
```

#### Add event

```php
use JournyIO\SDK\Event;

$event = Event::forUser("login", "userId");
$event = Event::forUser("some_historic_event", "userId")->happenedAt(new \DateTimeImmutable("now"));
$event = Event::forAccount("reached_monthly_volume", "accountId")->withMetadata([
"number" => 13313,
"string" => "string",
"boolean" => true,
]);
$event = Event::forUserInAccount("updated_settings", "userId", "accountId");
use JournyIO\SDK\UserIdentified;
use JournyIO\SDK\AccountIdentified;

$event = Event::forUser("login", UserIdentified::byUserId("userId"));

$event = Event::forUser("some_historic_event", UserIdentified::byUserId("userId"))
->happenedAt(new \DateTimeImmutable("now"))
;

$event = Event::forAccount("reached_monthly_volume", AccountIdentified::byAccountId("accountId"))
->withMetadata([
"number" => 13313,
"string" => "string",
"boolean" => true,
])
;

$event = Event::forUserInAccount(
"updated_settings",
UserIdentified::byUserId("userId"),
AccountIdentified::byAccountId("accountId")
);

$call = $client->addEvent($event);
```
Expand Down
41 changes: 41 additions & 0 deletions src/AccountIdentified.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace JournyIO\SDK;

use InvalidArgumentException;

final class AccountIdentified
{
private $accountId;
private $domain;

public function __construct(string $accountId = null, string $domain = null)
{
if (empty($accountId) && empty($domain)) {
throw new InvalidArgumentException("Account ID or domain needs to set or both");
}

$this->accountId = $accountId;
$this->domain = $domain;
}

public static function byAccountId(string $accountId)
{
return new AccountIdentified($accountId, null);
}

public static function byDomain(string $domain)
{
return new AccountIdentified(null, $domain);
}

public function getAccountId()
{
return $this->accountId;
}

public function getDomain()
{
return $this->domain;
}
}
125 changes: 85 additions & 40 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ private function getMaxRequests(ResponseInterface $response): int
{
$values = $response->getHeader("x-ratelimit-limit");

return count($values) > 0 ? (int) $values[0] : 0;
return count($values) > 0 ? (int)$values[0] : 0;
}

private function getRemainingRequests(ResponseInterface $response): int
{
$values = $response->getHeader("x-ratelimit-remaining");

return count($values) > 0 ? (int) $values[0] : 0;
return count($values) > 0 ? (int)$values[0] : 0;
}

private function check(ResponseInterface $response)
Expand Down Expand Up @@ -191,6 +191,17 @@ public function getTrackingSnippet(string $domain): CallResult
);
}

if ($response->getStatusCode() === 404) {
return new CallResult(
false,
false,
$this->getRemainingRequests($response),
$this->getMaxRequests($response),
$json['message'] ? [$json['message']] : [],
null
);
}

return new CallResult(
false,
false,
Expand All @@ -201,16 +212,50 @@ public function getTrackingSnippet(string $domain): CallResult
);
}

private function userIdentifiersToArray(UserIdentified $user): array
{
$result = [];

$userId = $user->getUserId();
if ($userId) {
$result["userId"] = $userId;
}

$email = $user->getEmail();
if ($email) {
$result["email"] = $email;
}

return $result;
}

private function accountIdentifiersToArray(AccountIdentified $account): array
{
$result = [];

$accountId = $account->getAccountId();
if ($accountId) {
$result["accountId"] = $accountId;
}

$domain = $account->getDomain();
if ($domain) {
$result["domain"] = $domain;
}

return $result;
}

public function addEvent(Event $event): CallResult
{
$identification = [];

if ($event->getUserId()) {
$identification["userId"] = $event->getUserId();
if ($event->getUser() instanceof UserIdentified) {
$identification["user"] = $this->userIdentifiersToArray($event->getUser());
}

if ($event->getAccountId()) {
$identification["accountId"] = $event->getAccountId();
if ($event->getAccount() instanceof AccountIdentified) {
$identification["account"] = $this->accountIdentifiersToArray($event->getAccount());
}

$payload = [
Expand Down Expand Up @@ -272,19 +317,20 @@ public function addEvent(Event $event): CallResult
);
}

public function link(string $deviceId, string $userId): CallResult
public function link(array $arguments): CallResult
{
if (empty($deviceId)) {
if (isset($arguments["deviceId"]) === false || empty($arguments["deviceId"])) {
throw new InvalidArgumentException("Device ID cannot be empty!");
}

if (empty($userId)) {
throw new InvalidArgumentException("User ID cannot be empty!");
}

$payload = [
"deviceId" => $deviceId,
"userId" => $userId,
"deviceId" => $arguments["deviceId"],
"identification" => $this->userIdentifiersToArray(
new UserIdentified(
$arguments["userId"] ?? null,
$arguments["email"] ?? null
)
),
];

$body = $this->streamFactory->createStream(json_encode($payload));
Expand Down Expand Up @@ -331,13 +377,13 @@ public function link(string $deviceId, string $userId): CallResult
);
}

private function formatMetadata(array $metadata)
private function formatMetadata(array $metadata): array
{
$formatted = array();

foreach ($metadata as $name => $value) {
if (is_int($value) || is_float($value) || is_string($value)) {
$formatted[$name] = (string) $value;
$formatted[$name] = (string)$value;
}

if (is_bool($value)) {
Expand All @@ -352,13 +398,13 @@ private function formatMetadata(array $metadata)
return $formatted;
}

private function formatProperties(array $properties)
private function formatProperties(array $properties): array
{
$formatted = array();

foreach ($properties as $name => $value) {
if (is_int($value) || is_float($value) || is_string($value)) {
$formatted[$name] = (string) $value;
$formatted[$name] = (string)$value;
}

if (is_bool($value)) {
Expand All @@ -375,17 +421,13 @@ private function formatProperties(array $properties)

public function upsertUser(array $user): CallResult
{
if (!isset($user["userId"]) || empty($user["userId"])) {
throw new InvalidArgumentException("User ID cannot be empty!");
}

if (!isset($user["email"]) || empty($user["email"])) {
throw new InvalidArgumentException("User email cannot be empty!");
}

$payload = [
"userId" => (string) $user["userId"],
"email" => (string) $user["email"],
"identification" => $this->userIdentifiersToArray(
new UserIdentified(
$user["userId"] ?? null,
$user["email"] ?? null
)
),
];

if (isset($user["properties"]) && is_array($user["properties"])) {
Expand Down Expand Up @@ -438,17 +480,13 @@ public function upsertUser(array $user): CallResult

public function upsertAccount(array $account): CallResult
{
if (!isset($account["accountId"]) || empty($account["accountId"])) {
throw new InvalidArgumentException("Account ID cannot be empty!");
}

if (!isset($account["name"]) || empty($account["name"])) {
throw new InvalidArgumentException("Account name cannot be empty!");
}

$payload = [
"accountId" => (string) $account["accountId"],
"name" => (string) $account["name"],
"identification" => $this->accountIdentifiersToArray(
new AccountIdentified(
$account["accountId"] ?? null,
$account["domain"] ?? null
)
),
];

if (isset($account["properties"]) && is_array($account["properties"])) {
Expand All @@ -457,8 +495,15 @@ public function upsertAccount(array $account): CallResult

if (isset($account["members"]) && is_array($account["members"])) {
$payload["members"] = array_map(
function ($value) {
return (string) $value;
function (array $user) {
return [
"identification" => $this->userIdentifiersToArray(
new UserIdentified(
$user["userId"] ?? null,
$user["email"] ?? null
)
),
];
},
$account["members"]
);
Expand Down
Loading

0 comments on commit 34d73a4

Please sign in to comment.