Completed
Push — develop ( 3644a8...72e19e )
by Nate
06:59
created

Contacts::readPipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 13
cp 0
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
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 Craft;
12
use craft\base\ElementInterface;
13
use flipbox\hubspot\fields\Resources;
14
use flipbox\hubspot\queue\jobs\SyncElementFrom;
15
use flipbox\hubspot\queue\jobs\SyncElementTo;
16
use flipbox\hubspot\connections\ConnectionInterface;
17
use flipbox\hubspot\criteria\ContactCriteria;
18
use flipbox\hubspot\criteria\ObjectCriteriaInterface;
19
use flipbox\hubspot\helpers\TransformerHelper;
20
use flipbox\hubspot\HubSpot;
21
use flipbox\hubspot\pipeline\Resource;
22
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
23
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Create;
24
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Delete;
25
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\ReadById;
26
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Update;
27
use League\Pipeline\PipelineBuilderInterface;
28
use Psr\SimpleCache\CacheInterface;
29
use yii\base\Component;
30
31
/**
32
 * @author Flipbox Factory <[email protected]>
33
 * @since 1.0.0
34
 */
35
class Contacts extends Component
36
{
37
    use traits\ElementCriteriaTrait,
38
        traits\TransformElementIdTrait,
39
        traits\SyncByElementTrait,
40
        traits\DeleteByElementTrait;
41
42
    /**
43
     * The HubSpot Resource name
44
     */
45
    const HUBSPOT_RESOURCE = 'contacts';
46
47
    /**
48
     * @inheritdoc
49
     * @return ContactCriteria
50
     */
51
    public function getCriteria(array $criteria = []): ObjectCriteriaInterface
52
    {
53
        return new ContactCriteria($criteria);
54
    }
55
56
    /*******************************************
57
     * ELEMENT SYNC JOBS
58
     *******************************************/
59
60
    /**
61
     * @param ElementInterface $element
62
     * @param Resources $field
63
     * @return null|string
64
     */
65
    public function createElementSyncToJob(
66
        ElementInterface $element,
67
        Resources $field
68
    ) {
69
        return Craft::$app->getQueue()->push(new SyncElementTo([
70
            'element' => $element,
71
            'field' => $field,
72
            'resource' => self::HUBSPOT_RESOURCE
73
        ]));
74
    }
75
76
    /**
77
     * @param ElementInterface $element
78
     * @param Resources $field
79
     * @return null|string
80
     */
81
    public function createElementSyncFromJob(
82
        ElementInterface $element,
83
        Resources $field
84
    ) {
85
        return Craft::$app->getQueue()->push(new SyncElementFrom([
86
            'element' => $element,
87
            'field' => $field,
88
            'resource' => self::HUBSPOT_RESOURCE
89
        ]));
90
    }
91
92
    /*******************************************
93
     * READ / GET
94
     *******************************************/
95
96
    /**
97
     * @param string $id
98
     * @param ConnectionInterface $connection
99
     * @param CacheInterface $cache
100
     * @param TransformerCollectionInterface|null $transformer
101
     * @param mixed|null $source
102
     * @return mixed
103
     */
104
    public function read(
105
        $id,
106
        ConnectionInterface $connection,
107
        CacheInterface $cache,
108
        TransformerCollectionInterface $transformer = null,
109
        $source = null
110
    ) {
111
        return $this->readPipeline(
112
            $id,
113
            $connection,
114
            $cache,
115
            $transformer
116
        )($source);
117
    }
118
119
    /**
120
     * @param string $id
121
     * @param ConnectionInterface $connection
122
     * @param CacheInterface $cache
123
     * @param TransformerCollectionInterface|null $transformer
124
     * @return Resource
125
     */
126
    public function readPipeline(
127
        $id,
128
        ConnectionInterface $connection,
129
        CacheInterface $cache,
130
        TransformerCollectionInterface $transformer = null
131
    ): PipelineBuilderInterface {
132
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
133
            'resource' => [ReadById::class]
134
        ]);
135
136
        $logger = HubSpot::getInstance()->getPsrLogger();
137
138
        return (new Resource(
139
            (new ReadById(
140
                $id,
141
                $connection,
142
                $cache,
143
                $logger
144
            ))->build(),
145
            $transformer,
146
            $logger
147
        ));
148
    }
149
150
    /**
151
     * @inheritdoc
152
     * @return mixed
153
     */
154
    public function readByCriteria(
155
        ObjectCriteriaInterface $criteria,
156
        $source = null
157
    ) {
158
        return $this->readPipelineByCriteria(
159
            $criteria
160
        )($source);
161
    }
162
163
    /**
164
     * @param ObjectCriteriaInterface $criteria
165
     * @return PipelineBuilderInterface
166
     */
167
    public function readPipelineByCriteria(
168
        ObjectCriteriaInterface $criteria
169
    ): PipelineBuilderInterface {
170
        return $this->readPipeline(
171
            $criteria->getId(),
172
            $criteria->getConnection(),
173
            $criteria->getCache(),
174
            $criteria->getTransformer()
175
        );
176
    }
177
178
    /*******************************************
179
     * CREATE
180
     *******************************************/
181
182
    /**
183
     * @param array $payload
184
     * @param ConnectionInterface $connection
185
     * @param TransformerCollectionInterface|null $transformer
186
     * @param null $source
187
     * @return mixed
188
     */
189
    public function create(
190
        array $payload,
191
        ConnectionInterface $connection,
192
        TransformerCollectionInterface $transformer = null,
193
        $source = null
194
    ) {
195
        return $this->createPipeline(
196
            $payload,
197
            $connection,
198
            $transformer
199
        )($source);
200
    }
201
202
    /**
203
     * @param array $payload
204
     * @param ConnectionInterface $connection
205
     * @param TransformerCollectionInterface|null $transformer
206
     * @return Resource
207
     */
208
    public function createPipeline(
209
        array $payload,
210
        ConnectionInterface $connection,
211
        TransformerCollectionInterface $transformer = null
212
    ): PipelineBuilderInterface {
213
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
214
            'resource' => [Create::class]
215
        ]);
216
217
        $logger = HubSpot::getInstance()->getPsrLogger();
218
219
        return (new Resource(
220
            (new Create(
221
                $payload,
222
                $connection,
223
                $logger
224
            ))->build(),
225
            $transformer,
226
            $logger
227
        ));
228
    }
229
230
    /**
231
     * @param ObjectCriteriaInterface $criteria
232
     * @param mixed $source
233
     * @return mixed
234
     */
235
    public function createByCriteria(
236
        ObjectCriteriaInterface $criteria,
237
        $source = null
238
    ) {
239
        return $this->createPipelineByCriteria(
240
            $criteria
241
        )($source);
242
    }
243
244
    /**
245
     * @param ObjectCriteriaInterface $criteria
246
     * @return Resource
247
     */
248
    public function createPipelineByCriteria(
249
        ObjectCriteriaInterface $criteria
250
    ): PipelineBuilderInterface {
251
        return $this->createPipeline(
252
            $criteria->getPayload(),
253
            $criteria->getConnection(),
254
            $criteria->getTransformer()
255
        );
256
    }
257
258
    /*******************************************
259
     * UPDATE
260
     *******************************************/
261
262
    /**
263
     * @param string $id
264
     * @param array $payload
265
     * @param ConnectionInterface $connection
266
     * @param CacheInterface $cache
267
     * @param TransformerCollectionInterface|null $transformer
268
     * @param null $source
269
     * @return mixed
270
     */
271
    public function update(
272
        string $id,
273
        array $payload,
274
        ConnectionInterface $connection,
275
        CacheInterface $cache,
276
        TransformerCollectionInterface $transformer = null,
277
        $source = null
278
    ) {
279
        return $this->updatePipeline(
280
            $id,
281
            $payload,
282
            $connection,
283
            $cache,
284
            $transformer
285
        )($source);
286
    }
287
288
    /**
289
     * @param string $id
290
     * @param array $payload
291
     * @param ConnectionInterface $connection
292
     * @param CacheInterface $cache
293
     * @param TransformerCollectionInterface|null $transformer
294
     * @return Resource
295
     */
296
    public function updatePipeline(
297
        string $id,
298
        array $payload,
299
        ConnectionInterface $connection,
300
        CacheInterface $cache,
301
        TransformerCollectionInterface $transformer = null
302
    ): PipelineBuilderInterface {
303
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
304
            'resource' => [Update::class]
305
        ]);
306
307
        $logger = HubSpot::getInstance()->getPsrLogger();
308
309
        return (new Resource(
310
            (new Update(
311
                $id,
312
                $payload,
313
                $connection,
314
                $cache,
315
                $logger
316
            ))->build(),
317
            $transformer,
318
            $logger
319
        ));
320
    }
321
322
    /**
323
     * @param ObjectCriteriaInterface $criteria
324
     * @param null $source
325
     * @return mixed
326
     */
327
    public function updateByCriteria(
328
        ObjectCriteriaInterface $criteria,
329
        $source = null
330
    ) {
331
        return $this->updatePipelineByCriteria(
332
            $criteria
333
        )($source);
334
    }
335
336
    /**
337
     * @param ObjectCriteriaInterface $criteria
338
     * @return Resource
339
     */
340
    public function updatePipelineByCriteria(
341
        ObjectCriteriaInterface $criteria
342
    ): PipelineBuilderInterface {
343
        return $this->updatePipeline(
344
            $criteria->getId(),
345
            $criteria->getPayload(),
346
            $criteria->getConnection(),
347
            $criteria->getCache(),
348
            $criteria->getTransformer()
349
        );
350
    }
351
352
    /*******************************************
353
     * UPSERT
354
     *******************************************/
355
356
    /**
357
     * @param array $payload
358
     * @param ConnectionInterface $connection
359
     * @param CacheInterface $cache
360
     * @param string|null $identifier
361
     * @param TransformerCollectionInterface|null $transformer
362
     * @param null $source
363
     * @return mixed
364
     */
365
    public function upsert(
366
        array $payload,
367
        ConnectionInterface $connection,
368
        CacheInterface $cache,
369
        string $identifier = null,
370
        TransformerCollectionInterface $transformer = null,
371
        $source = null
372
    ) {
373
        return $this->upsertPipeline(
374
            $payload,
375
            $connection,
376
            $cache,
377
            $identifier,
378
            $transformer
379
        )($source);
380
    }
381
382
    /**
383
     * @param array $payload
384
     * @param ConnectionInterface $connection
385
     * @param CacheInterface $cache
386
     * @param string|null $identifier
387
     * @param TransformerCollectionInterface|null $transformer
388
     * @return Resource
389
     */
390
    public function upsertPipeline(
391
        array $payload,
392
        ConnectionInterface $connection,
393
        CacheInterface $cache,
394
        string $identifier = null,
395
        TransformerCollectionInterface $transformer = null
396
    ): PipelineBuilderInterface {
397
        if (empty($identifier)) {
398
            return $this->createPipeline(
399
                $payload,
400
                $connection,
401
                $transformer
402
            );
403
        }
404
405
        return $this->updatePipeline(
406
            $identifier,
407
            $payload,
408
            $connection,
409
            $cache,
410
            $transformer
411
        );
412
    }
413
414
    /**
415
     * @inheritdoc
416
     * @return mixed
417
     */
418
    public function upsertByCriteria(
419
        ObjectCriteriaInterface $criteria,
420
        $source = null
421
    ) {
422
        return $this->upsertPipelineByCriteria(
423
            $criteria
424
        )($source);
425
    }
426
427
    /**
428
     * @inheritdoc
429
     * @return Resource
430
     */
431
    public function upsertPipelineByCriteria(
432
        ObjectCriteriaInterface $criteria
433
    ): PipelineBuilderInterface {
434
        return $this->upsertPipeline(
435
            $criteria->getPayload(),
436
            $criteria->getConnection(),
437
            $criteria->getCache(),
438
            $criteria->getId(),
439
            $criteria->getTransformer()
440
        );
441
    }
442
443
444
    /*******************************************
445
     * DELETE
446
     *******************************************/
447
448
    /**
449
     * @param string $id
450
     * @param ConnectionInterface $connection
451
     * @param CacheInterface $cache
452
     * @param TransformerCollectionInterface|null $transformer
453
     * @param null $source
454
     * @return mixed
455
     */
456
    public function delete(
457
        string $id,
458
        ConnectionInterface $connection,
459
        CacheInterface $cache,
460
        TransformerCollectionInterface $transformer = null,
461
        $source = null
462
    ): PipelineBuilderInterface {
463
        return $this->deletePipeline(
464
            $id,
465
            $connection,
466
            $cache,
467
            $transformer
468
        )($source);
469
    }
470
471
    /**
472
     * @param string $id
473
     * @param ConnectionInterface $connection
474
     * @param CacheInterface $cache
475
     * @param TransformerCollectionInterface|null $transformer
476
     * @return Resource
477
     */
478
    public function deletePipeline(
479
        string $id,
480
        ConnectionInterface $connection,
481
        CacheInterface $cache,
482
        TransformerCollectionInterface $transformer = null
483
    ): PipelineBuilderInterface {
484
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
485
            'resource' => [Delete::class]
486
        ]);
487
488
        $logger = HubSpot::getInstance()->getPsrLogger();
489
490
        return (new Resource(
491
            (new Delete(
492
                $id,
493
                $connection,
494
                $cache,
495
                $logger
496
            ))->build(),
497
            $transformer,
498
            $logger
499
        ));
500
    }
501
502
    /**
503
     * @param ObjectCriteriaInterface $criteria
504
     * @param null $source
505
     * @return mixed
506
     */
507
    public function deleteByCriteria(
508
        ObjectCriteriaInterface $criteria,
509
        $source = null
510
    ): PipelineBuilderInterface {
511
        return $this->deletePipelineByCriteria(
512
            $criteria
513
        )($source);
514
    }
515
516
    /**
517
     * @param ObjectCriteriaInterface $criteria
518
     * @return Resource
519
     */
520
    public function deletePipelineByCriteria(
521
        ObjectCriteriaInterface $criteria
522
    ): PipelineBuilderInterface {
523
        return $this->deletePipeline(
524
            $criteria->getId(),
525
            $criteria->getConnection(),
526
            $criteria->getCache(),
527
            $criteria->getTransformer()
528
        );
529
    }
530
}
531