Completed
Push — master ( 136153...d5a3eb )
by Nate
19:29 queued 17:38
created

CompanyContacts::defaultTransformer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 5
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\DynamicTransformerCollection;
23
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
24
use flipbox\hubspot\transformers\DynamicModelSuccess;
25
use Flipbox\Relay\Builder\RelayBuilderInterface;
26
use Flipbox\Relay\HubSpot\Builder\Resources\Company\Contacts\Add;
27
use Flipbox\Relay\HubSpot\Builder\Resources\Company\Contacts\All;
28
use Flipbox\Relay\HubSpot\Builder\Resources\Company\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 CompanyContacts extends Component
39
{
40
    use ReadObjectTrait;
41
42
    /**
43
     * The HubSpot Resource name
44
     */
45
    const HUBSPOT_RESOURCE = 'companyContacts';
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public static function defaultTransformer()
51
    {
52
        return [
53
            'class' => DynamicTransformerCollection::class,
54
            'handle' => self::HUBSPOT_RESOURCE,
55
            'transformers' => [
56
                TransformerCollectionInterface::SUCCESS_KEY => [
57
                    'class' => DynamicModelSuccess::class,
58
                    'resource' => self::HUBSPOT_RESOURCE
59
                ]
60
            ]
61
        ];
62
    }
63
64
    /**
65
     * @param array $config
66
     * @return ObjectCriteriaInterface
67
     */
68
    public function getCriteria(array $config = []): ObjectCriteriaInterface
69
    {
70
        return new CompanyContactsCriteria($config);
71
    }
72
73
    /**
74
     * @param array $config
75
     * @return CompanyContactsBuilderInterface
76
     */
77
    public function getBuilder(array $config = []): CompanyContactsBuilderInterface
78
    {
79
        return new CompanyContactsBuilder($config);
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85
    protected static function readRelayBuilderClass(): string
86
    {
87
        return All::class;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    protected static function addRelayBuilderClass(): string
94
    {
95
        return Add::class;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    protected static function removeRelayBuilderClass(): string
102
    {
103
        return Remove::class;
104
    }
105
106
107
    /*******************************************
108
     * ADD
109
     *******************************************/
110
111
    /**
112
     * @param CompanyContactsBuilderInterface $builder
113
     * @param ConnectionInterface|string|null $connection
114
     * @param CacheInterface|string|null $cache
115
     * @param TransformerCollectionInterface|array|null $transformer
116
     * @param null $source
117
     * @return mixed
118
     * @throws \yii\base\InvalidConfigException
119
     */
120
    public function add(
121
        CompanyContactsBuilderInterface $builder,
122
        ConnectionInterface $connection = null,
123
        CacheInterface $cache = null,
124
        TransformerCollectionInterface $transformer = null,
125
        $source = null
126
    ) {
127
        return $this->rawAdd(
128
            $builder->getCompanyId(),
129
            $builder->getContactId(),
130
            $connection,
131
            $cache,
132
            $transformer,
133
            $source
134
        );
135
    }
136
137
    /**
138
     * @param string $companyId
139
     * @param string $contactId
140
     * @param ConnectionInterface|string|null $connection
141
     * @param CacheInterface|string|null $cache
142
     * @param TransformerCollectionInterface|array|null $transformer
143
     * @return PipelineBuilderInterface
144
     * @throws \yii\base\InvalidConfigException
145
     */
146
    public function rawAddPipeline(
147
        string $companyId,
148
        string $contactId,
149
        ConnectionInterface $connection = null,
150
        CacheInterface $cache = null,
151
        TransformerCollectionInterface $transformer = null
152
    ): PipelineBuilderInterface {
153
        $transformer = TransformerHelper::populateTransformerCollection(
154
            TransformerHelper::resolveCollection($transformer),
155
            [
156
                'resource' => [static::addRelayBuilderClass()]
157
            ]
158
        );
159
160
        return (new Resource(
161
            $this->rawHttpAddRelay(
162
                $companyId,
163
                $contactId,
164
                ConnectionHelper::resolveConnection($connection),
165
                CacheHelper::resolveCache($cache)
166
            ),
167
            $transformer,
168
            HubSpot::getInstance()->getPsrLogger()
169
        ));
170
    }
171
172
    /**
173
     * @param string $companyId
174
     * @param string $contactId
175
     * @param ConnectionInterface|string|null $connection
176
     * @param CacheInterface|string|null $cache
177
     * @param TransformerCollectionInterface|array|null $transformer
178
     * @param null $source
179
     * @return mixed
180
     * @throws \yii\base\InvalidConfigException
181
     */
182
    public function rawAdd(
183
        string $companyId,
184
        string $contactId,
185
        ConnectionInterface $connection = null,
186
        CacheInterface $cache = null,
187
        TransformerCollectionInterface $transformer = null,
188
        $source = null
189
    ) {
190
        return $this->rawAddPipeline(
191
            $companyId,
192
            $contactId,
193
            $connection,
194
            $cache,
195
            $transformer
196
        )($source);
197
    }
198
199
200
    /**
201
     * @param CompanyContactsBuilderInterface $builder
202
     * @param ConnectionInterface|string|null $connection
203
     * @param CacheInterface|string|null $cache
204
     * @param TransformerCollectionInterface|array|null $transformer
205
     * @return PipelineBuilderInterface
206
     * @throws \yii\base\InvalidConfigException
207
     */
208
    public function addPipeline(
209
        CompanyContactsBuilderInterface $builder,
210
        ConnectionInterface $connection = null,
211
        CacheInterface $cache = null,
212
        TransformerCollectionInterface $transformer = null
213
    ): PipelineBuilderInterface {
214
        return $this->rawAddPipeline(
215
            $builder->getCompanyId(),
216
            $builder->getContactId(),
217
            $connection,
218
            $cache,
219
            $transformer
220
        );
221
    }
222
223
    /**
224
     * @param CompanyContactsBuilderInterface $builder
225
     * @param ConnectionInterface|string|null $connection
226
     * @param CacheInterface|string|null $cache
227
     * @return callable
228
     * @throws \yii\base\InvalidConfigException
229
     */
230
    public function httpAddRelay(
231
        CompanyContactsBuilderInterface $builder,
232
        ConnectionInterface $connection = null,
233
        CacheInterface $cache = null
234
    ): callable {
235
        return $this->rawHttpAddRelay(
236
            $builder->getCompanyId(),
237
            $builder->getContactId(),
238
            $connection,
239
            $cache
240
        );
241
    }
242
243
    /**
244
     * @param string $companyId
245
     * @param string $contactId
246
     * @param ConnectionInterface|string|null $connection
247
     * @param CacheInterface|string|null $cache
248
     * @return callable
249
     * @throws \yii\base\InvalidConfigException
250
     */
251
    public function rawHttpAddRelay(
252
        string $companyId,
253
        string $contactId,
254
        ConnectionInterface $connection = null,
255
        CacheInterface $cache = null
256
    ): callable {
257
        $class = static::addRelayBuilderClass();
258
259
        /** @var RelayBuilderInterface $builder */
260
        $builder = new $class(
261
            $companyId,
262
            $contactId,
263
            ConnectionHelper::resolveConnection($connection),
264
            CacheHelper::resolveCache($cache),
265
            HubSpot::getInstance()->getPsrLogger()
266
        );
267
268
        return $builder->build();
269
    }
270
271
    /**
272
     * @param CompanyContactsBuilderInterface $builder
273
     * @param ConnectionInterface|string|null $connection
274
     * @param CacheInterface|string|null $cache
275
     * @return ResponseInterface
276
     * @throws \yii\base\InvalidConfigException
277
     */
278
    public function httpAdd(
279
        CompanyContactsBuilderInterface $builder,
280
        ConnectionInterface $connection = null,
281
        CacheInterface $cache = null
282
    ): ResponseInterface {
283
        return $this->rawHttpAdd(
284
            $builder->getCompanyId(),
285
            $builder->getContactId(),
286
            $connection,
287
            $cache
288
        );
289
    }
290
291
    /**
292
     * @param string $companyId
293
     * @param string $contactId
294
     * @param ConnectionInterface|string|null $connection
295
     * @param CacheInterface|string|null $cache
296
     * @return ResponseInterface
297
     * @throws \yii\base\InvalidConfigException
298
     */
299
    public function rawHttpAdd(
300
        string $companyId,
301
        string $contactId,
302
        ConnectionInterface $connection = null,
303
        CacheInterface $cache = null
304
    ): ResponseInterface {
305
        return $this->rawHttpAddRelay(
306
            $companyId,
307
            $contactId,
308
            $connection,
309
            $cache
310
        )();
311
    }
312
313
314
    /*******************************************
315
     * REMOVE
316
     *******************************************/
317
318
    /**
319
     * @param CompanyContactsBuilderInterface $builder
320
     * @param ConnectionInterface|string|null $connection
321
     * @param CacheInterface|string|null $cache
322
     * @param TransformerCollectionInterface|array|null $transformer
323
     * @param null $source
324
     * @return mixed
325
     * @throws \yii\base\InvalidConfigException
326
     */
327
    public function remove(
328
        CompanyContactsBuilderInterface $builder,
329
        ConnectionInterface $connection = null,
330
        CacheInterface $cache = null,
331
        TransformerCollectionInterface $transformer = null,
332
        $source = null
333
    ) {
334
        return $this->rawRemove(
335
            $builder->getCompanyId(),
336
            $builder->getContactId(),
337
            $connection,
338
            $cache,
339
            $transformer,
340
            $source
341
        );
342
    }
343
344
    /**
345
     * @param string $companyId
346
     * @param string $contactId
347
     * @param ConnectionInterface|string|null $connection
348
     * @param CacheInterface|string|null $cache
349
     * @param TransformerCollectionInterface|array|null $transformer
350
     * @param null $source
351
     * @return mixed
352
     * @throws \yii\base\InvalidConfigException
353
     */
354
    public function rawRemove(
355
        string $companyId,
356
        string $contactId,
357
        ConnectionInterface $connection = null,
358
        CacheInterface $cache = null,
359
        TransformerCollectionInterface $transformer = null,
360
        $source = null
361
    ) {
362
        return $this->rawRemovePipeline(
363
            $companyId,
364
            $contactId,
365
            $connection,
366
            $cache,
367
            $transformer
368
        )($source);
369
    }
370
371
    /**
372
     * @param CompanyContactsBuilderInterface $builder
373
     * @param ConnectionInterface|string|null $connection
374
     * @param CacheInterface|string|null $cache
375
     * @param TransformerCollectionInterface|array|null $transformer
376
     * @return PipelineBuilderInterface
377
     * @throws \yii\base\InvalidConfigException
378
     */
379
    public function removePipeline(
380
        CompanyContactsBuilderInterface $builder,
381
        ConnectionInterface $connection = null,
382
        CacheInterface $cache = null,
383
        TransformerCollectionInterface $transformer = null
384
    ): PipelineBuilderInterface {
385
        return $this->rawRemovePipeline(
386
            $builder->getCompanyId(),
387
            $builder->getContactId(),
388
            $connection,
389
            $cache,
390
            $transformer
391
        );
392
    }
393
394
    /**
395
     * @param string $companyId
396
     * @param string $contactId
397
     * @param ConnectionInterface|string|null $connection
398
     * @param CacheInterface|string|null $cache
399
     * @param TransformerCollectionInterface|array|null $transformer
400
     * @return PipelineBuilderInterface
401
     * @throws \yii\base\InvalidConfigException
402
     */
403
    public function rawRemovePipeline(
404
        string $companyId,
405
        string $contactId,
406
        ConnectionInterface $connection = null,
407
        CacheInterface $cache = null,
408
        TransformerCollectionInterface $transformer = null
409
    ): PipelineBuilderInterface {
410
        $transformer = TransformerHelper::populateTransformerCollection(
411
            TransformerHelper::resolveCollection($transformer),
412
            [
413
                'resource' => [static::removeRelayBuilderClass()]
414
            ]
415
        );
416
417
        return (new Resource(
418
            $this->rawHttpAddRelay(
419
                $companyId,
420
                $contactId,
421
                ConnectionHelper::resolveConnection($connection),
422
                CacheHelper::resolveCache($cache)
423
            ),
424
            $transformer,
425
            HubSpot::getInstance()->getPsrLogger()
426
        ));
427
    }
428
429
    /**
430
     * @param CompanyContactsBuilderInterface $builder
431
     * @param ConnectionInterface|string|null $connection
432
     * @param CacheInterface|string|null $cache
433
     * @return callable
434
     * @throws \yii\base\InvalidConfigException
435
     */
436
    public function httpRemoveRelay(
437
        CompanyContactsBuilderInterface $builder,
438
        ConnectionInterface $connection = null,
439
        CacheInterface $cache = null
440
    ): callable {
441
        return $this->rawHttpRemoveRelay(
442
            $builder->getCompanyId(),
443
            $builder->getContactId(),
444
            $connection,
445
            $cache
446
        );
447
    }
448
449
    /**
450
     * @param string $companyId
451
     * @param string $contactId
452
     * @param ConnectionInterface|string|null $connection
453
     * @param CacheInterface|string|null $cache
454
     * @return callable
455
     * @throws \yii\base\InvalidConfigException
456
     */
457
    public function rawHttpRemoveRelay(
458
        string $companyId,
459
        string $contactId,
460
        ConnectionInterface $connection = null,
461
        CacheInterface $cache = null
462
    ): callable {
463
        $class = static::removeRelayBuilderClass();
464
465
        /** @var RelayBuilderInterface $builder */
466
        $builder = new $class(
467
            $companyId,
468
            $contactId,
469
            ConnectionHelper::resolveConnection($connection),
470
            CacheHelper::resolveCache($cache),
471
            HubSpot::getInstance()->getPsrLogger()
472
        );
473
474
        return $builder->build();
475
    }
476
477
    /**
478
     * @param CompanyContactsBuilderInterface $builder
479
     * @param ConnectionInterface|string|null $connection
480
     * @param CacheInterface|string|null $cache
481
     * @return ResponseInterface
482
     * @throws \yii\base\InvalidConfigException
483
     */
484
    public function httpRemove(
485
        CompanyContactsBuilderInterface $builder,
486
        ConnectionInterface $connection = null,
487
        CacheInterface $cache = null
488
    ): ResponseInterface {
489
        return $this->rawHttpRemove(
490
            $builder->getCompanyId(),
491
            $builder->getContactId(),
492
            $connection,
493
            $cache
494
        );
495
    }
496
497
    /**
498
     * @param string $companyId
499
     * @param string $contactId
500
     * @param ConnectionInterface|string|null $connection
501
     * @param CacheInterface|string|null $cache
502
     * @return ResponseInterface
503
     * @throws \yii\base\InvalidConfigException
504
     */
505
    public function rawHttpRemove(
506
        string $companyId,
507
        string $contactId,
508
        ConnectionInterface $connection = null,
509
        CacheInterface $cache = null
510
    ): ResponseInterface {
511
        return $this->rawHttpRemoveRelay(
512
            $companyId,
513
            $contactId,
514
            $connection,
515
            $cache
516
        )();
517
    }
518
}
519