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

Commit

Permalink
Merge pull request #12 from dlimars/develop
Browse files Browse the repository at this point in the history
Add Lead Statuses
  • Loading branch information
Marco Roßdeutscher committed Mar 8, 2016
2 parents a8643bf + ddccd5e commit ec53b12
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 0 deletions.
129 changes: 129 additions & 0 deletions src/LooplineSystems/CloseIoApiWrapper/Api/LeadStatusApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace LooplineSystems\CloseIoApiWrapper\Api;


use LooplineSystems\CloseIoApiWrapper\CloseIoResponse;
use LooplineSystems\CloseIoApiWrapper\Library\Api\AbstractApi;
use LooplineSystems\CloseIoApiWrapper\Library\Exception\InvalidParamException;
use LooplineSystems\CloseIoApiWrapper\Library\Exception\ResourceNotFoundException;
use LooplineSystems\CloseIoApiWrapper\Model\LeadStatus;

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

/**
* {@inheritdoc}
*/
protected function initUrls()
{
$this->urls = [
'get-statuses' => '/status/lead/',
'add-status' => '/status/lead/',
'get-status' => '/status/lead/[:id]/',
'update-status' => '/status/lead/[:id]/',
'delete-status' => '/status/lead/[:id]/'
];
}

/**
* @param LeadStatus $status
* @return LeadStatus
*/
public function addStatus(LeadStatus $status)
{
$status = json_encode($status);
$apiRequest = $this->prepareRequest('add-status', $status);
$response = $this->triggerPost($apiRequest);
return new LeadStatus($response->getData());
}

/**
* @param LeadStatus $status
* @return LeadStatus|string
* @throws InvalidParamException
* @throws ResourceNotFoundException
*/
public function updateStatus(LeadStatus $status)
{
if ($status->getId() == null) {
throw new InvalidParamException('When updating a status you must provide the statuses ID');
}

$id = $status->getId();
$status->setId(null);

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

// return Lead object if successful
if ($response->getReturnCode() == 200 && ($response->getData() !== null)) {
$status = new LeadStatus($response->getData());
} else {
throw new ResourceNotFoundException();
}
return $status;
}

/**
* @return LeadStatus[]
*/
public function getAllStatus()
{
/** @var LeadStatus[] $statuses */
$statuses = array();

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

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

if ($result->getReturnCode() == 200) {
$rawData = $result->getData()[CloseIoResponse::GET_ALL_RESPONSE_LEADS_KEY];
foreach ($rawData as $status) {
$statuses[] = new LeadStatus($status);
}
}
return $statuses;
}

/**
* @param $id
* @return LeadStatus
* @throws ResourceNotFoundException
*/
public function getStatus($id)
{
$apiRequest = $this->prepareRequest('get-status', null, ['id' => $id]);

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

if ($result->getReturnCode() == 200 && ($result->getData() !== null)) {
$status = new LeadStatus($result->getData());
} else {
throw new ResourceNotFoundException();
}
return $status;
}

/**
* @param $id
* @return CloseIoResponse
* @throws ResourceNotFoundException
*/
public function deleteStatus($id){
$apiRequest = $this->prepareRequest('delete-status', null, ['id' => $id]);

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

if ($result->getReturnCode() == 200) {
return $result;
} else {
throw new ResourceNotFoundException();
}
}
}
11 changes: 11 additions & 0 deletions src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace LooplineSystems\CloseIoApiWrapper;

use LooplineSystems\CloseIoApiWrapper\Api\LeadApi;
use LooplineSystems\CloseIoApiWrapper\Api\LeadStatusApi;
use LooplineSystems\CloseIoApiWrapper\Api\OpportunityApi;
use LooplineSystems\CloseIoApiWrapper\Library\Api\ApiHandler;

Expand Down Expand Up @@ -45,6 +46,7 @@ protected function initApiHandler(CloseIoConfig $config)
$apiHandler = new ApiHandler($config);
$apiHandler->setApi(new LeadApi($apiHandler));
$apiHandler->setApi(new OpportunityApi($apiHandler));
$apiHandler->setApi(new LeadStatusApi($apiHandler));

return $apiHandler;
}
Expand All @@ -67,6 +69,15 @@ public function getOpportunityApi()
return $this->apiHandler->getApi(OpportunityApi::NAME);
}

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

/**
* @return ApiHandler
*/
Expand Down
93 changes: 93 additions & 0 deletions src/LooplineSystems/CloseIoApiWrapper/Model/LeadStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace LooplineSystems\CloseIoApiWrapper\Model;


use LooplineSystems\CloseIoApiWrapper\Library\JsonSerializableHelperTrait;
use LooplineSystems\CloseIoApiWrapper\Library\ObjectHydrateHelperTrait;

class LeadStatus implements \JsonSerializable
{
use ObjectHydrateHelperTrait;
use JsonSerializableHelperTrait;

/**
* @var string
*/
private $label;

/**
* @var string
*/
private $id;

/**
* @var string
*/
private $organizationId;

/**
* LeadStatus constructor.
* @param array $data
*/
public function __construct(array $data)
{
if ($data) {
$this->hydrate($data);
}
}

/**
* @param $label
* @return LeadStatus
*/
public function setLabel($label)
{
$this->label = $label;
return $this;
}

/**
* @return string
*/
public function getLabel()
{
return $this->label;
}

/**
* @param $id
* @return LeadStatus
*/
public function setId($id)
{
$this->id = $id;
return $this;
}

/**
* @return string
*/
public function getId()
{
return $this->id;
}

/**
* @param string $organizationId
* @return LeadStatus
*/
public function setOrganizationId($organizationId)
{
$this->organizationId = $organizationId;
return $this;
}

/**
* @return string
*/
public function getOrganizationId()
{
return $this->organizationId;
}
}

0 comments on commit ec53b12

Please sign in to comment.