Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Zahn committed Mar 24, 2016
2 parents 4efaeeb + 686d871 commit 16b349b
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ $opportunityApi = $this->apiWrapper->getOpportunityApi();
$result = $opportunityApi->addOpportunity($opportunity);
```

Updating custom fields
----------------------
```php
$customField = new CustomField();
$customField->setId('Custom field id')
$customField->addChoice('Value for choices list');

$customFieldApi = $this->apiWrapper->getCustomFieldApi();
$result = $customFieldApi->updateCustomField($customField);
```

Info
------------
Right now just a few request are implemented, because the main need was to create leads.
Expand Down
79 changes: 79 additions & 0 deletions src/LooplineSystems/CloseIoApiWrapper/Api/CustomFieldApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace LooplineSystems\CloseIoApiWrapper\Api;

use LooplineSystems\CloseIoApiWrapper\CloseIoResponse;
use LooplineSystems\CloseIoApiWrapper\Library\Api\AbstractApi;
use LooplineSystems\CloseIoApiWrapper\Model\CustomField;

class CustomFieldApi extends AbstractApi
{
const NAME = 'CustomFieldApi';

protected function initUrls()
{
$this->urls = [
'get-customFields' => '/custom_fields/lead/',
'update-customField' => '/custom_fields/lead/[:id]/'
];
}

/**
* @param Curl $curl
*/
public function setCurl($curl)
{
$this->curl = $curl;
}

/**
* @return CustomField[]
*/
public function getAllCustomFields()
{
/** @var CustomField[] $customFields */
$customFields = array();

$apiRequest = $this->prepareRequest('get-customFields');

/** @var CloseIoResponse $result */
$result = $this->triggerGet($apiRequest);

if ($result->getReturnCode() == 200) {
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];

foreach ($rawData as $customField) {
$customFields[] = new CustomField($customField);
}
}
return $customFields;
}

/**
* @param CustomField $customField
*
* @return CustomField
* @throws InvalidParamException
* @throws ResourceNotFoundException
*/
public function updateCustomField(CustomField $customField)
{
if ($customField->getId() == null) {
throw new InvalidParamException('When updating a custom field you must provide the custom field ID');
}
$id = $customField->getId();
$customField->setId(null);

$customField = json_encode($customField);
$apiRequest = $this->prepareRequest('update-customField', $customField, ['id' => $id]);
$response = $this->triggerPut($apiRequest);

if ($response->getReturnCode() == 200 && ($response->getData() !== null)) {
$customField = new CustomField($response->getData());
} else {
throw new ResourceNotFoundException();
}

return $customField;
}
}
42 changes: 41 additions & 1 deletion src/LooplineSystems/CloseIoApiWrapper/Api/LeadApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getAllLeads()
$result = $this->triggerGet($apiRequest);

if ($result->getReturnCode() == 200) {
$rawData = $result->getData()[CloseIoResponse::GET_ALL_RESPONSE_LEADS_KEY];
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];

foreach ($rawData as $lead) {
$leads[] = new Lead($lead);
Expand All @@ -57,6 +57,46 @@ public function getAllLeads()
return $leads;
}

/**
* @param array $queryParams
*
* @return Lead[]
*/
public function findLeads(array $queryParams)
{
/** @var Lead[] $leads */
$leads = array();
if (count($queryParams) > 0) {
$queryParams = ['query' => $this->buildQueryString($queryParams)];
}
$apiRequest = $this->prepareRequest('get-leads', '', [], $queryParams);
/** @var CloseIoResponse $result */
$result = $this->triggerGet($apiRequest);
if ($result->getReturnCode() == 200) {
$rawData = $result->getData()['data'];
foreach ($rawData as $lead) {
$leads[] = new Lead($lead);
}
}

return $leads;
}

/**
* @param array $params
*
* @return string
*/
private function buildQueryString(array $params)
{
$flattened = [];
foreach ($params as $key => $value) {
$flattened[] = $key . '=' . $value;
}
$queryString = implode('&', $flattened);
return $queryString;
}

/**
* @param $id
* @return Lead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getAllStatus()
$result = $this->triggerGet($apiRequest);

if ($result->getReturnCode() == 200) {
$rawData = $result->getData()[CloseIoResponse::GET_ALL_RESPONSE_LEADS_KEY];
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];
foreach ($rawData as $status) {
$statuses[] = new LeadStatus($status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getAllOpportunities()
$result = $this->triggerGet($apiRequest);

if ($result->getReturnCode() == 200) {
$rawData = $result->getData()[CloseIoResponse::GET_ALL_RESPONSE_LEADS_KEY];
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];
foreach ($rawData as $opportunity) {
$opportunities[] = new Opportunity($opportunity);
}
Expand Down
11 changes: 11 additions & 0 deletions src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace LooplineSystems\CloseIoApiWrapper;

use LooplineSystems\CloseIoApiWrapper\Api\CustomFieldApi;
use LooplineSystems\CloseIoApiWrapper\Api\LeadApi;
use LooplineSystems\CloseIoApiWrapper\Api\LeadStatusApi;
use LooplineSystems\CloseIoApiWrapper\Api\OpportunityApi;
Expand Down Expand Up @@ -47,6 +48,7 @@ protected function initApiHandler(CloseIoConfig $config)
$apiHandler->setApi(new LeadApi($apiHandler));
$apiHandler->setApi(new OpportunityApi($apiHandler));
$apiHandler->setApi(new LeadStatusApi($apiHandler));
$apiHandler->setApi(new CustomFieldApi($apiHandler));

return $apiHandler;
}
Expand All @@ -60,6 +62,15 @@ public function getLeadApi()
return $this->apiHandler->getApi(LeadApi::NAME);
}

/**
* @return CustomFieldApi
* @throws Library\Exception\ApiNotFoundException
*/
public function getCustomFieldApi()
{
return $this->apiHandler->getApi(CustomFieldApi::NAME);
}

/**
* @return OpportunityApi
* @throws Library\Exception\ApiNotFoundException
Expand Down
2 changes: 1 addition & 1 deletion src/LooplineSystems/CloseIoApiWrapper/CloseIoResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class CloseIoResponse
{

const GET_ALL_RESPONSE_LEADS_KEY = 'data';
const GET_RESPONSE_DATA_KEY = 'data';
const GET_ALL_RESPONSE_HAS_MORE_KEY = 'has_more';
const GET_ALL_RESPONSE_TOTAL_RESULTS_KEY = 'total_results';

Expand Down
Loading

0 comments on commit 16b349b

Please sign in to comment.