Completed
Push — develop ( fee95e...245199 )
by Nate
10:11
created

ContactListContacts::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 8
cp 0
rs 9.7333
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\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\AddObjectTrait;
22
use flipbox\hubspot\services\resources\traits\ReadObjectTrait;
23
use flipbox\hubspot\services\resources\traits\RemoveObjectTrait;
24
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
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
        AddObjectTrait,
42
        RemoveObjectTrait;
43
44
    /**
45
     * The HubSpot Resource name
46
     */
47
    const HUBSPOT_RESOURCE = 'contactListContacts';
48
49
    /**
50
     * @param array $config
51
     * @return ObjectCriteriaInterface
52
     */
53
    public function getCriteria(array $config = []): ObjectCriteriaInterface
54
    {
55
        return new ContactListContactsCriteria($config);
56
    }
57
58
    /**
59
     * @param array $config
60
     * @return ObjectBuilderInterface
61
     */
62
    public function getBuilder(array $config = []): ObjectBuilderInterface
63
    {
64
        return new ContactListContactsBuilder($config);
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    protected static function readRelayBuilderClass(): string
71
    {
72
        return All::class;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    protected static function addRelayBuilderClass(): string
79
    {
80
        return Add::class;
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    protected static function removeRelayBuilderClass(): string
87
    {
88
        return Remove::class;
89
    }
90
91
92
    /*******************************************
93
     * ADD
94
     *******************************************/
95
96
    /**
97
     * @param ObjectBuilderInterface $builder
98
     * @param ConnectionInterface|string|null $connection
99
     * @param CacheInterface|string|null $cache
100
     * @param TransformerCollectionInterface|array|null $transformer
101
     * @param null $source
102
     * @return mixed
103
     * @throws \yii\base\InvalidConfigException
104
     */
105
    public function add(
106
        ObjectBuilderInterface $builder,
107
        ConnectionInterface $connection = null,
108
        CacheInterface $cache = null,
109
        TransformerCollectionInterface $transformer = null,
110
        $source = null
111
    ) {
112
        return $this->rawAdd(
113
            $builder->getId(),
114
            $builder->getPayload(),
115
            $connection,
116
            $cache,
117
            $transformer,
118
            $source
119
        );
120
    }
121
122
    /**
123
     * @param string $id
124
     * @param array $payload
125
     * @param ConnectionInterface|string|null $connection
126
     * @param CacheInterface|string|null $cache
127
     * @param TransformerCollectionInterface|array|null $transformer
128
     * @param null $source
129
     * @return mixed
130
     * @throws \yii\base\InvalidConfigException
131
     */
132
    public function rawAdd(
133
        string $id,
134
        array $payload,
135
        ConnectionInterface $connection = null,
136
        CacheInterface $cache = null,
137
        TransformerCollectionInterface $transformer = null,
138
        $source = null
139
    ) {
140
        return $this->rawAddPipeline(
141
            $id,
142
            $payload,
143
            $connection,
144
            $cache,
145
            $transformer
146
        )($source);
147
    }
148
149
    /**
150
     * @param ObjectBuilderInterface $builder
151
     * @param ConnectionInterface|string|null $connection
152
     * @param CacheInterface|string|null $cache
153
     * @param TransformerCollectionInterface|array|null $transformer
154
     * @return PipelineBuilderInterface
155
     * @throws \yii\base\InvalidConfigException
156
     */
157
    public function addPipeline(
158
        ObjectBuilderInterface $builder,
159
        ConnectionInterface $connection = null,
160
        CacheInterface $cache = null,
161
        TransformerCollectionInterface $transformer = null
162
    ): PipelineBuilderInterface {
163
        return $this->rawAddPipeline(
164
            $builder->getId(),
165
            $builder->getPayload(),
166
            $connection,
167
            $cache,
168
            $transformer
169
        );
170
    }
171
172
    /**
173
     * @param string $id
174
     * @param array $payload
175
     * @param ConnectionInterface|string|null $connection
176
     * @param CacheInterface|string|null $cache
177
     * @param TransformerCollectionInterface|array|null $transformer
178
     * @return PipelineBuilderInterface
179
     * @throws \yii\base\InvalidConfigException
180
     */
181
    public function rawAddPipeline(
182
        string $id,
183
        array $payload,
184
        ConnectionInterface $connection = null,
185
        CacheInterface $cache = null,
186
        TransformerCollectionInterface $transformer = null
187
    ): PipelineBuilderInterface {
188
        $transformer = TransformerHelper::populateTransformerCollection(
189
            TransformerHelper::resolveCollection($transformer),
1 ignored issue
show
Bug introduced by
It seems like \flipbox\hubspot\helpers...ollection($transformer) targeting flipbox\hubspot\helpers\...er::resolveCollection() can also be of type object or string; however, flipbox\hubspot\helpers\...TransformerCollection() does only seem to accept null|object<flipbox\hubs...merCollectionInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
190
            [
191
                'resource' => [static::addRelayBuilderClass()]
192
            ]
193
        );
194
195
        return (new Resource(
196
            $this->rawHttpAddRelay(
197
                $id,
198
                $payload,
199
                ConnectionHelper::resolveConnection($connection),
200
                CacheHelper::resolveCache($cache)
201
            ),
202
            $transformer,
203
            HubSpot::getInstance()->getPsrLogger()
204
        ));
205
    }
206
207
    /**
208
     * @param ObjectBuilderInterface $builder
209
     * @param ConnectionInterface|string|null $connection
210
     * @param CacheInterface|string|null $cache
211
     * @return callable
212
     * @throws \yii\base\InvalidConfigException
213
     */
214
    public function httpAddRelay(
215
        ObjectBuilderInterface $builder,
216
        ConnectionInterface $connection = null,
217
        CacheInterface $cache = null
218
    ): callable {
219
        return $this->rawHttpAddRelay(
220
            $builder->getId(),
221
            $builder->getPayload(),
222
            $connection,
223
            $cache
224
        );
225
    }
226
227
    /**
228
     * @param string $id
229
     * @param array $payload
230
     * @param ConnectionInterface|string|null $connection
231
     * @param CacheInterface|string|null $cache
232
     * @return callable
233
     * @throws \yii\base\InvalidConfigException
234
     */
235
    public function rawHttpAddRelay(
236
        string $id,
237
        array $payload,
238
        ConnectionInterface $connection = null,
239
        CacheInterface $cache = null
240
    ): callable {
241
        $class = static::addRelayBuilderClass();
242
243
        /** @var RelayBuilderInterface $builder */
244
        $builder = new $class(
245
            $id,
246
            $payload,
247
            ConnectionHelper::resolveConnection($connection),
248
            CacheHelper::resolveCache($cache),
249
            HubSpot::getInstance()->getPsrLogger()
250
        );
251
252
        return $builder->build();
253
    }
254
255
    /**
256
     * @param ObjectBuilderInterface $builder
257
     * @param ConnectionInterface|string|null $connection
258
     * @param CacheInterface|string|null $cache
259
     * @return ResponseInterface
260
     * @throws \yii\base\InvalidConfigException
261
     */
262
    public function httpAdd(
263
        ObjectBuilderInterface $builder,
264
        ConnectionInterface $connection = null,
265
        CacheInterface $cache = null
266
    ): ResponseInterface {
267
        return $this->rawHttpAdd(
268
            $builder->getId(),
269
            $builder->getPayload(),
270
            $connection,
271
            $cache
272
        );
273
    }
274
275
    /**
276
     * @param string $id
277
     * @param array $payload
278
     * @param ConnectionInterface|string|null $connection
279
     * @param CacheInterface|string|null $cache
280
     * @return ResponseInterface
281
     * @throws \yii\base\InvalidConfigException
282
     */
283
    public function rawHttpAdd(
284
        string $id,
285
        array $payload,
286
        ConnectionInterface $connection = null,
287
        CacheInterface $cache = null
288
    ): ResponseInterface {
289
        return $this->rawHttpAddRelay(
290
            $id,
291
            $payload,
292
            $connection,
293
            $cache
294
        )();
295
    }
296
297
298
    /*******************************************
299
     * REMOVE
300
     *******************************************/
301
302
    /**
303
     * @param ObjectBuilderInterface $builder
304
     * @param ConnectionInterface|string|null $connection
305
     * @param CacheInterface|string|null $cache
306
     * @param TransformerCollectionInterface|array|null $transformer
307
     * @param null $source
308
     * @return mixed
309
     * @throws \yii\base\InvalidConfigException
310
     */
311
    public function remove(
312
        ObjectBuilderInterface $builder,
313
        ConnectionInterface $connection = null,
314
        CacheInterface $cache = null,
315
        TransformerCollectionInterface $transformer = null,
316
        $source = null
317
    ) {
318
        return $this->rawRemove(
319
            $builder->getId(),
320
            $builder->getPayload(),
321
            $connection,
322
            $cache,
323
            $transformer,
324
            $source
325
        );
326
    }
327
328
    /**
329
     * @param string $id
330
     * @param array $payload
331
     * @param ConnectionInterface|string|null $connection
332
     * @param CacheInterface|string|null $cache
333
     * @param TransformerCollectionInterface|array|null $transformer
334
     * @param null $source
335
     * @return mixed
336
     * @throws \yii\base\InvalidConfigException
337
     */
338
    public function rawRemove(
339
        string $id,
340
        array $payload,
341
        ConnectionInterface $connection = null,
342
        CacheInterface $cache = null,
343
        TransformerCollectionInterface $transformer = null,
344
        $source = null
345
    ) {
346
        return $this->rawRemovePipeline(
347
            $id,
348
            $payload,
349
            $connection,
350
            $cache,
351
            $transformer
352
        )($source);
353
    }
354
355
    /**
356
     * @param ObjectBuilderInterface $builder
357
     * @param ConnectionInterface|string|null $connection
358
     * @param CacheInterface|string|null $cache
359
     * @param TransformerCollectionInterface|array|null $transformer
360
     * @return PipelineBuilderInterface
361
     * @throws \yii\base\InvalidConfigException
362
     */
363
    public function removePipeline(
364
        ObjectBuilderInterface $builder,
365
        ConnectionInterface $connection = null,
366
        CacheInterface $cache = null,
367
        TransformerCollectionInterface $transformer = null
368
    ): PipelineBuilderInterface {
369
        return $this->rawRemovePipeline(
370
            $builder->getId(),
371
            $builder->getPayload(),
372
            $connection,
373
            $cache,
374
            $transformer
375
        );
376
    }
377
378
    /**
379
     * @param string $id
380
     * @param array $payload
381
     * @param ConnectionInterface|string|null $connection
382
     * @param CacheInterface|string|null $cache
383
     * @param TransformerCollectionInterface|array|null $transformer
384
     * @return PipelineBuilderInterface
385
     * @throws \yii\base\InvalidConfigException
386
     */
387
    public function rawRemovePipeline(
388
        string $id,
389
        array $payload,
390
        ConnectionInterface $connection = null,
391
        CacheInterface $cache = null,
392
        TransformerCollectionInterface $transformer = null
393
    ): PipelineBuilderInterface {
394
        $transformer = TransformerHelper::populateTransformerCollection(
395
            TransformerHelper::resolveCollection($transformer),
1 ignored issue
show
Bug introduced by
It seems like \flipbox\hubspot\helpers...ollection($transformer) targeting flipbox\hubspot\helpers\...er::resolveCollection() can also be of type object or string; however, flipbox\hubspot\helpers\...TransformerCollection() does only seem to accept null|object<flipbox\hubs...merCollectionInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
396
            [
397
                'resource' => [static::removeRelayBuilderClass()]
398
            ]
399
        );
400
401
        return (new Resource(
402
            $this->rawHttpRemoveRelay(
403
                $id,
404
                $payload,
405
                ConnectionHelper::resolveConnection($connection),
406
                CacheHelper::$cache($connection)
407
            ),
408
            $transformer,
409
            HubSpot::getInstance()->getPsrLogger()
410
        ));
411
    }
412
413
    /**
414
     * @param ObjectBuilderInterface $builder
415
     * @param ConnectionInterface|string|null $connection
416
     * @param CacheInterface|string|null $cache
417
     * @return callable
418
     * @throws \yii\base\InvalidConfigException
419
     */
420
    public function httpRemoveRelay(
421
        ObjectBuilderInterface $builder,
422
        ConnectionInterface $connection = null,
423
        CacheInterface $cache = null
424
    ): callable {
425
        return $this->rawHttpRemoveRelay(
426
            $builder->getId(),
427
            $builder->getPayload(),
428
            $connection,
429
            $cache
430
        );
431
    }
432
433
    /**
434
     * @param string $id
435
     * @param array $payload
436
     * @param ConnectionInterface|string|null $connection
437
     * @param CacheInterface|string|null $cache
438
     * @return callable
439
     * @throws \yii\base\InvalidConfigException
440
     */
441
    public function rawHttpRemoveRelay(
442
        string $id,
443
        array $payload,
444
        ConnectionInterface $connection = null,
445
        CacheInterface $cache = null
446
    ): callable {
447
        $class = static::removeRelayBuilderClass();
448
449
        /** @var RelayBuilderInterface $builder */
450
        $builder = new $class(
451
            $id,
452
            $payload,
453
            ConnectionHelper::resolveConnection($connection),
454
            CacheHelper::resolveCache($cache),
455
            HubSpot::getInstance()->getPsrLogger()
456
        );
457
458
        return $builder->build();
459
    }
460
461
    /**
462
     * @param ObjectBuilderInterface $builder
463
     * @param ConnectionInterface|string|null $connection
464
     * @param CacheInterface|string|null $cache
465
     * @return ResponseInterface
466
     * @throws \yii\base\InvalidConfigException
467
     */
468
    public function httpRemove(
469
        ObjectBuilderInterface $builder,
470
        ConnectionInterface $connection = null,
471
        CacheInterface $cache = null
472
    ): ResponseInterface {
473
        return $this->rawHttpRemove(
474
            $builder->getId(),
475
            $builder->getPayload(),
476
            $connection,
477
            $cache
478
        );
479
    }
480
481
    /**
482
     * @param string $id
483
     * @param array $payload
484
     * @param ConnectionInterface|string|null $connection
485
     * @param CacheInterface|string|null $cache
486
     * @return ResponseInterface
487
     * @throws \yii\base\InvalidConfigException
488
     */
489
    public function rawHttpRemove(
490
        string $id,
491
        array $payload,
492
        ConnectionInterface $connection = null,
493
        CacheInterface $cache = null
494
    ): ResponseInterface {
495
        return $this->rawHttpRemoveRelay(
496
            $id,
497
            $payload,
498
            $connection,
499
            $cache
500
        )();
501
    }
502
}
503