Skip to content

Commit

Permalink
💄 Simplify Code
Browse files Browse the repository at this point in the history
  • Loading branch information
HighLiuk committed Jul 27, 2022
1 parent eb341d2 commit 5a6e5fa
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 207 deletions.
59 changes: 22 additions & 37 deletions src/Accounts/Accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class Accounts extends Resource
public function create($account)
{
$req = $this->client->getClient()->post('/api/3/accounts', [
'json' => [
'account' => $account,
],
'json' => compact('account'),
]);

return $req->getBody()->getContents();
Expand All @@ -43,9 +41,7 @@ public function create($account)
public function update($id, $account)
{
$req = $this->client->getClient()->put('/api/3/accounts/' . $id, [
'json' => [
'account' => $account,
],
'json' => compact('account'),
]);

return $req->getBody()->getContents();
Expand Down Expand Up @@ -86,21 +82,18 @@ public function get($id)
*
* @see https://developers.activecampaign.com/reference#list-all-accounts
*
* @param array $query_params
* @param array $query
* @param int $limit
* @param int $offset
* @return mixed
*/
public function listAll($query_params = [], $limit = 20, $offset = 0)
public function listAll($query = [], $limit = 20, $offset = 0)
{
$query_params = array_merge($query_params, [
'limit' => $limit,
'offset' => $offset,
]);
$query = array_merge($query, compact('limit', 'offset'));

$req = $this->client->getClient()->get('/api/3/accounts', [
'query' => $query_params,
]);
$req = $this->client
->getClient()
->get('/api/3/accounts', compact('query'));

return $req->getBody()->getContents();
}
Expand Down Expand Up @@ -128,18 +121,18 @@ public function getAccountCustomFields($account_id)
/**
* Elenca tutti gli account, iterando sulla paginazione
*
* @param array $query_params
* @param array $query
* @param int $accounts_per_page
* @param bool $debug
* @return array
*/
public function listAllLoop(
$query_params = [],
$query = [],
$accounts_per_page = 100,
$debug = false
) {
// Risposta JSON dal server
$res = $this->listAll($query_params, $accounts_per_page);
$res = $this->listAll($query, $accounts_per_page);

// Converto la risposta in array
$res = json_decode($res, true);
Expand All @@ -157,7 +150,7 @@ public function listAllLoop(
// Loop sulle pagine
for ($page = 1; $page < $pages; $page++) {
$res = $this->listAll(
$query_params,
$query,
$accounts_per_page,
$page * $accounts_per_page
);
Expand Down Expand Up @@ -191,13 +184,13 @@ public function listAllWithCustomFields(
$accounts_per_page = 100
) {
// aggiungo ai parametri della query i contactLists
$query_params = [
$query = [
'include' =>
'accountCustomFieldData.customerAccountCustomFieldMetum',
];

// Risposta JSON dal server
$res = $this->listAll($query_params, $accounts_per_page);
$res = $this->listAll($query, $accounts_per_page);

// Converto la risposta in array
$res = json_decode($res, true);
Expand All @@ -218,7 +211,7 @@ public function listAllWithCustomFields(
// Loop sulle pagine
for ($page = 1; $page < $pages; $page++) {
$res = $this->listAll(
$query_params,
$query,
$accounts_per_page,
$page * $accounts_per_page
);
Expand Down Expand Up @@ -247,28 +240,22 @@ public function listAllWithCustomFields(
// rimuovo eventuali copie inutili nei custom fields meta
$customFieldsMeta = array_unique($customFieldsMeta, SORT_REGULAR);

return [
'accounts' => $accounts,
'customFields' => $customFields,
'customFieldsMeta' => $customFieldsMeta,
];
return compact('accounts', 'customFields', 'customFieldsMeta');
}

/**
* List all custom fields
*
* @see https://developers.activecampaign.com/reference#list-all-custom-fields
*
* @param array $query_params
* @param array $query
* @return string
*/
public function listAllCustomFields($query_params = [])
public function listAllCustomFields($query = [])
{
$req = $this->client
->getClient()
->get('/api/3/accountCustomFieldMeta', [
'query' => $query_params,
]);
->get('/api/3/accountCustomFieldMeta', compact('query'));

return $req->getBody()->getContents();
}
Expand All @@ -279,16 +266,14 @@ public function listAllCustomFields($query_params = [])
*
* @see https://developers.activecampaign.com/reference#list-all-custom-field-values-2
*
* @param array $query_params
* @param array $query
* @return string
*/
public function listAllCustomFieldValues($query_params = [])
public function listAllCustomFieldValues($query = [])
{
$req = $this->client
->getClient()
->get('/api/3/accountCustomFieldData', [
'query' => $query_params,
]);
->get('/api/3/accountCustomFieldData', compact('query'));

return $req->getBody()->getContents();
}
Expand Down
78 changes: 31 additions & 47 deletions src/Contacts/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ public function removeSubscription($contact_list_id)
/**
* Elenca tutti i contatti, iterando sulla paginazione
*
* @param array $query_params
* @param array $query
* @param int $contacts_per_page
* @param bool $debug
* @return array
*/
public function listAllLoop(
$query_params = [],
$query = [],
$contacts_per_page = 100,
$debug = false
) {
// Risposta JSON dal server
$res = $this->listAll($query_params, $contacts_per_page);
$res = $this->listAll($query, $contacts_per_page);

// Converto la risposta in array
$res = json_decode($res, true);
Expand All @@ -81,7 +81,7 @@ public function listAllLoop(
// Loop sulle pagine
for ($page = 1; $page < $pages; $page++) {
$res = $this->listAll(
$query_params,
$query,
$contacts_per_page,
$page * $contacts_per_page
);
Expand Down Expand Up @@ -115,12 +115,12 @@ public function listAllWithContactLists(
$contacts_per_page = 100
) {
// aggiungo ai parametri della query i contactLists
$query_params = [
$query = [
'include' => 'contactLists',
];

// Risposta JSON dal server
$res = $this->listAll($query_params, $contacts_per_page);
$res = $this->listAll($query, $contacts_per_page);

// Converto la risposta in array
$res = json_decode($res, true);
Expand All @@ -140,7 +140,7 @@ public function listAllWithContactLists(
// Loop sulle pagine
for ($page = 1; $page < $pages; $page++) {
$res = $this->listAll(
$query_params,
$query,
$contacts_per_page,
$page * $contacts_per_page
);
Expand All @@ -159,10 +159,7 @@ public function listAllWithContactLists(
}
}

return [
'contacts' => $contacts,
'contactLists' => $contactLists,
];
return compact('contacts', 'contactLists');
}

/**
Expand Down Expand Up @@ -225,9 +222,7 @@ public function createWithAccount($contact, $account_id)
public function bulkImport($contacts)
{
$req = $this->client->getClient()->post('/api/3/import/bulk_import', [
'json' => [
'contacts' => $contacts,
],
'json' => compact('contacts'),
]);

return $req->getBody()->getContents();
Expand All @@ -244,9 +239,7 @@ public function bulkImport($contacts)
public function create($contact)
{
$req = $this->client->getClient()->post('/api/3/contacts', [
'json' => [
'contact' => $contact,
],
'json' => compact('contact'),
]);

return $req->getBody()->getContents();
Expand All @@ -263,9 +256,7 @@ public function create($contact)
public function sync($contact)
{
$req = $this->client->getClient()->post('/api/3/contact/sync', [
'json' => [
'contact' => $contact,
],
'json' => compact('contact'),
]);

return $req->getBody()->getContents();
Expand All @@ -291,15 +282,13 @@ public function get($id)
*
* @see https://developers.activecampaign.com/reference#update-list-status-for-contact
*
* @param array $contact_list
* @param array $contactList
* @return string
*/
public function updateListStatus($contact_list)
public function updateListStatus($contactList)
{
$req = $this->client->getClient()->post('/api/3/contactLists', [
'json' => [
'contactList' => $contact_list,
],
'json' => compact('contactList'),
]);

return $req->getBody()->getContents();
Expand All @@ -317,9 +306,7 @@ public function updateListStatus($contact_list)
public function update($id, $contact)
{
$req = $this->client->getClient()->put('/api/3/contacts/' . $id, [
'json' => [
'contact' => $contact,
],
'json' => compact('contact'),
]);

return $req->getBody()->getContents();
Expand Down Expand Up @@ -419,21 +406,18 @@ public function untag($contact_tag_id)
*
* @see https://developers.activecampaign.com/reference#list-all-contacts
*
* @param array $query_params
* @param array $query
* @param int $limit
* @param int $offset
* @return string
*/
public function listAll($query_params = [], $limit = 20, $offset = 0)
public function listAll($query = [], $limit = 20, $offset = 0)
{
$query_params = array_merge($query_params, [
'limit' => $limit,
'offset' => $offset,
]);
$query = array_merge($query, compact('limit', 'offset'));

$req = $this->client->getClient()->get('/api/3/contacts', [
'query' => $query_params,
]);
$req = $this->client
->getClient()
->get('/api/3/contacts', compact('query'));

return $req->getBody()->getContents();
}
Expand All @@ -443,14 +427,14 @@ public function listAll($query_params = [], $limit = 20, $offset = 0)
*
* @see https://developers.activecampaign.com/v3/reference#retrieve-fields-1
*
* @param array $query_params
* @param array $query
* @return string
*/
public function listAllCustomFields($query_params = [])
public function listAllCustomFields($query = [])
{
$req = $this->client->getClient()->get('/api/3/fields', [
'query' => $query_params,
]);
$req = $this->client
->getClient()
->get('/api/3/fields', compact('query'));

return $req->getBody()->getContents();
}
Expand Down Expand Up @@ -596,14 +580,14 @@ public function addContactToAccount(
*
* @see https://developers.activecampaign.com/reference#list-all-associations
*
* @param array $query_params
* @param array $query
* @return string
*/
public function getContactAccountAssociation($query_params = [])
public function getContactAccountAssociation($query = [])
{
$req = $this->client->getClient()->get('/api/3/accountContacts', [
'query' => $query_params,
]);
$req = $this->client
->getClient()
->get('/api/3/accountContacts', compact('query'));

return $req->getBody()->getContents();
}
Expand Down
Loading

0 comments on commit 5a6e5fa

Please sign in to comment.