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

Commit ec53b12

Browse files
author
Marco Roßdeutscher
committed
Merge pull request #12 from dlimars/develop
Add Lead Statuses
2 parents a8643bf + ddccd5e commit ec53b12

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace LooplineSystems\CloseIoApiWrapper\Api;
4+
5+
6+
use LooplineSystems\CloseIoApiWrapper\CloseIoResponse;
7+
use LooplineSystems\CloseIoApiWrapper\Library\Api\AbstractApi;
8+
use LooplineSystems\CloseIoApiWrapper\Library\Exception\InvalidParamException;
9+
use LooplineSystems\CloseIoApiWrapper\Library\Exception\ResourceNotFoundException;
10+
use LooplineSystems\CloseIoApiWrapper\Model\LeadStatus;
11+
12+
class LeadStatusApi extends AbstractApi
13+
{
14+
const NAME = 'LeadStatusApi';
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
protected function initUrls()
20+
{
21+
$this->urls = [
22+
'get-statuses' => '/status/lead/',
23+
'add-status' => '/status/lead/',
24+
'get-status' => '/status/lead/[:id]/',
25+
'update-status' => '/status/lead/[:id]/',
26+
'delete-status' => '/status/lead/[:id]/'
27+
];
28+
}
29+
30+
/**
31+
* @param LeadStatus $status
32+
* @return LeadStatus
33+
*/
34+
public function addStatus(LeadStatus $status)
35+
{
36+
$status = json_encode($status);
37+
$apiRequest = $this->prepareRequest('add-status', $status);
38+
$response = $this->triggerPost($apiRequest);
39+
return new LeadStatus($response->getData());
40+
}
41+
42+
/**
43+
* @param LeadStatus $status
44+
* @return LeadStatus|string
45+
* @throws InvalidParamException
46+
* @throws ResourceNotFoundException
47+
*/
48+
public function updateStatus(LeadStatus $status)
49+
{
50+
if ($status->getId() == null) {
51+
throw new InvalidParamException('When updating a status you must provide the statuses ID');
52+
}
53+
54+
$id = $status->getId();
55+
$status->setId(null);
56+
57+
$status = json_encode($status);
58+
$apiRequest = $this->prepareRequest('update-status', $status, ['id' => $id]);
59+
$response = $this->triggerPut($apiRequest);
60+
61+
// return Lead object if successful
62+
if ($response->getReturnCode() == 200 && ($response->getData() !== null)) {
63+
$status = new LeadStatus($response->getData());
64+
} else {
65+
throw new ResourceNotFoundException();
66+
}
67+
return $status;
68+
}
69+
70+
/**
71+
* @return LeadStatus[]
72+
*/
73+
public function getAllStatus()
74+
{
75+
/** @var LeadStatus[] $statuses */
76+
$statuses = array();
77+
78+
$apiRequest = $this->prepareRequest('get-statuses');
79+
80+
/** @var CloseIoResponse $result */
81+
$result = $this->triggerGet($apiRequest);
82+
83+
if ($result->getReturnCode() == 200) {
84+
$rawData = $result->getData()[CloseIoResponse::GET_ALL_RESPONSE_LEADS_KEY];
85+
foreach ($rawData as $status) {
86+
$statuses[] = new LeadStatus($status);
87+
}
88+
}
89+
return $statuses;
90+
}
91+
92+
/**
93+
* @param $id
94+
* @return LeadStatus
95+
* @throws ResourceNotFoundException
96+
*/
97+
public function getStatus($id)
98+
{
99+
$apiRequest = $this->prepareRequest('get-status', null, ['id' => $id]);
100+
101+
/** @var CloseIoResponse $result */
102+
$result = $this->triggerGet($apiRequest);
103+
104+
if ($result->getReturnCode() == 200 && ($result->getData() !== null)) {
105+
$status = new LeadStatus($result->getData());
106+
} else {
107+
throw new ResourceNotFoundException();
108+
}
109+
return $status;
110+
}
111+
112+
/**
113+
* @param $id
114+
* @return CloseIoResponse
115+
* @throws ResourceNotFoundException
116+
*/
117+
public function deleteStatus($id){
118+
$apiRequest = $this->prepareRequest('delete-status', null, ['id' => $id]);
119+
120+
/** @var CloseIoResponse $result */
121+
$result = $this->triggerDelete($apiRequest);
122+
123+
if ($result->getReturnCode() == 200) {
124+
return $result;
125+
} else {
126+
throw new ResourceNotFoundException();
127+
}
128+
}
129+
}

src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace LooplineSystems\CloseIoApiWrapper;
1111

1212
use LooplineSystems\CloseIoApiWrapper\Api\LeadApi;
13+
use LooplineSystems\CloseIoApiWrapper\Api\LeadStatusApi;
1314
use LooplineSystems\CloseIoApiWrapper\Api\OpportunityApi;
1415
use LooplineSystems\CloseIoApiWrapper\Library\Api\ApiHandler;
1516

@@ -45,6 +46,7 @@ protected function initApiHandler(CloseIoConfig $config)
4546
$apiHandler = new ApiHandler($config);
4647
$apiHandler->setApi(new LeadApi($apiHandler));
4748
$apiHandler->setApi(new OpportunityApi($apiHandler));
49+
$apiHandler->setApi(new LeadStatusApi($apiHandler));
4850

4951
return $apiHandler;
5052
}
@@ -67,6 +69,15 @@ public function getOpportunityApi()
6769
return $this->apiHandler->getApi(OpportunityApi::NAME);
6870
}
6971

72+
/**
73+
* @return LeadStatusApi
74+
* @throws Library\Exception\ApiNotFoundException
75+
*/
76+
public function getLeadStatusesApi()
77+
{
78+
return $this->apiHandler->getApi(LeadStatusApi::NAME);
79+
}
80+
7081
/**
7182
* @return ApiHandler
7283
*/
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace LooplineSystems\CloseIoApiWrapper\Model;
4+
5+
6+
use LooplineSystems\CloseIoApiWrapper\Library\JsonSerializableHelperTrait;
7+
use LooplineSystems\CloseIoApiWrapper\Library\ObjectHydrateHelperTrait;
8+
9+
class LeadStatus implements \JsonSerializable
10+
{
11+
use ObjectHydrateHelperTrait;
12+
use JsonSerializableHelperTrait;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $label;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $id;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $organizationId;
28+
29+
/**
30+
* LeadStatus constructor.
31+
* @param array $data
32+
*/
33+
public function __construct(array $data)
34+
{
35+
if ($data) {
36+
$this->hydrate($data);
37+
}
38+
}
39+
40+
/**
41+
* @param $label
42+
* @return LeadStatus
43+
*/
44+
public function setLabel($label)
45+
{
46+
$this->label = $label;
47+
return $this;
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function getLabel()
54+
{
55+
return $this->label;
56+
}
57+
58+
/**
59+
* @param $id
60+
* @return LeadStatus
61+
*/
62+
public function setId($id)
63+
{
64+
$this->id = $id;
65+
return $this;
66+
}
67+
68+
/**
69+
* @return string
70+
*/
71+
public function getId()
72+
{
73+
return $this->id;
74+
}
75+
76+
/**
77+
* @param string $organizationId
78+
* @return LeadStatus
79+
*/
80+
public function setOrganizationId($organizationId)
81+
{
82+
$this->organizationId = $organizationId;
83+
return $this;
84+
}
85+
86+
/**
87+
* @return string
88+
*/
89+
public function getOrganizationId()
90+
{
91+
return $this->organizationId;
92+
}
93+
}

0 commit comments

Comments
 (0)