This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Endpoint.php
296 lines (255 loc) · 9 KB
/
Endpoint.php
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php namespace ProsperWorks\Endpoints;
use ProsperWorks\Resources\BareResource;
/**
* Represents a CRM API Endpoint.
* @author igorsantos07
*/
class Endpoint extends BaseEndpoint
{
/**
* Creates a new entry at this Endpoint.
* @param array $data
* @return object The data as processed by the CRM.
*/
public function create(array $data)
{
$relations = !empty($data['relations']) ? $data['relations'] : [];
unset($data['relations']);
$result = $this->request('post', '', ['json' => $data]);
foreach ($relations as $relation) {
if (!empty($relation['id'])) {
$this->related($result->id)->create($relation['id'], $relation['type']);
}
}
return $result;
}
/** @noinspection PhpInconsistentReturnPointsInspection
* Creates a generator wrapper based on a list of entries, setting each as value of a "json" key in an array.
* @param array|\Traversable $entries
* @return \Generator
*/
private function entriesJsonifier($entries)
{
return (function ($entries) {
foreach ($entries as $entry) {
yield ['json' => $entry];
}
})($entries);
}
/**
* Creates many entries at once.
* @param array[]|\Traversable $entries List of arrays, each defining data for one entry.
* @return BareResource[] All entries, as processed by the CRM.
*/
public function createMany($entries)
{
// first extract the relations wich should be managed through a side request
$relations = [];
$entriesGenerator = function($entries) use (&$relations) {
foreach ($entries as $entry) {
//$tm2id = null;
/*foreach ($entry['custom_fields'] as $customfield) {
if (is_object($customfield)) {
if ($customfield->name == "TM2 ID") {
$tm2id = $customfield->getValue();
break;
}
}
}*/
if (isset($entry['relations'])) {
//$relations[$tm2id] = $entry['relations'];
unset($entry['relations']);
}
yield $entry;
}
};
$results = $this->request('post', $this->entriesJsonifier($entriesGenerator($entries)));
/*$pwlinkedRelations = [];
foreach ($results as $result) {
if ( !empty($result->custom_fields['TM2 ID']) && !empty($relations[$result->custom_fields['TM2 ID']->getValue()]) ) {
$pwlinkedRelations[$result->id] = & $relations[$result->custom_fields['TM2 ID']->getValue()];
}
}
$this->relatedBatch()->create($pwlinkedRelations);*/
return $results;
}
/**
* Finds one entry, per ID.
* @param int|int[] $id
* @return BareResource|BareResource[]
*/
public function find(int ...$id)
{
return $this->request('get', $id);
}
/**
* Returns all entries at this resource.
* HACKY ALERT: Calls {@link search()} on resources that do not support plain GET calls like we do here.
* @return BareResource[]
*/
public function all()
{
try {
return $this->request('get');
} catch (\RuntimeException $e) {
if ($e->getCode() == 404) {
//aha! we may try to get all resources anyway with a sneaky empty /search call
return $this->search();
} else {
throw $e;
}
}
}
/**
* Runs /search on the resource.
* @param array $params Search fields to query for.
* @param int $size The amount of items to return at a single query. The maximum number is 200.
* @param int|null $page The page to be returned. If null, loops and gets all possible pages.
* @todo if we could get the total amount of entries we could infer the number of pages and run concurrent calls
* @return BareResource[]
*/
public function search(array $params = [], int $size = 200, int $page = null)
{
$allPages = !$page;
$params['page_number'] = $page ?? 1;
$params['page_size'] = ($size > 200)? 200 : $size;
//FIXME there's some bug on the PW API that returns fewer entries than the requested...
//so we're creating a margin to define when it should be safe to stop requesting pages
$safeMargin = ($size > 20)? 0.9 : 0.5; //trial-and-error-based guess
$safeLimit = floor($params['page_size'] * $safeMargin);
$entries = [];
do {
$results = $this->request('post', 'search', ['json' => $params]);
$entries = array_merge($entries, is_object($results)? [$results] : $results);
} while ($allPages && sizeof($results) >= $safeLimit && ++$params['page_number']);
return $entries;
}
/**
* Updates information in one entry.
* @param int $id
* @param array $data
* @return BareResource
*/
public function edit(int $id, array $data)
{
$relations = !empty($data['relations']) ? $data['relations'] : [];
unset($data['relations']);
$result = $this->request('put', $id, ['json' => $data]);
foreach ($relations as $relation) {
if (!empty($relation['id'])) {
$this->related($id)->create($relation['id'], $relation['type']);
}
}
return $result;
}
/**
* Allows to update many entries at once.
* @param array|\Traversable $entries An array of fields to update, indexed by the entry ID
* @return BareResource[]
*/
public function editMany($entries)
{
$relations = [];
foreach ($entries as $entry) {
$tm2id = null;
foreach ($entry['custom_fields'] as $customfield) {
if ($customfield->name == "TM2 ID") {
$tm2id = $customfield->getValue();
break;
}
}
if (isset($entry['relations'])) {
$relations[$tm2id] = $entry['relations'];
unset($entry['relations']);
}
}
$results = $this->request('put', $this->entriesJsonifier($entries));
foreach ($results as $result) {
if (!empty($relations[$result->custom_fields['TM2 ID']->getValue()])) {
foreach ($relations[$result->custom_fields['TM2 ID']->getValue()] as $relation) {
$this->related($result->id)->create($relation['id'], $relation['type']);
}
}
}
return $results;
}
/**
* Removes an entry.
* @param int|int[] $id
* @return bool|bool[]
*/
public function delete(int ...$id)
{
$raw = $this->request('delete', $id);
/** @noinspection PhpUndefinedFieldInspection */
return is_array($raw)?
array_map(function ($r) { return $r->is_deleted ?? false; }, $raw) :
$raw->is_deleted;
}
/**
* Returns a sub-resource, related to the current one.
* @example CRM::tasks->related(99)->fetch(); // GET tasks/99/related
* @example CRM::lead->relatedTasks(88)->fetch(); // GET leads/88/related/tasks
* @see https://www.prosperworks.com/developer_api/related_items
*
* @param int $id This resource's id
* @return RelatedEndpoint
*/
public function related(int $id)
{
return new RelatedEndpoint($this->uri, $id, $this->client);
}
public function relatedBatch()
{
return new BatchRelatedEndpoint($this->uri, $this->client);
}
/**
* Allows for fancy fetch calls.
* @example $leads = CRM::lead(); // GET leads
* @example $leads = CRM::lead(int $id); // GET leads/{id}
* @example $leads = CRM::lead(array $searchParams); // GET leads/search {$searchParams}
*
* @param int|array $param Runs {@link find()} if int and {@link search()} if array.
* @return BareResource|BareResource[]
*/
public function __invoke($param = null)
{
switch (gettype($param)) {
case 'integer':
return $this->find($param);
case 'array':
return $this->search($param);
//so fancy it gets weird: "CRM::task()()". Got removed in favor of plural calls
//case 'NULL':
// return $this->all();
default:
throw new \BadMethodCallException("Unknown desired operation with magic Endpoint $this->uri($param) invocation");
}
}
/**
* Magic call of specific Endpoint methods.
* An optional first parameter as integer will be converted into an ID in the requested path.
* @example CRM::lead()->convert(10, [...]); // POST leads/10/convert {...}
* @example CRM::person()->search([...]); // POST people/search {...}
* @param $method
* @param $params
* @return bool|object
*/
public function __call($method, $params)
{
$method = strtolower($method);
if (isset($params[0]) && is_int($params[0])) {
$method = "$params[0]/$method";
$options = $params[1] ?? [];
} else {
$options = $params[0] ?? [];
}
//forcing encoding of POST/PUT bodies and simplifying their structure
if ($options) {
if (!isset($options['json']) && !isset($options['body'])) {
$options = ['json' => $options];
}
}
return $this->request('post', $method, $options);
}
}