Contact   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 538
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 42
lcom 2
cbo 8
dl 0
loc 538
ccs 0
cts 307
cp 0
rs 9.0399
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A upsertHasId() 0 4 1
A create() 0 13 1
A createRelay() 0 16 3
A read() 0 15 1
A readRelay() 0 39 4
A readById() 0 15 1
A readByIdRelay() 0 18 4
A readByEmail() 0 15 1
A readByEmailRelay() 0 18 4
A readByToken() 0 15 1
A readByTokenRelay() 0 18 4
A update() 0 17 1
A updateRelay() 0 20 4
A upsert() 0 17 1
A upsertRelay() 0 27 2
A delete() 0 15 1
A deleteRelay() 0 18 4
A batch() 0 13 1
A batchRelay() 0 16 3

How to fix   Complexity   

Complex Class

Complex classes like Contact often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Contact, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/hubspot/blob/master/LICENSE.md
6
 * @link       https://github.com/flipbox/hubspot
7
 */
8
9
namespace Flipbox\HubSpot\Resources;
10
11
use Flipbox\HubSpot\Connections\ConnectionInterface;
12
use Flipbox\HubSpot\HubSpot;
13
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Batch;
14
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Create;
15
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Delete;
16
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\ReadByToken;
17
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\ReadByEmail;
18
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\ReadById;
19
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Update;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Log\LoggerInterface;
22
use Psr\SimpleCache\CacheInterface;
23
24
/**
25
 * @author Flipbox Factory <[email protected]>
26
 * @since 2.0.0
27
 */
28
class Contact
29
{
30
    /**
31
     * @param string|null $identifier
32
     * @return bool
33
     */
34
    protected static function upsertHasId(string $identifier = null): bool
35
    {
36
        return !empty($identifier);
37
    }
38
39
    /*******************************************
40
     * CREATE
41
     *******************************************/
42
43
    /**
44
     * @param array $payload
45
     * @param ConnectionInterface|null $connection
46
     * @param LoggerInterface $logger
47
     * @param array $config
48
     * @return ResponseInterface
49
     */
50
    public static function create(
51
        array $payload,
52
        ConnectionInterface $connection = null,
53
        LoggerInterface $logger = null,
54
        array $config = []
55
    ): ResponseInterface {
56
        return static::createRelay(
57
            $payload,
58
            $connection,
59
            $logger,
60
            $config
61
        )();
62
    }
63
64
    /**
65
     * @param array $payload
66
     * @param ConnectionInterface|null $connection
67
     * @param LoggerInterface $logger
68
     * @param array $config
69
     * @return callable
70
     */
71
    public static function createRelay(
72
        array $payload,
73
        ConnectionInterface $connection = null,
74
        LoggerInterface $logger = null,
75
        array $config = []
76
    ): callable {
77
78
        $builder = new Create(
79
            $payload,
80
            $connection ?: HubSpot::getConnection(),
81
            $logger ?: HubSpot::getLogger(),
82
            $config
83
        );
84
85
        return $builder->build();
86
    }
87
88
89
    /*******************************************
90
     * READ
91
     *******************************************/
92
93
    /**
94
     * @param ConnectionInterface|null $connection
95
     * @param CacheInterface|null $cache
96
     * @param string $identifier
97
     * @param LoggerInterface|null $logger
98
     * @param array $config
99
     * @return ResponseInterface
100
     */
101
    public static function read(
102
        string $identifier,
103
        ConnectionInterface $connection = null,
104
        CacheInterface $cache = null,
105
        LoggerInterface $logger = null,
106
        array $config = []
107
    ): ResponseInterface {
108
        return static::readRelay(
109
            $identifier,
110
            $connection,
111
            $cache,
112
            $logger,
113
            $config
114
        )();
115
    }
116
117
    /**
118
     * @param ConnectionInterface|null $connection
119
     * @param CacheInterface|null $cache
120
     * @param string $identifier
121
     * @param LoggerInterface|null $logger
122
     * @param array $config
123
     * @return callable
124
     */
125
    public static function readRelay(
126
        string $identifier,
127
        ConnectionInterface $connection = null,
128
        CacheInterface $cache = null,
129
        LoggerInterface $logger = null,
130
        array $config = []
131
    ): callable {
132
133
134
135
        if (is_numeric($identifier)) {
136
            return static::readByIdRelay(
137
                $identifier,
138
                $connection,
139
                $cache,
140
                $logger,
141
                $config
142
            );
143
        }
144
145
        // If the identifier doesn't contain a '@' or '.' we'll treat it as a token
146
        if (false === strpos($identifier, '@') && false === strpos($identifier, '.')) {
147
            return static::readByTokenRelay(
148
                $identifier,
149
                $connection,
150
                $cache,
151
                $logger,
152
                $config
153
            );
154
        }
155
156
        return static::readByEmailRelay(
157
            $identifier,
158
            $connection,
159
            $cache,
160
            $logger,
161
            $config
162
        );
163
    }
164
165
    /*******************************************
166
     * READ BY ID
167
     *******************************************/
168
169
    /**
170
     * @param ConnectionInterface|null $connection
171
     * @param CacheInterface|null $cache
172
     * @param string $identifier
173
     * @param LoggerInterface|null $logger
174
     * @param array $config
175
     * @return ResponseInterface
176
     */
177
    public static function readById(
178
        string $identifier,
179
        ConnectionInterface $connection = null,
180
        CacheInterface $cache = null,
181
        LoggerInterface $logger = null,
182
        array $config = []
183
    ): ResponseInterface {
184
        return static::readByIdRelay(
185
            $identifier,
186
            $connection,
187
            $cache,
188
            $logger,
189
            $config
190
        )();
191
    }
192
193
    /**
194
     * @param ConnectionInterface|null $connection
195
     * @param CacheInterface|null $cache
196
     * @param string $identifier
197
     * @param LoggerInterface|null $logger
198
     * @param array $config
199
     * @return callable
200
     */
201
    public static function readByIdRelay(
202
        string $identifier,
203
        ConnectionInterface $connection = null,
204
        CacheInterface $cache = null,
205
        LoggerInterface $logger = null,
206
        array $config = []
207
    ): callable {
208
209
        $builder = new ReadById(
210
            $identifier,
211
            $connection ?: HubSpot::getConnection(),
212
            $cache ?: HubSpot::getCache(),
213
            $logger ?: HubSpot::getLogger(),
214
            $config
215
        );
216
217
        return $builder->build();
218
    }
219
220
    /*******************************************
221
     * READ BY EMAIL
222
     *******************************************/
223
224
    /**
225
     * @param ConnectionInterface|null $connection
226
     * @param CacheInterface|null $cache
227
     * @param string $identifier
228
     * @param LoggerInterface|null $logger
229
     * @param array $config
230
     * @return ResponseInterface
231
     */
232
    public static function readByEmail(
233
        string $identifier,
234
        ConnectionInterface $connection = null,
235
        CacheInterface $cache = null,
236
        LoggerInterface $logger = null,
237
        array $config = []
238
    ): ResponseInterface {
239
        return static::readByEmailRelay(
240
            $identifier,
241
            $connection,
242
            $cache,
243
            $logger,
244
            $config
245
        )();
246
    }
247
248
    /**
249
     * @param ConnectionInterface|null $connection
250
     * @param CacheInterface|null $cache
251
     * @param string $identifier
252
     * @param LoggerInterface|null $logger
253
     * @param array $config
254
     * @return callable
255
     */
256
    public static function readByEmailRelay(
257
        string $identifier,
258
        ConnectionInterface $connection = null,
259
        CacheInterface $cache = null,
260
        LoggerInterface $logger = null,
261
        array $config = []
262
    ): callable {
263
264
        $builder = new ReadByEmail(
265
            $identifier,
266
            $connection ?: HubSpot::getConnection(),
267
            $cache ?: HubSpot::getCache(),
268
            $logger ?: HubSpot::getLogger(),
269
            $config
270
        );
271
272
        return $builder->build();
273
    }
274
275
    /*******************************************
276
     * READ BY TOKEN
277
     *******************************************/
278
279
    /**
280
     * @param ConnectionInterface|null $connection
281
     * @param CacheInterface|null $cache
282
     * @param string $identifier
283
     * @param LoggerInterface|null $logger
284
     * @param array $config
285
     * @return ResponseInterface
286
     */
287
    public static function readByToken(
288
        string $identifier,
289
        ConnectionInterface $connection = null,
290
        CacheInterface $cache = null,
291
        LoggerInterface $logger = null,
292
        array $config = []
293
    ): ResponseInterface {
294
        return static::readByTokenRelay(
295
            $identifier,
296
            $connection,
297
            $cache,
298
            $logger,
299
            $config
300
        )();
301
    }
302
303
    /**
304
     * @param ConnectionInterface|null $connection
305
     * @param CacheInterface|null $cache
306
     * @param string $identifier
307
     * @param LoggerInterface|null $logger
308
     * @param array $config
309
     * @return callable
310
     */
311
    public static function readByTokenRelay(
312
        string $identifier,
313
        ConnectionInterface $connection = null,
314
        CacheInterface $cache = null,
315
        LoggerInterface $logger = null,
316
        array $config = []
317
    ): callable {
318
319
        $builder = new ReadByToken(
320
            $identifier,
321
            $connection ?: HubSpot::getConnection(),
322
            $cache ?: HubSpot::getCache(),
323
            $logger ?: HubSpot::getLogger(),
324
            $config
325
        );
326
327
        return $builder->build();
328
    }
329
330
    /*******************************************
331
     * UPDATE
332
     *******************************************/
333
334
    /**
335
     * @param string $identifier
336
     * @param array $payload
337
     * @param ConnectionInterface|null $connection
338
     * @param CacheInterface|null $cache
339
     * @param LoggerInterface|null $logger
340
     * @param array $config
341
     * @return ResponseInterface
342
     */
343
    public static function update(
344
        array $payload,
345
        string $identifier,
346
        ConnectionInterface $connection = null,
347
        CacheInterface $cache = null,
348
        LoggerInterface $logger = null,
349
        array $config = []
350
    ): ResponseInterface {
351
        return static::updateRelay(
352
            $payload,
353
            $identifier,
354
            $connection,
355
            $cache,
356
            $logger,
357
            $config
358
        )();
359
    }
360
361
    /**
362
     * @param string $identifier
363
     * @param array $payload
364
     * @param ConnectionInterface|null $connection
365
     * @param CacheInterface|null $cache
366
     * @param LoggerInterface|null $logger
367
     * @param array $config
368
     * @return callable
369
     */
370
    public static function updateRelay(
371
        array $payload,
372
        string $identifier,
373
        ConnectionInterface $connection = null,
374
        CacheInterface $cache = null,
375
        LoggerInterface $logger = null,
376
        array $config = []
377
    ): callable {
378
379
        $builder = new Update(
380
            $identifier,
381
            $payload,
382
            $connection ?: HubSpot::getConnection(),
383
            $cache ?: HubSpot::getCache(),
384
            $logger ?: HubSpot::getLogger(),
385
            $config
386
        );
387
388
        return $builder->build();
389
    }
390
391
392
    /*******************************************
393
     * UPSERT
394
     *******************************************/
395
396
    /**
397
     * @param ConnectionInterface|null $connection
398
     * @param CacheInterface|null $cache
399
     * @param array $payload
400
     * @param string|null $identifier
401
     * @param LoggerInterface|null $logger
402
     * @param array $config
403
     * @return ResponseInterface
404
     */
405
    public static function upsert(
406
        array $payload,
407
        string $identifier = null,
408
        ConnectionInterface $connection = null,
409
        CacheInterface $cache = null,
410
        LoggerInterface $logger = null,
411
        array $config = []
412
    ): ResponseInterface {
413
        return static::upsertRelay(
414
            $payload,
415
            $identifier,
416
            $connection,
417
            $cache,
418
            $logger,
419
            $config
420
        )();
421
    }
422
423
    /**
424
     * @param ConnectionInterface|null $connection
425
     * @param CacheInterface|null $cache
426
     * @param array $payload
427
     * @param string|null $identifier
428
     * @param LoggerInterface|null $logger
429
     * @param array $config
430
     * @return callable
431
     */
432
    public static function upsertRelay(
433
        array $payload,
434
        string $identifier = null,
435
        ConnectionInterface $connection = null,
436
        CacheInterface $cache = null,
437
        LoggerInterface $logger = null,
438
        array $config = []
439
    ): callable {
440
441
        if (!static::upsertHasId($identifier)) {
442
            return static::createRelay(
443
                $payload,
444
                $connection,
445
                $logger,
446
                $config
447
            );
448
        }
449
450
        return static::updateRelay(
451
            $payload,
452
            $identifier,
453
            $connection,
454
            $cache,
455
            $logger,
456
            $config
457
        );
458
    }
459
460
461
    /*******************************************
462
     * DELETE
463
     *******************************************/
464
465
    /**
466
     * @param string $identifier
467
     * @param ConnectionInterface|null $connection
468
     * @param CacheInterface|null $cache
469
     * @param LoggerInterface $logger
470
     * @param array $config
471
     * @return ResponseInterface
472
     */
473
    public static function delete(
474
        string $identifier,
475
        ConnectionInterface $connection = null,
476
        CacheInterface $cache = null,
477
        LoggerInterface $logger = null,
478
        array $config = []
479
    ): ResponseInterface {
480
        return static::deleteRelay(
481
            $identifier,
482
            $connection,
483
            $cache,
484
            $logger,
485
            $config
486
        )();
487
    }
488
489
    /**
490
     * @param string $identifier
491
     * @param ConnectionInterface|null $connection
492
     * @param CacheInterface|null $cache
493
     * @param LoggerInterface $logger
494
     * @param array $config
495
     * @return callable
496
     */
497
    public static function deleteRelay(
498
        string $identifier,
499
        ConnectionInterface $connection = null,
500
        CacheInterface $cache = null,
501
        LoggerInterface $logger = null,
502
        array $config = []
503
    ): callable {
504
505
        $builder = new Delete(
506
            $identifier,
507
            $connection ?: HubSpot::getConnection(),
508
            $cache ?: HubSpot::getCache(),
509
            $logger ?: HubSpot::getLogger(),
510
            $config
511
        );
512
513
        return $builder->build();
514
    }
515
516
517
    /*******************************************
518
     * BATCH
519
     *******************************************/
520
521
    /**
522
     * @param ConnectionInterface $connection
523
     * @param array $payload
524
     * @param LoggerInterface $logger
525
     * @param array $config
526
     * @return ResponseInterface
527
     */
528
    public static function batch(
529
        array $payload,
530
        ConnectionInterface $connection = null,
531
        LoggerInterface $logger = null,
532
        array $config = []
533
    ): ResponseInterface {
534
        return static::batchRelay(
535
            $payload,
536
            $connection,
537
            $logger,
538
            $config
539
        )();
540
    }
541
542
    /**
543
     * @param ConnectionInterface $connection
544
     * @param array $payload
545
     * @param LoggerInterface $logger
546
     * @param array $config
547
     * @return callable
548
     */
549
    public static function batchRelay(
550
        array $payload,
551
        ConnectionInterface $connection = null,
552
        LoggerInterface $logger = null,
553
        array $config = []
554
    ): callable {
555
556
        $builder = new Batch(
557
            $payload,
558
            $connection ?: HubSpot::getConnection(),
559
            $logger ?: HubSpot::getLogger(),
560
            $config
561
        );
562
563
        return $builder->build();
564
    }
565
}
566