This repository was archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathOpportunityApi.php
More file actions
224 lines (191 loc) · 6.93 KB
/
OpportunityApi.php
File metadata and controls
224 lines (191 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Close.io Api Wrapper - LLS Internet GmbH - Loopline Systems.
*
* @see https://github.com/loopline-systems/closeio-api-wrapper for the canonical source repository
*
* @copyright Copyright (c) 2014 LLS Internet GmbH - Loopline Systems (http://www.loopline-systems.com)
* @license https://github.com/loopline-systems/closeio-api-wrapper/blob/master/LICENSE (MIT Licence)
*/
declare(strict_types=1);
namespace LooplineSystems\CloseIoApiWrapper\Api;
use LooplineSystems\CloseIoApiWrapper\Library\Api\AbstractApi;
use LooplineSystems\CloseIoApiWrapper\Model\Opportunity;
class OpportunityApi extends AbstractApi
{
/**
* The maximum number of items that are requested by default.
*/
private const MAX_ITEMS_PER_REQUEST = 100;
/**
* {@inheritdoc}
*/
protected function initUrls()
{
$this->urls = [
'get-opportunities' => '/opportunity/',
'add-opportunity' => '/opportunity/',
'get-opportunity' => '/opportunity/[:id]/',
'update-opportunity' => '/opportunity/[:id]/',
'delete-opportunity' => '/opportunity/[:id]/',
];
}
/**
* Gets up to the specified number of opportunities that matches the given
* criteria.
*
* @param int $offset The offset from which start getting the items
* @param int $limit The maximum number of items to get
* @param array $filters A set of criteria to filter the items by
* @param string[] $fields The subset of fields to get (defaults to all)
*
* @return Opportunity[]
*/
public function list(int $offset = 0, int $limit = self::MAX_ITEMS_PER_REQUEST, array $filters = [], array $fields = []): array
{
/** @var Opportunity[] $opportunities */
$opportunities = [];
$response = $this->client->get($this->prepareUrlForKey('get-opportunities'), array_merge($filters, [
'_skip' => $offset,
'_limit' => $limit,
'_fields' => $fields,
]));
$responseData = $response->getDecodedBody();
foreach ($responseData['data'] as $opportunity) {
$opportunities[] = new Opportunity($opportunity);
}
return $opportunities;
}
/**
* Gets the information about the opportunity that matches the given ID.
*
* @param string $id The ID of the opportunity
* @param string[] $fields The subset of fields to get (defaults to all)
*
* @return Opportunity
*/
public function get(string $id, array $fields = []): Opportunity
{
$response = $this->client->get($this->prepareUrlForKey('get-opportunity', ['id' => $id]), ['_fields' => $fields]);
return new Opportunity($response->getDecodedBody());
}
/**
* Creates a new opportunity using the given information.
*
* @param Opportunity $opportunity The information of the opportunity to
* create
*
* @return Opportunity
*/
public function create(Opportunity $opportunity): Opportunity
{
$response = $this->client->post($this->prepareUrlForKey('add-opportunity'), [], $opportunity->jsonSerialize());
$responseData = $response->getDecodedBody();
return new Opportunity($responseData);
}
/**
* Updates the given opportunity.
*
* @param Opportunity $opportunity The opportunity to update
*
* @return Opportunity
*/
public function update(Opportunity $opportunity): Opportunity
{
$id = $opportunity->getId();
$opportunity->setId(null);
$response = $this->client->put($this->prepareUrlForKey('update-opportunity', ['id' => $id]), [], $opportunity->jsonSerialize());
$responseData = $response->getDecodedBody();
return new Opportunity($responseData);
}
/**
* Deletes the given opportunity.
*
* @param Opportunity $opportunity The opportunity to delete
*/
public function delete(Opportunity $opportunity): void
{
$id = $opportunity->getId();
$opportunity->setId(null);
$this->client->delete($this->prepareUrlForKey('delete-opportunity', ['id' => $id]));
}
/**
* Gets all the opportunities.
*
* @return Opportunity[]
*
* @deprecated since version 0.8, to be removed in 0.9. Use list() instead
*/
public function getAllOpportunities(): array
{
@trigger_error(sprintf('The %s() method is deprecated since version 0.8. Use list() instead.', __METHOD__), E_USER_DEPRECATED);
return $this->list();
}
/**
* Gets the information about the opportunity that matches the given ID.
*
* @param string $opportunityId The ID of the opportunity
*
* @return Opportunity
*
* @deprecated since version 0.8, to be removed in 0.9. Use get() instead
*/
public function getOpportunity($opportunityId): Opportunity
{
@trigger_error(sprintf('The %s() method is deprecated since version 0.8. Use get() instead.', __METHOD__), E_USER_DEPRECATED);
return $this->get($opportunityId);
}
/**
* Creates a new opportunity using the given information.
*
* @param Opportunity $opportunity The information of the opportunity to
* create
*
* @return Opportunity
*
* @deprecated since version 0.8, to be removed in 0.9. Use create() instead
*/
public function addOpportunity(Opportunity $opportunity): Opportunity
{
@trigger_error(sprintf('The %s() method is deprecated since version 0.8. Use create() instead.', __METHOD__), E_USER_DEPRECATED);
return $this->create($opportunity);
}
/**
* Updates the given opportunity.
*
* @param Opportunity $opportunity The opportunity to update
*
* @return Opportunity
*/
public function updateOpportunity(Opportunity $opportunity): Opportunity
{
@trigger_error(sprintf('The %s() method is deprecated since version 0.8. Use update() instead.', __METHOD__), E_USER_DEPRECATED);
return $this->update($opportunity);
}
/**
* Deletes the given opportunity.
*
* @param string $id The ID of the opportunity to delete
*
* @deprecated since version 0.8, to be removed in 0.9. Use delete() instead
*/
public function deleteOpportunity(string $id): void
{
@trigger_error(sprintf('The %s() method is deprecated since version 0.8. Use delete() instead.', __METHOD__), E_USER_DEPRECATED);
$this->client->delete($this->prepareUrlForKey('delete-opportunity', ['id' => $id]));
}
/**
* @param array $params
*
* @return string
*/
private function buildQueryString(array $params)
{
$flattened = [];
foreach ($params as $key => $value) {
$flattened[] = $key . '=' . $value;
}
$queryString = implode('&', $flattened);
return $queryString;
}
}