1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/hubspot/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/hubspot/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\hubspot\services\resources; |
10
|
|
|
|
11
|
|
|
use flipbox\hubspot\builders\ContactListContactsBuilder; |
12
|
|
|
use flipbox\hubspot\builders\ObjectBuilderInterface; |
13
|
|
|
use flipbox\hubspot\connections\ConnectionInterface; |
14
|
|
|
use flipbox\hubspot\criteria\ContactListContactsCriteria; |
15
|
|
|
use flipbox\hubspot\criteria\ObjectCriteriaInterface; |
16
|
|
|
use flipbox\hubspot\helpers\CacheHelper; |
17
|
|
|
use flipbox\hubspot\helpers\ConnectionHelper; |
18
|
|
|
use flipbox\hubspot\helpers\TransformerHelper; |
19
|
|
|
use flipbox\hubspot\HubSpot; |
20
|
|
|
use flipbox\hubspot\pipeline\Resource; |
21
|
|
|
use flipbox\hubspot\services\resources\traits\ReadObjectTrait; |
22
|
|
|
use flipbox\hubspot\transformers\collections\DynamicTransformerCollection; |
23
|
|
|
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface; |
24
|
|
|
use flipbox\hubspot\transformers\DynamicModelSuccess; |
25
|
|
|
use Flipbox\Relay\Builder\RelayBuilderInterface; |
26
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\Contacts\Add; |
27
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\Contacts\All; |
28
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\Contacts\Remove; |
29
|
|
|
use League\Pipeline\PipelineBuilderInterface; |
30
|
|
|
use Psr\Http\Message\ResponseInterface; |
31
|
|
|
use Psr\SimpleCache\CacheInterface; |
32
|
|
|
use yii\base\Component; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @author Flipbox Factory <[email protected]> |
36
|
|
|
* @since 1.0.0 |
37
|
|
|
*/ |
38
|
|
|
class ContactListContacts extends Component |
39
|
|
|
{ |
40
|
|
|
use ReadObjectTrait; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The HubSpot Resource name |
44
|
|
|
*/ |
45
|
|
|
const HUBSPOT_RESOURCE = 'contactListContacts'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public static function defaultTransformer() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
'class' => DynamicTransformerCollection::class, |
54
|
|
|
'handle' => self::HUBSPOT_RESOURCE, |
55
|
|
|
'transformers' => [ |
56
|
|
|
TransformerCollectionInterface::SUCCESS_KEY => [ |
57
|
|
|
'class' => DynamicModelSuccess::class, |
58
|
|
|
'resource' => self::HUBSPOT_RESOURCE |
59
|
|
|
] |
60
|
|
|
] |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $config |
66
|
|
|
* @return ObjectCriteriaInterface |
67
|
|
|
*/ |
68
|
|
|
public function getCriteria(array $config = []): ObjectCriteriaInterface |
69
|
|
|
{ |
70
|
|
|
return new ContactListContactsCriteria($config); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $config |
75
|
|
|
* @return ObjectBuilderInterface |
76
|
|
|
*/ |
77
|
|
|
public function getBuilder(array $config = []): ObjectBuilderInterface |
78
|
|
|
{ |
79
|
|
|
return new ContactListContactsBuilder($config); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
|
|
protected static function readRelayBuilderClass(): string |
86
|
|
|
{ |
87
|
|
|
return All::class; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
|
|
protected static function addRelayBuilderClass(): string |
94
|
|
|
{ |
95
|
|
|
return Add::class; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
|
|
protected static function removeRelayBuilderClass(): string |
102
|
|
|
{ |
103
|
|
|
return Remove::class; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/******************************************* |
108
|
|
|
* ADD |
109
|
|
|
*******************************************/ |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param ObjectBuilderInterface $builder |
113
|
|
|
* @param ConnectionInterface|string|null $connection |
114
|
|
|
* @param CacheInterface|string|null $cache |
115
|
|
|
* @param TransformerCollectionInterface|array|null $transformer |
116
|
|
|
* @param null $source |
117
|
|
|
* @return mixed |
118
|
|
|
* @throws \yii\base\InvalidConfigException |
119
|
|
|
*/ |
120
|
|
|
public function add( |
121
|
|
|
ObjectBuilderInterface $builder, |
122
|
|
|
ConnectionInterface $connection = null, |
123
|
|
|
CacheInterface $cache = null, |
124
|
|
|
TransformerCollectionInterface $transformer = null, |
125
|
|
|
$source = null |
126
|
|
|
) { |
127
|
|
|
return $this->rawAdd( |
128
|
|
|
$builder->getId(), |
129
|
|
|
$builder->getPayload(), |
130
|
|
|
$connection, |
131
|
|
|
$cache, |
132
|
|
|
$transformer, |
133
|
|
|
$source |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $id |
139
|
|
|
* @param array $payload |
140
|
|
|
* @param ConnectionInterface|string|null $connection |
141
|
|
|
* @param CacheInterface|string|null $cache |
142
|
|
|
* @param TransformerCollectionInterface|array|null $transformer |
143
|
|
|
* @param null $source |
144
|
|
|
* @return mixed |
145
|
|
|
* @throws \yii\base\InvalidConfigException |
146
|
|
|
*/ |
147
|
|
|
public function rawAdd( |
148
|
|
|
string $id, |
149
|
|
|
array $payload, |
150
|
|
|
ConnectionInterface $connection = null, |
151
|
|
|
CacheInterface $cache = null, |
152
|
|
|
TransformerCollectionInterface $transformer = null, |
153
|
|
|
$source = null |
154
|
|
|
) { |
155
|
|
|
return $this->rawAddPipeline( |
156
|
|
|
$id, |
157
|
|
|
$payload, |
158
|
|
|
$connection, |
159
|
|
|
$cache, |
160
|
|
|
$transformer |
161
|
|
|
)($source); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param ObjectBuilderInterface $builder |
166
|
|
|
* @param ConnectionInterface|string|null $connection |
167
|
|
|
* @param CacheInterface|string|null $cache |
168
|
|
|
* @param TransformerCollectionInterface|array|null $transformer |
169
|
|
|
* @return PipelineBuilderInterface |
170
|
|
|
* @throws \yii\base\InvalidConfigException |
171
|
|
|
*/ |
172
|
|
|
public function addPipeline( |
173
|
|
|
ObjectBuilderInterface $builder, |
174
|
|
|
ConnectionInterface $connection = null, |
175
|
|
|
CacheInterface $cache = null, |
176
|
|
|
TransformerCollectionInterface $transformer = null |
177
|
|
|
): PipelineBuilderInterface { |
178
|
|
|
return $this->rawAddPipeline( |
179
|
|
|
$builder->getId(), |
180
|
|
|
$builder->getPayload(), |
181
|
|
|
$connection, |
182
|
|
|
$cache, |
183
|
|
|
$transformer |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $id |
189
|
|
|
* @param array $payload |
190
|
|
|
* @param ConnectionInterface|string|null $connection |
191
|
|
|
* @param CacheInterface|string|null $cache |
192
|
|
|
* @param TransformerCollectionInterface|array|null $transformer |
193
|
|
|
* @return PipelineBuilderInterface |
194
|
|
|
* @throws \yii\base\InvalidConfigException |
195
|
|
|
*/ |
196
|
|
|
public function rawAddPipeline( |
197
|
|
|
string $id, |
198
|
|
|
array $payload, |
199
|
|
|
ConnectionInterface $connection = null, |
200
|
|
|
CacheInterface $cache = null, |
201
|
|
|
TransformerCollectionInterface $transformer = null |
202
|
|
|
): PipelineBuilderInterface { |
203
|
|
|
$transformer = TransformerHelper::populateTransformerCollection( |
204
|
|
|
TransformerHelper::resolveCollection($transformer), |
205
|
|
|
[ |
206
|
|
|
'resource' => [static::addRelayBuilderClass()] |
207
|
|
|
] |
208
|
|
|
); |
209
|
|
|
|
210
|
|
|
return (new Resource( |
211
|
|
|
$this->rawHttpAddRelay( |
212
|
|
|
$id, |
213
|
|
|
$payload, |
214
|
|
|
ConnectionHelper::resolveConnection($connection), |
215
|
|
|
CacheHelper::resolveCache($cache) |
216
|
|
|
), |
217
|
|
|
$transformer, |
218
|
|
|
HubSpot::getInstance()->getPsrLogger() |
219
|
|
|
)); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param ObjectBuilderInterface $builder |
224
|
|
|
* @param ConnectionInterface|string|null $connection |
225
|
|
|
* @param CacheInterface|string|null $cache |
226
|
|
|
* @return callable |
227
|
|
|
* @throws \yii\base\InvalidConfigException |
228
|
|
|
*/ |
229
|
|
|
public function httpAddRelay( |
230
|
|
|
ObjectBuilderInterface $builder, |
231
|
|
|
ConnectionInterface $connection = null, |
232
|
|
|
CacheInterface $cache = null |
233
|
|
|
): callable { |
234
|
|
|
return $this->rawHttpAddRelay( |
235
|
|
|
$builder->getId(), |
236
|
|
|
$builder->getPayload(), |
237
|
|
|
$connection, |
238
|
|
|
$cache |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $id |
244
|
|
|
* @param array $payload |
245
|
|
|
* @param ConnectionInterface|string|null $connection |
246
|
|
|
* @param CacheInterface|string|null $cache |
247
|
|
|
* @return callable |
248
|
|
|
* @throws \yii\base\InvalidConfigException |
249
|
|
|
*/ |
250
|
|
|
public function rawHttpAddRelay( |
251
|
|
|
string $id, |
252
|
|
|
array $payload, |
253
|
|
|
ConnectionInterface $connection = null, |
254
|
|
|
CacheInterface $cache = null |
255
|
|
|
): callable { |
256
|
|
|
$class = static::addRelayBuilderClass(); |
257
|
|
|
|
258
|
|
|
/** @var RelayBuilderInterface $builder */ |
259
|
|
|
$builder = new $class( |
260
|
|
|
$id, |
261
|
|
|
$payload, |
262
|
|
|
ConnectionHelper::resolveConnection($connection), |
263
|
|
|
CacheHelper::resolveCache($cache), |
264
|
|
|
HubSpot::getInstance()->getPsrLogger() |
265
|
|
|
); |
266
|
|
|
|
267
|
|
|
return $builder->build(); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param ObjectBuilderInterface $builder |
272
|
|
|
* @param ConnectionInterface|string|null $connection |
273
|
|
|
* @param CacheInterface|string|null $cache |
274
|
|
|
* @return ResponseInterface |
275
|
|
|
* @throws \yii\base\InvalidConfigException |
276
|
|
|
*/ |
277
|
|
|
public function httpAdd( |
278
|
|
|
ObjectBuilderInterface $builder, |
279
|
|
|
ConnectionInterface $connection = null, |
280
|
|
|
CacheInterface $cache = null |
281
|
|
|
): ResponseInterface { |
282
|
|
|
return $this->rawHttpAdd( |
283
|
|
|
$builder->getId(), |
284
|
|
|
$builder->getPayload(), |
285
|
|
|
$connection, |
286
|
|
|
$cache |
287
|
|
|
); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param string $id |
292
|
|
|
* @param array $payload |
293
|
|
|
* @param ConnectionInterface|string|null $connection |
294
|
|
|
* @param CacheInterface|string|null $cache |
295
|
|
|
* @return ResponseInterface |
296
|
|
|
* @throws \yii\base\InvalidConfigException |
297
|
|
|
*/ |
298
|
|
|
public function rawHttpAdd( |
299
|
|
|
string $id, |
300
|
|
|
array $payload, |
301
|
|
|
ConnectionInterface $connection = null, |
302
|
|
|
CacheInterface $cache = null |
303
|
|
|
): ResponseInterface { |
304
|
|
|
return $this->rawHttpAddRelay( |
305
|
|
|
$id, |
306
|
|
|
$payload, |
307
|
|
|
$connection, |
308
|
|
|
$cache |
309
|
|
|
)(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
/******************************************* |
314
|
|
|
* REMOVE |
315
|
|
|
*******************************************/ |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param ObjectBuilderInterface $builder |
319
|
|
|
* @param ConnectionInterface|string|null $connection |
320
|
|
|
* @param CacheInterface|string|null $cache |
321
|
|
|
* @param TransformerCollectionInterface|array|null $transformer |
322
|
|
|
* @param null $source |
323
|
|
|
* @return mixed |
324
|
|
|
* @throws \yii\base\InvalidConfigException |
325
|
|
|
*/ |
326
|
|
|
public function remove( |
327
|
|
|
ObjectBuilderInterface $builder, |
328
|
|
|
ConnectionInterface $connection = null, |
329
|
|
|
CacheInterface $cache = null, |
330
|
|
|
TransformerCollectionInterface $transformer = null, |
331
|
|
|
$source = null |
332
|
|
|
) { |
333
|
|
|
return $this->rawRemove( |
334
|
|
|
$builder->getId(), |
335
|
|
|
$builder->getPayload(), |
336
|
|
|
$connection, |
337
|
|
|
$cache, |
338
|
|
|
$transformer, |
339
|
|
|
$source |
340
|
|
|
); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param string $id |
345
|
|
|
* @param array $payload |
346
|
|
|
* @param ConnectionInterface|string|null $connection |
347
|
|
|
* @param CacheInterface|string|null $cache |
348
|
|
|
* @param TransformerCollectionInterface|array|null $transformer |
349
|
|
|
* @param null $source |
350
|
|
|
* @return mixed |
351
|
|
|
* @throws \yii\base\InvalidConfigException |
352
|
|
|
*/ |
353
|
|
|
public function rawRemove( |
354
|
|
|
string $id, |
355
|
|
|
array $payload, |
356
|
|
|
ConnectionInterface $connection = null, |
357
|
|
|
CacheInterface $cache = null, |
358
|
|
|
TransformerCollectionInterface $transformer = null, |
359
|
|
|
$source = null |
360
|
|
|
) { |
361
|
|
|
return $this->rawRemovePipeline( |
362
|
|
|
$id, |
363
|
|
|
$payload, |
364
|
|
|
$connection, |
365
|
|
|
$cache, |
366
|
|
|
$transformer |
367
|
|
|
)($source); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @param ObjectBuilderInterface $builder |
372
|
|
|
* @param ConnectionInterface|string|null $connection |
373
|
|
|
* @param CacheInterface|string|null $cache |
374
|
|
|
* @param TransformerCollectionInterface|array|null $transformer |
375
|
|
|
* @return PipelineBuilderInterface |
376
|
|
|
* @throws \yii\base\InvalidConfigException |
377
|
|
|
*/ |
378
|
|
|
public function removePipeline( |
379
|
|
|
ObjectBuilderInterface $builder, |
380
|
|
|
ConnectionInterface $connection = null, |
381
|
|
|
CacheInterface $cache = null, |
382
|
|
|
TransformerCollectionInterface $transformer = null |
383
|
|
|
): PipelineBuilderInterface { |
384
|
|
|
return $this->rawRemovePipeline( |
385
|
|
|
$builder->getId(), |
386
|
|
|
$builder->getPayload(), |
387
|
|
|
$connection, |
388
|
|
|
$cache, |
389
|
|
|
$transformer |
390
|
|
|
); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @param string $id |
395
|
|
|
* @param array $payload |
396
|
|
|
* @param ConnectionInterface|string|null $connection |
397
|
|
|
* @param CacheInterface|string|null $cache |
398
|
|
|
* @param TransformerCollectionInterface|array|null $transformer |
399
|
|
|
* @return PipelineBuilderInterface |
400
|
|
|
* @throws \yii\base\InvalidConfigException |
401
|
|
|
*/ |
402
|
|
|
public function rawRemovePipeline( |
403
|
|
|
string $id, |
404
|
|
|
array $payload, |
405
|
|
|
ConnectionInterface $connection = null, |
406
|
|
|
CacheInterface $cache = null, |
407
|
|
|
TransformerCollectionInterface $transformer = null |
408
|
|
|
): PipelineBuilderInterface { |
409
|
|
|
$transformer = TransformerHelper::populateTransformerCollection( |
410
|
|
|
TransformerHelper::resolveCollection($transformer), |
411
|
|
|
[ |
412
|
|
|
'resource' => [static::removeRelayBuilderClass()] |
413
|
|
|
] |
414
|
|
|
); |
415
|
|
|
|
416
|
|
|
return (new Resource( |
417
|
|
|
$this->rawHttpRemoveRelay( |
418
|
|
|
$id, |
419
|
|
|
$payload, |
420
|
|
|
ConnectionHelper::resolveConnection($connection), |
421
|
|
|
CacheHelper::$cache($connection) |
422
|
|
|
), |
423
|
|
|
$transformer, |
424
|
|
|
HubSpot::getInstance()->getPsrLogger() |
425
|
|
|
)); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* @param ObjectBuilderInterface $builder |
430
|
|
|
* @param ConnectionInterface|string|null $connection |
431
|
|
|
* @param CacheInterface|string|null $cache |
432
|
|
|
* @return callable |
433
|
|
|
* @throws \yii\base\InvalidConfigException |
434
|
|
|
*/ |
435
|
|
|
public function httpRemoveRelay( |
436
|
|
|
ObjectBuilderInterface $builder, |
437
|
|
|
ConnectionInterface $connection = null, |
438
|
|
|
CacheInterface $cache = null |
439
|
|
|
): callable { |
440
|
|
|
return $this->rawHttpRemoveRelay( |
441
|
|
|
$builder->getId(), |
442
|
|
|
$builder->getPayload(), |
443
|
|
|
$connection, |
444
|
|
|
$cache |
445
|
|
|
); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* @param string $id |
450
|
|
|
* @param array $payload |
451
|
|
|
* @param ConnectionInterface|string|null $connection |
452
|
|
|
* @param CacheInterface|string|null $cache |
453
|
|
|
* @return callable |
454
|
|
|
* @throws \yii\base\InvalidConfigException |
455
|
|
|
*/ |
456
|
|
|
public function rawHttpRemoveRelay( |
457
|
|
|
string $id, |
458
|
|
|
array $payload, |
459
|
|
|
ConnectionInterface $connection = null, |
460
|
|
|
CacheInterface $cache = null |
461
|
|
|
): callable { |
462
|
|
|
$class = static::removeRelayBuilderClass(); |
463
|
|
|
|
464
|
|
|
/** @var RelayBuilderInterface $builder */ |
465
|
|
|
$builder = new $class( |
466
|
|
|
$id, |
467
|
|
|
$payload, |
468
|
|
|
ConnectionHelper::resolveConnection($connection), |
469
|
|
|
CacheHelper::resolveCache($cache), |
470
|
|
|
HubSpot::getInstance()->getPsrLogger() |
471
|
|
|
); |
472
|
|
|
|
473
|
|
|
return $builder->build(); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* @param ObjectBuilderInterface $builder |
478
|
|
|
* @param ConnectionInterface|string|null $connection |
479
|
|
|
* @param CacheInterface|string|null $cache |
480
|
|
|
* @return ResponseInterface |
481
|
|
|
* @throws \yii\base\InvalidConfigException |
482
|
|
|
*/ |
483
|
|
|
public function httpRemove( |
484
|
|
|
ObjectBuilderInterface $builder, |
485
|
|
|
ConnectionInterface $connection = null, |
486
|
|
|
CacheInterface $cache = null |
487
|
|
|
): ResponseInterface { |
488
|
|
|
return $this->rawHttpRemove( |
489
|
|
|
$builder->getId(), |
490
|
|
|
$builder->getPayload(), |
491
|
|
|
$connection, |
492
|
|
|
$cache |
493
|
|
|
); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* @param string $id |
498
|
|
|
* @param array $payload |
499
|
|
|
* @param ConnectionInterface|string|null $connection |
500
|
|
|
* @param CacheInterface|string|null $cache |
501
|
|
|
* @return ResponseInterface |
502
|
|
|
* @throws \yii\base\InvalidConfigException |
503
|
|
|
*/ |
504
|
|
|
public function rawHttpRemove( |
505
|
|
|
string $id, |
506
|
|
|
array $payload, |
507
|
|
|
ConnectionInterface $connection = null, |
508
|
|
|
CacheInterface $cache = null |
509
|
|
|
): ResponseInterface { |
510
|
|
|
return $this->rawHttpRemoveRelay( |
511
|
|
|
$id, |
512
|
|
|
$payload, |
513
|
|
|
$connection, |
514
|
|
|
$cache |
515
|
|
|
)(); |
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
|