Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Try/Catch to allow for error handling above library #50

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions src/Analytics/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Utopia\Analytics;

use Exception;
use Utopia\CLI\Console;

abstract class Adapter
{
Expand Down Expand Up @@ -95,16 +94,12 @@ public function setUserAgent(string $userAgent): self

/**
* Creates an Event on the remote analytics platform.
*
* @throws Exception
*/
public function createEvent(Event $event): bool
{
try {
return $this->send($event);
} catch (\Exception $e) {
$this->logError($e);

return false;
}
return $this->send($event);
}

/**
Expand Down Expand Up @@ -212,20 +207,4 @@ protected function flatten(array $data, string $prefix = ''): array

return $output;
}

/**
* Log Error
*
* @return void
*/
protected function logError(Exception $e)
{
Console::error('[Error] '.$this->getName().' Error: ');
Console::error('[Error] Type: '.get_class($e));
Console::error('[Error] Message: '.$e->getMessage());
Console::error('[Error] File: '.$e->getFile());
Console::error('[Error] Line: '.$e->getLine());
Console::error('[Error] Trace: ');
Console::error($e->getTraceAsString());
}
}
201 changes: 74 additions & 127 deletions src/Analytics/Adapter/HubSpot.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,25 @@ public function getName(): string
*/
public function contactExists(string $email): bool|int
{
try {
$result = $this->call('POST', '/crm/v3/objects/contacts/search', [
'Content-Type' => 'application/json',
], [
'filterGroups' => [[
'filters' => [
[
'value' => $email,
'propertyName' => 'email',
'operator' => 'EQ',
],
$result = $this->call('POST', '/crm/v3/objects/contacts/search', [
'Content-Type' => 'application/json',
], [
'filterGroups' => [[
'filters' => [
[
'value' => $email,
'propertyName' => 'email',
'operator' => 'EQ',
],
], ],
]);
],
], ],
]);

$result = json_decode($result, true);

if ($result && $result['total'] > 0 && count($result['results']) > 0) {
return $result['results'][0]['id'];
} else {
return false;
}
} catch (\Exception $e) {
$this->logError($e);
$result = json_decode($result, true);

if ($result && $result['total'] > 0 && count($result['results']) > 0) {
return $result['results'][0]['id'];
} else {
return false;
}
}
Expand All @@ -97,17 +91,11 @@ public function createContact(string $email, string $firstName = '', string $las
'phone' => $phone,
]];

try {
$this->call('POST', '/crm/v3/objects/contacts', [
'Content-Type' => 'application/json',
], $body);

return true;
} catch (\Exception $e) {
$this->logError($e);
$this->call('POST', '/crm/v3/objects/contacts', [
'Content-Type' => 'application/json',
], $body);

return false;
}
return true;
}

/**
Expand All @@ -134,9 +122,7 @@ public function updateContact(string $contactId, string $email, string $firstNam
return true;
}

$this->logError($e);

return false;
throw $e;
}
}

Expand All @@ -151,49 +137,37 @@ public function deleteContact(string $email): bool
return false;
}

try {
$this->call('DELETE', '/crm/v3/objects/contacts/'.$contact, [
'Content-Type' => 'application/json',
]);

return true;
} catch (\Exception $e) {
$this->logError($e);
$this->call('DELETE', '/crm/v3/objects/contacts/'.$contact, [
'Content-Type' => 'application/json',
]);

return false;
}
return true;
}

/**
* Account Exists
*/
public function accountExists(string $name): bool|int
{
try {
$result = $this->call('POST', '/crm/v3/objects/companies/search', [
'Content-Type' => 'application/json',
], [
'filterGroups' => [[
'filters' => [
[
'value' => $name,
'propertyName' => 'name',
'operator' => 'EQ',
],
$result = $this->call('POST', '/crm/v3/objects/companies/search', [
'Content-Type' => 'application/json',
], [
'filterGroups' => [[
'filters' => [
[
'value' => $name,
'propertyName' => 'name',
'operator' => 'EQ',
],
]],
]);

$result = json_decode($result, true);
],
]],
]);

if ($result && $result['total'] > 0 && count($result['results']) > 0) {
return $result['results'][0]['id'];
} else {
return false;
}
} catch (\Exception $e) {
$this->logError($e);
$result = json_decode($result, true);

if ($result && $result['total'] > 0 && count($result['results']) > 0) {
return $result['results'][0]['id'];
} else {
return false;
}
}
Expand All @@ -208,17 +182,11 @@ public function createAccount(string $name, string $url = ''): bool
'domain' => $url,
]];

try {
$this->call('POST', '/crm/v3/objects/companies', [
'Content-Type' => 'application/json',
], $body);

return true;
} catch (\Exception $e) {
$this->logError($e);
$this->call('POST', '/crm/v3/objects/companies', [
'Content-Type' => 'application/json',
], $body);

return false;
}
return true;
}

/**
Expand All @@ -243,9 +211,7 @@ public function updateAccount(string $accountId, string $name, string $url = '',
return true;
}

$this->logError($e);

return false;
throw $e;
}
}

Expand All @@ -254,17 +220,11 @@ public function updateAccount(string $accountId, string $name, string $url = '',
*/
public function deleteAccount(string $accountId): bool
{
try {
$this->call('DELETE', '/crm/v3/objects/companies/'.$accountId, [
'Content-Type' => 'application/json',
]);
$this->call('DELETE', '/crm/v3/objects/companies/'.$accountId, [
'Content-Type' => 'application/json',
]);

return true;
} catch (\Exception $e) {
$this->logError($e);

return false;
}
return true;
}

/**
Expand All @@ -275,39 +235,32 @@ public function deleteAccount(string $accountId): bool
public function syncAssociation(string $accountId, string $contactId, string $role = ''): bool
{
// See if the association already exists
$response = $this->call('GET', '/crm/v4/objects/contact/'.$accountId.'/associations/company');

try {
$response = $this->call('GET', '/crm/v4/objects/contact/'.$accountId.'/associations/company');

$response = json_decode($response, true);
$response = json_decode($response, true);

$associationId = null;
$associationId = null;

foreach ($response['results'] as $association) {
if ($association['from']['id'] == $contactId) {
$associationId = $association['id'];
}
foreach ($response['results'] as $association) {
if ($association['from']['id'] == $contactId) {
$associationId = $association['id'];
}
}

if (empty($associationId)) {
// Create the association
$this->call('PUT', '/crm/v4/objects/contact/'.$contactId.'/associations/default/company/'.$accountId, [
'Content-Type' => 'application/json',
]);
} else {
// Delete and recreate the association
$this->call('DELETE', '/crm/v4/objects/contact/'.$contactId.'/associations/company/'.$accountId, [
'Content-Type' => 'application/json',
]);

$this->call('PUT', '/crm/v4/objects/contact/'.$contactId.'/associations/default/company/'.$accountId, [
'Content-Type' => 'application/json',
]);
}
} catch (\Exception $e) {
$this->logError($e);
if (empty($associationId)) {
// Create the association
$this->call('PUT', '/crm/v4/objects/contact/'.$contactId.'/associations/default/company/'.$accountId, [
'Content-Type' => 'application/json',
]);
} else {
// Delete and recreate the association
$this->call('DELETE', '/crm/v4/objects/contact/'.$contactId.'/associations/company/'.$accountId, [
'Content-Type' => 'application/json',
]);

return false;
$this->call('PUT', '/crm/v4/objects/contact/'.$contactId.'/associations/default/company/'.$accountId, [
'Content-Type' => 'application/json',
]);
}

return true;
Expand All @@ -318,16 +271,10 @@ public function syncAssociation(string $accountId, string $contactId, string $ro
*/
public function addToList(int $listId, int $contactId): bool
{
try {
$this->call('PUT', '/crm/v3/lists/'.$listId.'/memberships/add', [
'Content-Type' => 'application/json',
], [$contactId]);
$this->call('PUT', '/crm/v3/lists/'.$listId.'/memberships/add', [
'Content-Type' => 'application/json',
], [$contactId]);

return true;
} catch (\Exception $e) {
$this->logError($e);

return false;
}
return true;
}
}