Completed
Push — develop ( a9d8b7...303fd0 )
by Nate
20:48
created

ContactLists::updatePipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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