diff --git a/README.md b/README.md index a738b1a..34e798e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/LooplineSystems/CloseIoApiWrapper/Api/CustomFieldApi.php b/src/LooplineSystems/CloseIoApiWrapper/Api/CustomFieldApi.php new file mode 100644 index 0000000..f1d1db9 --- /dev/null +++ b/src/LooplineSystems/CloseIoApiWrapper/Api/CustomFieldApi.php @@ -0,0 +1,79 @@ +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; + } +} \ No newline at end of file diff --git a/src/LooplineSystems/CloseIoApiWrapper/Api/LeadApi.php b/src/LooplineSystems/CloseIoApiWrapper/Api/LeadApi.php index 6d8172e..7c9c11a 100644 --- a/src/LooplineSystems/CloseIoApiWrapper/Api/LeadApi.php +++ b/src/LooplineSystems/CloseIoApiWrapper/Api/LeadApi.php @@ -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); @@ -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 diff --git a/src/LooplineSystems/CloseIoApiWrapper/Api/LeadStatusApi.php b/src/LooplineSystems/CloseIoApiWrapper/Api/LeadStatusApi.php index e5bf9c0..9a14fdf 100644 --- a/src/LooplineSystems/CloseIoApiWrapper/Api/LeadStatusApi.php +++ b/src/LooplineSystems/CloseIoApiWrapper/Api/LeadStatusApi.php @@ -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); } diff --git a/src/LooplineSystems/CloseIoApiWrapper/Api/OpportunityApi.php b/src/LooplineSystems/CloseIoApiWrapper/Api/OpportunityApi.php index 2f1e6c5..39e6831 100644 --- a/src/LooplineSystems/CloseIoApiWrapper/Api/OpportunityApi.php +++ b/src/LooplineSystems/CloseIoApiWrapper/Api/OpportunityApi.php @@ -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); } diff --git a/src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php b/src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php index 1a2297d..ee4e11e 100644 --- a/src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php +++ b/src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php @@ -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; @@ -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; } @@ -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 diff --git a/src/LooplineSystems/CloseIoApiWrapper/CloseIoResponse.php b/src/LooplineSystems/CloseIoApiWrapper/CloseIoResponse.php index 9e464eb..2f3e7a1 100644 --- a/src/LooplineSystems/CloseIoApiWrapper/CloseIoResponse.php +++ b/src/LooplineSystems/CloseIoApiWrapper/CloseIoResponse.php @@ -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'; diff --git a/src/LooplineSystems/CloseIoApiWrapper/Model/CustomField.php b/src/LooplineSystems/CloseIoApiWrapper/Model/CustomField.php new file mode 100644 index 0000000..37d8e6b --- /dev/null +++ b/src/LooplineSystems/CloseIoApiWrapper/Model/CustomField.php @@ -0,0 +1,255 @@ +hydrate($data, []); + } + } + + /** + * @return string + */ + public function getId() + { + return $this->id; + } + + /** + * @param string $id + */ + public function setId($id) + { + $this->id = $id; + + return $this; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * @return string + */ + public function getDateUpdated() + { + return $this->dateUpdated; + } + + /** + * @param string $dateUpdated + */ + public function setDateUpdated($dateUpdated) + { + $this->dateUpdated = $dateUpdated; + + return $this; + } + + /** + * @return string + */ + public function getCreatedBy() + { + return $this->createdBy; + } + + /** + * @param string $createdBy + */ + public function setCreatedBy($createdBy) + { + $this->createdBy = $createdBy; + + return $this; + } + + /** + * @return string + */ + public function getUpdatedBy() + { + return $this->updatedBy; + } + + /** + * @param string $updatedBy + */ + public function setUpdatedBy($updatedBy) + { + $this->updatedBy = $updatedBy; + + return $this; + } + + /** + * @return string[] + */ + public function getChoices() + { + return $this->choices; + } + + /** + * @param string[] $choices + */ + public function setChoices($choices) + { + $this->choices = $choices; + + return $this; + } + + /** + * @param $choice + */ + public function addChoice($choice) + { + if (!in_array($choice, $this->choices)) { + $this->choices[] = $choice; + } + + return $this; + } + + /** + * @param $choice + */ + public function removeChoice($choice) + { + $index = array_search($choice, $this->choices); + + if ($index !== false) { + unset($this->choices[$index]); + } + + return $this; + } + + /** + * @return string + */ + public function getOrganizationId() + { + return $this->organization_id; + } + + /** + * @param string $organization_id + */ + public function setOrganizationId($organization_id) + { + $this->organization_id = $organization_id; + + return $this; + } + + /** + * @return string + */ + public function getDateCreated() + { + return $this->date_created; + } + + /** + * @param string $date_created + */ + public function setDateCreated($date_created) + { + $this->date_created = $date_created; + + return $this; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @param string $type + */ + public function setType($type) + { + $this->type = $type; + + return $this; + } +} \ No newline at end of file diff --git a/src/LooplineSystems/CloseIoApiWrapper/Model/Lead.php b/src/LooplineSystems/CloseIoApiWrapper/Model/Lead.php index 2810250..1b50b29 100644 --- a/src/LooplineSystems/CloseIoApiWrapper/Model/Lead.php +++ b/src/LooplineSystems/CloseIoApiWrapper/Model/Lead.php @@ -33,6 +33,11 @@ class Lead implements \JsonSerializable */ private $status_id; + /** + * @var string + */ + private $status; + /* * @var string */ @@ -397,6 +402,24 @@ public function setStatusId($status) return $this; } + /** + * @return string + */ + public function getStatus() + { + return $this->status; + } + + /** + * @param string $status + */ + public function setStatus($status) + { + $this->status = $status; + + return $this; + } + /** * @return string */ diff --git a/tests/LooplineSystems/CloseIoApi/Api/CustomFieldApiTest.php b/tests/LooplineSystems/CloseIoApi/Api/CustomFieldApiTest.php new file mode 100755 index 0000000..a1ec1be --- /dev/null +++ b/tests/LooplineSystems/CloseIoApi/Api/CustomFieldApiTest.php @@ -0,0 +1,153 @@ + false, + CloseIoResponse::GET_ALL_RESPONSE_TOTAL_RESULTS_KEY => '3', + CloseIoResponse::GET_RESPONSE_DATA_KEY => $customFieldArray + ]; + + $customFieldApi = $this->getCustomFieldApi(); + + $expectedResponse = new CloseIoResponse(); + $expectedResponse->setReturnCode(200); + $expectedResponse->setRawData(json_encode($responseBody)); + $expectedResponse->setData(json_decode($expectedResponse->getRawData(), true)); + + // create stub + $mockCurl = $this->getMockResponderCurl($expectedResponse); + $customFieldApi->setCurl($mockCurl); + + $returnedcustomFields = $customFieldApi->getAllCustomFields(); + + foreach ($returnedcustomFields as $key => $customField) { + $this->assertTrue($customField == $customFieldArray[$key]); + } + } + + /** + * @test + * @dataProvider customFieldDataProvider + * + * @param CustomField $customField + */ + public function testUpdateCustomField($customField) + { + $customFieldApi = $this->getCustomFieldApi(); + + $customField->setId('TestId'); + $originalCustomField = clone $customField; + + $returnedCustomField = $customField; + $returnedCustomField->setName('Test Name'); + + // init expected response + $expectedResponse = new CloseIoResponse(); + $expectedResponse->setReturnCode('200'); + $expectedResponse->setRawData(json_encode($returnedCustomField)); + $expectedResponse->setData(json_decode($expectedResponse->getRawData(), true)); + + // create stub + $mockCurl = $this->getMockResponderCurl($expectedResponse); + $customFieldApi->setCurl($mockCurl); + + /* @var CustomField $response */ + $response = $customFieldApi->updateCustomField($customField); + + $this->assertEquals($customField->getName(), $response->getName()); + $this->assertEquals($originalCustomField->getId(), $response->getId()); + } + + /** + * @return CustomFieldApi + */ + private function getCustomFieldApi() + { + // init wrapper + $closeIoConfig = new CloseIoConfig(); + $closeIoConfig->setApiKey('testapikey'); + $closeIoApiWrapper = new CloseIoApiWrapper($closeIoConfig); + + return $closeIoApiWrapper->getCustomFieldApi(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject + * @description Need to be careful of the order, if method() comes after expects() it will return null + */ + private function getMockResponderCurl($expectedResponse) + { + // create stub + $mockCurl = $this->getMockBuilder('Curl') + ->setMethods(['getResponse']) + ->getMock(); + + // configure the stub. + $mockCurl->method('getResponse')->willReturn($expectedResponse); + + $mockCurl->expects($this->once()) + ->method('getResponse'); + + return $mockCurl; + } + + /** + * @return array + */ + public function customFieldDataProvider() + { + return [ + [ + new CustomField(['name'=>'Test Name']) + ] + ]; + } + + /** + * @return array + */ + public function customFieldArrayProvider() + { + $customField = new CustomField(['name' => 'Test Name']); + + $customFieldOne = clone $customField; + $customFieldTwo = clone $customField; + $customFieldThree = clone $customField; + + $customFieldOne->setId('TestIdOne'); + $customFieldTwo->setId('TestIdTwo'); + $customFieldThree->setId('TestIdThree'); + + $customFields = [ + $customFieldOne, + $customFieldTwo, + $customFieldThree + ]; + + return [[$customFields]]; + } +} + diff --git a/tests/LooplineSystems/CloseIoApi/Api/LeadsApiTest.php b/tests/LooplineSystems/CloseIoApi/Api/LeadsApiTest.php index fff3f4f..ab6b9d6 100755 --- a/tests/LooplineSystems/CloseIoApi/Api/LeadsApiTest.php +++ b/tests/LooplineSystems/CloseIoApi/Api/LeadsApiTest.php @@ -78,7 +78,7 @@ public function testGetAllLeads($leadsArray) $responseBody = [ CloseIoResponse::GET_ALL_RESPONSE_HAS_MORE_KEY => false, CloseIoResponse::GET_ALL_RESPONSE_TOTAL_RESULTS_KEY => '3', - CloseIoResponse::GET_ALL_RESPONSE_LEADS_KEY => $leadsArray + CloseIoResponse::GET_RESPONSE_DATA_KEY => $leadsArray ]; $leadsApi = $this->getLeadsApi();