Completed
Push — develop ( 136153...525928 )
by Nate
03:34
created

CompanyContacts::rawHttpAddRelay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 9
cp 0
rs 9.6333
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 flipbox\hubspot\builders\CompanyContactsBuilder;
12
use flipbox\hubspot\builders\CompanyContactsBuilderInterface;
13
use flipbox\hubspot\connections\ConnectionInterface;
14
use flipbox\hubspot\criteria\CompanyContactsCriteria;
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\TransformerCollectionInterface;
23
use flipbox\hubspot\transformers\DynamicModelSuccess;
24
use Flipbox\Relay\Builder\RelayBuilderInterface;
25
use Flipbox\Relay\HubSpot\Builder\Resources\Company\Contacts\Add;
26
use Flipbox\Relay\HubSpot\Builder\Resources\Company\Contacts\All;
27
use Flipbox\Relay\HubSpot\Builder\Resources\Company\Contacts\Remove;
28
use League\Pipeline\PipelineBuilderInterface;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\SimpleCache\CacheInterface;
31
use yii\base\Component;
32
33
/**
34
 * @author Flipbox Factory <[email protected]>
35
 * @since 1.0.0
36
 */
37
class CompanyContacts extends Component
38
{
39
    use ReadObjectTrait;
40
41
    /**
42
     * The HubSpot Resource name
43
     */
44
    const HUBSPOT_RESOURCE = 'companyContacts';
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public static function defaultTransformer()
50
    {
51
        return [
52
            'class' => DynamicTransformerCollection::class,
53
            'handle' => self::HUBSPOT_RESOURCE,
54
            'transformers' => [
55
                TransformerCollectionInterface::SUCCESS_KEY => [
56
                    'class' => DynamicModelSuccess::class,
57
                    'resource' => self::HUBSPOT_RESOURCE
58
                ]
59
            ]
60
        ];
61
    }
62
63
    /**
64
     * @param array $config
65
     * @return ObjectCriteriaInterface
66
     */
67
    public function getCriteria(array $config = []): ObjectCriteriaInterface
68
    {
69
        return new CompanyContactsCriteria($config);
70
    }
71
72
    /**
73
     * @param array $config
74
     * @return CompanyContactsBuilderInterface
75
     */
76
    public function getBuilder(array $config = []): CompanyContactsBuilderInterface
77
    {
78
        return new CompanyContactsBuilder($config);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    protected static function readRelayBuilderClass(): string
85
    {
86
        return All::class;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    protected static function addRelayBuilderClass(): string
93
    {
94
        return Add::class;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    protected static function removeRelayBuilderClass(): string
101
    {
102
        return Remove::class;
103
    }
104
105
106
    /*******************************************
107
     * ADD
108
     *******************************************/
109
110
    /**
111
     * @param CompanyContactsBuilderInterface $builder
112
     * @param ConnectionInterface|string|null $connection
113
     * @param CacheInterface|string|null $cache
114
     * @param TransformerCollectionInterface|array|null $transformer
115
     * @param null $source
116
     * @return mixed
117
     * @throws \yii\base\InvalidConfigException
118
     */
119
    public function add(
120
        CompanyContactsBuilderInterface $builder,
121
        ConnectionInterface $connection = null,
122
        CacheInterface $cache = null,
123
        TransformerCollectionInterface $transformer = null,
124
        $source = null
125
    ) {
126
        return $this->rawAdd(
127
            $builder->getCompanyId(),
128
            $builder->getContactId(),
129
            $connection,
130
            $cache,
131
            $transformer,
132
            $source
133
        );
134
    }
135
136
    /**
137
     * @param string $companyId
138
     * @param string $contactId
139
     * @param ConnectionInterface|string|null $connection
140
     * @param CacheInterface|string|null $cache
141
     * @param TransformerCollectionInterface|array|null $transformer
142
     * @return PipelineBuilderInterface
143
     * @throws \yii\base\InvalidConfigException
144
     */
145
    public function rawAddPipeline(
146
        string $companyId,
147
        string $contactId,
148
        ConnectionInterface $connection = null,
149
        CacheInterface $cache = null,
150
        TransformerCollectionInterface $transformer = null
151
    ): PipelineBuilderInterface {
152
        $transformer = TransformerHelper::populateTransformerCollection(
153
            TransformerHelper::resolveCollection($transformer),
154
            [
155
                'resource' => [static::addRelayBuilderClass()]
156
            ]
157
        );
158
159
        return (new Resource(
160
            $this->rawHttpAddRelay(
161
                $companyId,
162
                $contactId,
163
                ConnectionHelper::resolveConnection($connection),
164
                CacheHelper::resolveCache($cache)
165
            ),
166
            $transformer,
167
            HubSpot::getInstance()->getPsrLogger()
168
        ));
169
    }
170
171
    /**
172
     * @param string $companyId
173
     * @param string $contactId
174
     * @param ConnectionInterface|string|null $connection
175
     * @param CacheInterface|string|null $cache
176
     * @param TransformerCollectionInterface|array|null $transformer
177
     * @param null $source
178
     * @return mixed
179
     * @throws \yii\base\InvalidConfigException
180
     */
181
    public function rawAdd(
182
        string $companyId,
183
        string $contactId,
184
        ConnectionInterface $connection = null,
185
        CacheInterface $cache = null,
186
        TransformerCollectionInterface $transformer = null,
187
        $source = null
188
    ) {
189
        return $this->rawAddPipeline(
190
            $companyId,
191
            $contactId,
192
            $connection,
193
            $cache,
194
            $transformer
195
        )($source);
196
    }
197
198
199
    /**
200
     * @param CompanyContactsBuilderInterface $builder
201
     * @param ConnectionInterface|string|null $connection
202
     * @param CacheInterface|string|null $cache
203
     * @param TransformerCollectionInterface|array|null $transformer
204
     * @return PipelineBuilderInterface
205
     * @throws \yii\base\InvalidConfigException
206
     */
207
    public function addPipeline(
208
        CompanyContactsBuilderInterface $builder,
209
        ConnectionInterface $connection = null,
210
        CacheInterface $cache = null,
211
        TransformerCollectionInterface $transformer = null
212
    ): PipelineBuilderInterface {
213
        return $this->rawAddPipeline(
214
            $builder->getCompanyId(),
215
            $builder->getContactId(),
216
            $connection,
217
            $cache,
218
            $transformer
219
        );
220
    }
221
222
    /**
223
     * @param CompanyContactsBuilderInterface $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
        CompanyContactsBuilderInterface $builder,
231
        ConnectionInterface $connection = null,
232
        CacheInterface $cache = null
233
    ): callable {
234
        return $this->rawHttpAddRelay(
235
            $builder->getCompanyId(),
236
            $builder->getContactId(),
237
            $connection,
238
            $cache
239
        );
240
    }
241
242
    /**
243
     * @param string $companyId
244
     * @param string $contactId
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 $companyId,
252
        string $contactId,
253
        ConnectionInterface $connection = null,
254
        CacheInterface $cache = null
255
    ): callable {
256
        $class = static::addRelayBuilderClass();
257
258
        /** @var RelayBuilderInterface $builder */
259
        $builder = new $class(
260
            $companyId,
261
            $contactId,
262
            ConnectionHelper::resolveConnection($connection),
263
            CacheHelper::resolveCache($cache),
264
            HubSpot::getInstance()->getPsrLogger()
265
        );
266
267
        return $builder->build();
268
    }
269
270
    /**
271
     * @param CompanyContactsBuilderInterface $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
        CompanyContactsBuilderInterface $builder,
279
        ConnectionInterface $connection = null,
280
        CacheInterface $cache = null
281
    ): ResponseInterface {
282
        return $this->rawHttpAdd(
283
            $builder->getCompanyId(),
284
            $builder->getContactId(),
285
            $connection,
286
            $cache
287
        );
288
    }
289
290
    /**
291
     * @param string $companyId
292
     * @param string $contactId
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 $companyId,
300
        string $contactId,
301
        ConnectionInterface $connection = null,
302
        CacheInterface $cache = null
303
    ): ResponseInterface {
304
        return $this->rawHttpAddRelay(
305
            $companyId,
306
            $contactId,
307
            $connection,
308
            $cache
309
        )();
310
    }
311
312
313
    /*******************************************
314
     * REMOVE
315
     *******************************************/
316
317
    /**
318
     * @param CompanyContactsBuilderInterface $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
        CompanyContactsBuilderInterface $builder,
328
        ConnectionInterface $connection = null,
329
        CacheInterface $cache = null,
330
        TransformerCollectionInterface $transformer = null,
331
        $source = null
332
    ) {
333
        return $this->rawRemove(
334
            $builder->getCompanyId(),
335
            $builder->getContactId(),
336
            $connection,
337
            $cache,
338
            $transformer,
339
            $source
340
        );
341
    }
342
343
    /**
344
     * @param string $companyId
345
     * @param string $contactId
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 $companyId,
355
        string $contactId,
356
        ConnectionInterface $connection = null,
357
        CacheInterface $cache = null,
358
        TransformerCollectionInterface $transformer = null,
359
        $source = null
360
    ) {
361
        return $this->rawRemovePipeline(
362
            $companyId,
363
            $contactId,
364
            $connection,
365
            $cache,
366
            $transformer
367
        )($source);
368
    }
369
370
    /**
371
     * @param CompanyContactsBuilderInterface $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
        CompanyContactsBuilderInterface $builder,
380
        ConnectionInterface $connection = null,
381
        CacheInterface $cache = null,
382
        TransformerCollectionInterface $transformer = null
383
    ): PipelineBuilderInterface {
384
        return $this->rawRemovePipeline(
385
            $builder->getCompanyId(),
386
            $builder->getContactId(),
387
            $connection,
388
            $cache,
389
            $transformer
390
        );
391
    }
392
393
    /**
394
     * @param string $companyId
395
     * @param string $contactId
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 $companyId,
404
        string $contactId,
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->rawHttpAddRelay(
418
                $companyId,
419
                $contactId,
420
                ConnectionHelper::resolveConnection($connection),
421
                CacheHelper::resolveCache($cache)
422
            ),
423
            $transformer,
424
            HubSpot::getInstance()->getPsrLogger()
425
        ));
426
    }
427
428
    /**
429
     * @param CompanyContactsBuilderInterface $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
        CompanyContactsBuilderInterface $builder,
437
        ConnectionInterface $connection = null,
438
        CacheInterface $cache = null
439
    ): callable {
440
        return $this->rawHttpRemoveRelay(
441
            $builder->getCompanyId(),
442
            $builder->getContactId(),
443
            $connection,
444
            $cache
445
        );
446
    }
447
448
    /**
449
     * @param string $companyId
450
     * @param string $contactId
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 $companyId,
458
        string $contactId,
459
        ConnectionInterface $connection = null,
460
        CacheInterface $cache = null
461
    ): callable {
462
        $class = static::removeRelayBuilderClass();
463
464
        /** @var RelayBuilderInterface $builder */
465
        $builder = new $class(
466
            $companyId,
467
            $contactId,
468
            ConnectionHelper::resolveConnection($connection),
469
            CacheHelper::resolveCache($cache),
470
            HubSpot::getInstance()->getPsrLogger()
471
        );
472
473
        return $builder->build();
474
    }
475
476
    /**
477
     * @param CompanyContactsBuilderInterface $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
        CompanyContactsBuilderInterface $builder,
485
        ConnectionInterface $connection = null,
486
        CacheInterface $cache = null
487
    ): ResponseInterface {
488
        return $this->rawHttpRemove(
489
            $builder->getCompanyId(),
490
            $builder->getContactId(),
491
            $connection,
492
            $cache
493
        );
494
    }
495
496
    /**
497
     * @param string $companyId
498
     * @param string $contactId
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 $companyId,
506
        string $contactId,
507
        ConnectionInterface $connection = null,
508
        CacheInterface $cache = null
509
    ): ResponseInterface {
510
        return $this->rawHttpRemoveRelay(
511
            $companyId,
512
            $contactId,
513
            $connection,
514
            $cache
515
        )();
516
    }
517
}
518