Completed
Push — develop ( 5ca96d...d54e0b )
by Nate
07:04
created

TimelineEvents::rawBatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.8666
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 flipbox\hubspot\connections\ConnectionInterface;
13
use flipbox\hubspot\connections\IntegrationConnectionInterface;
14
use flipbox\hubspot\criteria\TimelineEventAccessorInterface;
15
use flipbox\hubspot\criteria\TimelineEventBatchMutatorInterface;
16
use flipbox\hubspot\criteria\TimelineEventMutatorInterface;
17
use flipbox\hubspot\helpers\CacheHelper;
18
use flipbox\hubspot\helpers\ConnectionHelper;
19
use flipbox\hubspot\helpers\TransformerHelper;
20
use flipbox\hubspot\HubSpot;
21
use flipbox\hubspot\pipeline\Resource;
22
use flipbox\hubspot\queue\jobs\UpsertTimelineEvent;
23
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
24
use Flipbox\Relay\HubSpot\Builder\Resources\Timeline\Event\Batch;
25
use Flipbox\Relay\HubSpot\Builder\Resources\Timeline\Event\Read;
26
use Flipbox\Relay\HubSpot\Builder\Resources\Timeline\Event\Upsert;
27
use League\Pipeline\PipelineBuilderInterface;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\SimpleCache\CacheInterface;
30
use yii\base\Component;
31
32
/**
33
 * @author Flipbox Factory <[email protected]>
34
 * @since 1.0.0
35
 */
36
class TimelineEvents extends Component
37
{
38
    /**
39
     * The HubSpot Resource name
40
     */
41
    const HUBSPOT_RESOURCE = 'timelineEvents';
42
43
    /*******************************************
44
     * ELEMENT SYNC JOBS
45
     *******************************************/
46
47
    /**
48
     * @param TimelineEventMutatorInterface $criteria
49
     * @return null|string
50
     * @throws \yii\base\InvalidConfigException
51
     */
52
    public function upsertJob(
53
        TimelineEventMutatorInterface $criteria
54
    ) {
55
        return $this->rawUpsertJob(
56
            $criteria->getId(),
57
            $criteria->getTypeId(),
58
            $criteria->getPayload(),
59
            $criteria->getConnection()
60
        );
61
    }
62
63
    /**
64
     * @param string $id
65
     * @param string $typeId
66
     * @param array $payload
67
     * @param IntegrationConnectionInterface|null $connection
68
     * @return null|string
69
     * @throws \yii\base\InvalidConfigException
70
     */
71
    public function rawUpsertJob(
72
        string $id,
73
        string $typeId,
74
        array $payload,
75
        IntegrationConnectionInterface $connection = null
76
    ) {
77
        return Craft::$app->getQueue()->push(new UpsertTimelineEvent([
78
            'id' => $id,
79
            'typeId' => $typeId,
80
            'payload' => $payload,
81
            'connection' => ConnectionHelper::resolveIntegrationConnection($connection)
82
        ]));
83
    }
84
85
    /*******************************************
86
     * GET / READ
87
     *******************************************/
88
89
    /**
90
     * @param TimelineEventAccessorInterface $criteria
91
     * @param array $extra
92
     * @return mixed
93
     * @throws \yii\base\InvalidConfigException
94
     */
95
    public function read(
96
        TimelineEventAccessorInterface $criteria,
97
        array $extra = []
98
    ) {
99
        return $this->rawRead(
100
            $criteria->getId(),
101
            $criteria->getTypeId(),
102
            $criteria->getConnection(),
103
            $criteria->getCache(),
104
            $criteria->getTransformer(),
105
            $extra
106
        );
107
    }
108
109
    /**
110
     * @param string $id
111
     * @param string $typeId
112
     * @param IntegrationConnectionInterface|null $connection
113
     * @param CacheInterface|string|null $cache
114
     * @param TransformerCollectionInterface|array|null $transformer
115
     * @param array $extra
116
     * @return mixed
117
     * @throws \yii\base\InvalidConfigException
118
     */
119
    public function rawRead(
120
        string $id,
121
        string $typeId,
122
        IntegrationConnectionInterface $connection = null,
123
        CacheInterface $cache = null,
124
        TransformerCollectionInterface $transformer = null,
125
        array $extra = []
126
    ) {
127
        return $this->rawReadPipeline(
128
            $id,
129
            $typeId,
130
            $connection,
131
            $cache,
132
            $transformer
133
        )($extra);
134
    }
135
136
    /**
137
     * @param TimelineEventAccessorInterface $criteria
138
     * @return PipelineBuilderInterface
139
     * @throws \yii\base\InvalidConfigException
140
     */
141
    public function readPipeline(
142
        TimelineEventAccessorInterface $criteria
143
    ): PipelineBuilderInterface {
144
        return $this->rawReadPipeline(
145
            $criteria->getId(),
146
            $criteria->getTypeId(),
147
            $criteria->getConnection(),
148
            $criteria->getCache(),
149
            $criteria->getTransformer()
150
        );
151
    }
152
153
    /**
154
     * @param string $id
155
     * @param string $typeId
156
     * @param IntegrationConnectionInterface|null $connection
157
     * @param CacheInterface|string|null $cache
158
     * @param TransformerCollectionInterface|array|null $transformer
159
     * @return PipelineBuilderInterface
160
     * @throws \yii\base\InvalidConfigException
161
     */
162
    public function rawReadPipeline(
163
        string $id,
164
        string $typeId,
165
        IntegrationConnectionInterface $connection = null,
166
        CacheInterface $cache = null,
167
        TransformerCollectionInterface $transformer = null
168
    ): PipelineBuilderInterface {
169
        $transformer = TransformerHelper::populateTransformerCollection(
170
            TransformerHelper::resolveCollection($transformer),
171
            [
172
                'resource' => [Read::class]
173
            ]
174
        );
175
176
        return (new Resource(
177
            $this->rawHttpReadRelay(
178
                $id,
179
                $typeId,
180
                $connection,
181
                $cache
182
            ),
183
            $transformer,
184
            HubSpot::getInstance()->getPsrLogger()
185
        ));
186
    }
187
188
    /**
189
     * @param TimelineEventAccessorInterface $criteria
190
     * @return ResponseInterface
191
     * @throws \yii\base\InvalidConfigException
192
     */
193
    public function httpRead(
194
        TimelineEventAccessorInterface $criteria
195
    ): ResponseInterface {
196
        return $this->rawHttpRead(
197
            $criteria->getId(),
198
            $criteria->getTypeId(),
199
            $criteria->getConnection(),
200
            $criteria->getCache()
201
        );
202
    }
203
204
    /**
205
     * @param string $id
206
     * @param string $typeId
207
     * @param IntegrationConnectionInterface|null $connection
208
     * @param CacheInterface|string|null $cache
209
     * @return ResponseInterface
210
     * @throws \yii\base\InvalidConfigException
211
     */
212
    public function rawHttpRead(
213
        string $id,
214
        string $typeId,
215
        IntegrationConnectionInterface $connection = null,
216
        CacheInterface $cache = null
217
    ): ResponseInterface {
218
        return $this->rawHttpReadRelay(
219
            $id,
220
            $typeId,
221
            $connection,
222
            $cache
223
        )();
224
    }
225
226
    /**
227
     * @param TimelineEventAccessorInterface $criteria
228
     * @return callable
229
     * @throws \yii\base\InvalidConfigException
230
     */
231
    public function httpReadRelay(
232
        TimelineEventAccessorInterface $criteria
233
    ): callable {
234
        return $this->rawHttpReadRelay(
235
            $criteria->getId(),
236
            $criteria->getTypeId(),
237
            $criteria->getConnection(),
238
            $criteria->getCache()
239
        );
240
    }
241
242
    /**
243
     * @param string $id
244
     * @param string $typeId
245
     * @param IntegrationConnectionInterface|null $connection
246
     * @param CacheInterface|string|null $cache
247
     * @return callable
248
     * @throws \yii\base\InvalidConfigException
249
     */
250
    public function rawHttpReadRelay(
251
        string $id,
252
        string $typeId,
253
        IntegrationConnectionInterface $connection = null,
254
        CacheInterface $cache = null
255
    ): callable {
256
        $connection = ConnectionHelper::resolveIntegrationConnection($connection);
257
258
        return (new Read(
259
            $connection->getAppId(),
260
            $id,
261
            $typeId,
262
            $connection,
263
            CacheHelper::resolveCache($cache),
264
            HubSpot::getInstance()->getPsrLogger()
265
        ))->build();
266
    }
267
268
269
    /*******************************************
270
     * UPSERT
271
     *******************************************/
272
273
    /**
274
     * @param TimelineEventMutatorInterface $criteria
275
     * @param array $extra
276
     * @return mixed
277
     * @throws \yii\base\InvalidConfigException
278
     */
279
    public function upsert(
280
        TimelineEventMutatorInterface $criteria,
281
        array $extra = []
282
    ) {
283
        return $this->rawUpsert(
284
            $criteria->getTypeId(),
285
            $criteria->getId(),
286
            $criteria->getPayload(),
287
            $criteria->getConnection(),
288
            $criteria->getCache(),
289
            $criteria->getTransformer(),
290
            $extra
291
        );
292
    }
293
294
    /**
295
     * @param string $typeId
296
     * @param string $id
297
     * @param array $payload
298
     * @param IntegrationConnectionInterface|null $connection
299
     * @param CacheInterface|string|null $cache
300
     * @param TransformerCollectionInterface|array|null $transformer
301
     * @param array $extra
302
     * @return mixed
303
     * @throws \yii\base\InvalidConfigException
304
     */
305
    public function rawUpsert(
306
        string $typeId,
307
        string $id,
308
        array $payload,
309
        IntegrationConnectionInterface $connection = null,
310
        CacheInterface $cache = null,
311
        TransformerCollectionInterface $transformer = null,
312
        array $extra = []
313
    ) {
314
        return $this->rawUpsertPipeline(
315
            $typeId,
316
            $id,
317
            $payload,
318
            $connection,
319
            $cache,
320
            $transformer
321
        )($extra);
322
    }
323
324
    /**
325
     * @param TimelineEventMutatorInterface $criteria
326
     * @return PipelineBuilderInterface
327
     * @throws \yii\base\InvalidConfigException
328
     */
329
    public function upsertPipeline(
330
        TimelineEventMutatorInterface $criteria
331
    ): PipelineBuilderInterface {
332
        return $this->rawUpsertPipeline(
333
            $criteria->getTypeId(),
334
            $criteria->getId(),
335
            $criteria->getPayload(),
336
            $criteria->getConnection(),
337
            $criteria->getCache(),
338
            $criteria->getTransformer()
339
        );
340
    }
341
342
    /**
343
     * @param string $typeId
344
     * @param string $id
345
     * @param array $payload
346
     * @param IntegrationConnectionInterface|null $connection
347
     * @param CacheInterface|string|null $cache
348
     * @param TransformerCollectionInterface|array|null $transformer
349
     * @return PipelineBuilderInterface
350
     * @throws \yii\base\InvalidConfigException
351
     */
352
    public function rawUpsertPipeline(
353
        string $typeId,
354
        string $id,
355
        array $payload,
356
        IntegrationConnectionInterface $connection = null,
357
        CacheInterface $cache = null,
358
        TransformerCollectionInterface $transformer = null
359
    ): PipelineBuilderInterface {
360
        $transformer = TransformerHelper::populateTransformerCollection(
361
            TransformerHelper::resolveCollection($transformer),
362
            [
363
                'resource' => [Upsert::class]
364
            ]
365
        );
366
367
        return (new Resource(
368
            $this->rawHttpUpsertRelay(
369
                $typeId,
370
                $id,
371
                $payload,
372
                ConnectionHelper::resolveIntegrationConnection($connection),
373
                $cache
374
            ),
375
            $transformer,
376
            HubSpot::getInstance()->getPsrLogger()
377
        ));
378
    }
379
380
    /**
381
     * @param TimelineEventMutatorInterface $criteria
382
     * @return ResponseInterface
383
     * @throws \yii\base\InvalidConfigException
384
     */
385
    public function httpUpsert(
386
        TimelineEventMutatorInterface $criteria
387
    ): ResponseInterface {
388
        return $this->rawHttpUpsert(
389
            $criteria->getTypeId(),
390
            $criteria->getId(),
391
            $criteria->getPayload(),
392
            $criteria->getConnection(),
393
            $criteria->getCache()
394
        );
395
    }
396
397
    /**
398
     * @param string $typeId
399
     * @param string $id
400
     * @param array $payload
401
     * @param IntegrationConnectionInterface|null $connection
402
     * @param CacheInterface|string|null $cache
403
     * @return ResponseInterface
404
     * @throws \yii\base\InvalidConfigException
405
     */
406
    public function rawHttpUpsert(
407
        string $typeId,
408
        string $id,
409
        array $payload,
410
        IntegrationConnectionInterface $connection = null,
411
        CacheInterface $cache = null
412
    ): ResponseInterface {
413
        return $this->rawHttpUpsertRelay(
414
            $typeId,
415
            $id,
416
            $payload,
417
            $connection,
418
            $cache
419
        )();
420
    }
421
422
    /**
423
     * @param TimelineEventMutatorInterface $criteria
424
     * @return callable
425
     * @throws \yii\base\InvalidConfigException
426
     */
427
    public function httpUpsertRelay(
428
        TimelineEventMutatorInterface $criteria
429
    ): callable {
430
        return $this->rawHttpUpsertRelay(
431
            $criteria->getTypeId(),
432
            $criteria->getId(),
433
            $criteria->getPayload(),
434
            $criteria->getConnection(),
435
            $criteria->getCache()
436
        );
437
    }
438
439
    /**
440
     * @param string $typeId
441
     * @param string $id
442
     * @param array $payload
443
     * @param IntegrationConnectionInterface|null $connection
444
     * @param CacheInterface|string|null $cache
445
     * @return callable
446
     * @throws \yii\base\InvalidConfigException
447
     */
448
    public function rawHttpUpsertRelay(
449
        string $typeId,
450
        string $id,
451
        array $payload,
452
        IntegrationConnectionInterface $connection = null,
453
        CacheInterface $cache = null
454
    ): callable {
455
        $connection = ConnectionHelper::resolveIntegrationConnection($connection);
456
457
        $payload['id'] = $id;
458
        $payload['eventTypeId'] = $typeId;
459
460
        return (new Upsert(
461
            $connection->getAppId(),
462
            $typeId,
463
            $id,
464
            $payload,
465
            $connection,
466
            CacheHelper::resolveCache($cache),
467
            HubSpot::getInstance()->getPsrLogger()
468
        ))->build();
469
    }
470
471
472
    /*******************************************
473
     * BATCH
474
     *******************************************/
475
476
    /**
477
     * @param TimelineEventBatchMutatorInterface $criteria
478
     * @param array $extra
479
     * @return mixed
480
     * @throws \yii\base\InvalidConfigException
481
     */
482
    public function batch(
483
        TimelineEventBatchMutatorInterface $criteria,
484
        array $extra = []
485
    ) {
486
        return $this->rawBatch(
487
            $criteria->getPayload(),
488
            $criteria->getConnection(),
489
            $criteria->getTransformer(),
490
            $extra
491
        );
492
    }
493
494
    /**
495
     * @param array $payload
496
     * @param IntegrationConnectionInterface|null $connection
497
     * @param TransformerCollectionInterface|array|null $transformer
498
     * @param array $extra
499
     * @return mixed
500
     * @throws \yii\base\InvalidConfigException
501
     */
502
    public function rawBatch(
503
        array $payload,
504
        IntegrationConnectionInterface $connection = null,
505
        TransformerCollectionInterface $transformer = null,
506
        array $extra = []
507
    ) {
508
        return $this->rawBatchPipeline(
509
            $payload,
510
            $connection,
511
            $transformer
512
        )($extra);
513
    }
514
515
    /**
516
     * @param TimelineEventBatchMutatorInterface $criteria
517
     * @return mixed
518
     * @throws \yii\base\InvalidConfigException
519
     */
520
    public function batchPipeline(
521
        TimelineEventBatchMutatorInterface $criteria
522
    ): PipelineBuilderInterface {
523
        return $this->rawBatchPipeline(
524
            $criteria->getPayload(),
525
            $criteria->getConnection(),
526
            $criteria->getTransformer()
527
        );
528
    }
529
530
    /**
531
     * @param array $payload
532
     * @param IntegrationConnectionInterface|null $connection
533
     * @param TransformerCollectionInterface|array|null $transformer
534
     * @return PipelineBuilderInterface
535
     * @throws \yii\base\InvalidConfigException
536
     */
537
    public function rawBatchPipeline(
538
        array $payload,
539
        IntegrationConnectionInterface $connection = null,
540
        TransformerCollectionInterface $transformer = null
541
    ): PipelineBuilderInterface {
542
        $transformer = TransformerHelper::populateTransformerCollection(
543
            TransformerHelper::resolveCollection($transformer),
544
            [
545
                'resource' => [Batch::class]
546
            ]
547
        );
548
549
        return (new Resource(
550
            $this->rawHttpBatchRelay(
551
                $payload,
552
                ConnectionHelper::resolveIntegrationConnection($connection)
553
            ),
554
            $transformer,
555
            HubSpot::getInstance()->getPsrLogger()
556
        ));
557
    }
558
559
    /**
560
     * @param TimelineEventBatchMutatorInterface $criteria
561
     * @return ResponseInterface
562
     * @throws \yii\base\InvalidConfigException
563
     */
564
    public function httpBatch(
565
        TimelineEventBatchMutatorInterface $criteria
566
    ): ResponseInterface {
567
        return $this->rawHttpBatch(
568
            $criteria->getPayload(),
569
            $criteria->getConnection()
570
        )();
571
    }
572
573
    /**
574
     * @param array $payload
575
     * @param IntegrationConnectionInterface|string|null $connection
576
     * @return ResponseInterface
577
     * @throws \yii\base\InvalidConfigException
578
     */
579
    public function rawHttpBatch(
580
        array $payload,
581
        IntegrationConnectionInterface $connection = null
582
    ): ResponseInterface {
583
        return $this->rawHttpBatchRelay(
584
            $payload,
585
            $connection
586
        )();
587
    }
588
589
    /**
590
     * @param TimelineEventBatchMutatorInterface $criteria
591
     * @return callable
592
     * @throws \yii\base\InvalidConfigException
593
     */
594
    public function httpBatchRelay(
595
        TimelineEventBatchMutatorInterface $criteria
596
    ): callable {
597
        return $this->rawHttpBatchRelay(
598
            $criteria->getPayload(),
599
            $criteria->getConnection()
600
        );
601
    }
602
603
    /**
604
     * @param array $payload
605
     * @param IntegrationConnectionInterface|null $connection
606
     * @return callable
607
     * @throws \yii\base\InvalidConfigException
608
     */
609
    public function rawHttpBatchRelay(
610
        array $payload,
611
        IntegrationConnectionInterface $connection = null
612
    ): callable {
613
        $connection = ConnectionHelper::resolveIntegrationConnection($connection);
614
615
        return (new Batch(
616
            $connection->getAppId(),
617
            $payload,
618
            $connection,
619
            HubSpot::getInstance()->getPsrLogger()
620
        ))->build();
621
    }
622
}
623