Completed
Push — master ( 4ae5ab...1ded7d )
by Nate
02:23
created

Contact::readByTokenRelay()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.6666
c 0
b 0
f 0
cc 4
nc 1
nop 5
crap 20
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
        if (is_numeric($identifier)) {
134
            return static::readByIdRelay(
135
                $identifier,
136
                $connection,
137
                $cache,
138
                $logger,
139
                $config
140
            );
141
        }
142
143
        // If the identifier doesn't contain a '@' or '.' we'll treat it as a token
144
        if (false !== strpos($identifier, '@') && false !== strpos($identifier, '.')) {
145
            return static::readByTokenRelay(
146
                $identifier,
147
                $connection,
148
                $cache,
149
                $logger,
150
                $config
151
            );
152
        }
153
154
        return static::readByEmailRelay(
155
            $identifier,
156
            $connection,
157
            $cache,
158
            $logger,
159
            $config
160
        );
161
    }
162
163
    /*******************************************
164
     * READ BY ID
165
     *******************************************/
166
167
    /**
168
     * @param ConnectionInterface|null $connection
169
     * @param CacheInterface|null $cache
170
     * @param string $identifier
171
     * @param LoggerInterface|null $logger
172
     * @param array $config
173
     * @return ResponseInterface
174
     */
175
    public static function readById(
176
        string $identifier,
177
        ConnectionInterface $connection = null,
178
        CacheInterface $cache = null,
179
        LoggerInterface $logger = null,
180
        array $config = []
181
    ): ResponseInterface {
182
        return static::readByIdRelay(
183
            $identifier,
184
            $connection,
185
            $cache,
186
            $logger,
187
            $config
188
        )();
189
    }
190
191
    /**
192
     * @param ConnectionInterface|null $connection
193
     * @param CacheInterface|null $cache
194
     * @param string $identifier
195
     * @param LoggerInterface|null $logger
196
     * @param array $config
197
     * @return callable
198
     */
199
    public static function readByIdRelay(
200
        string $identifier,
201
        ConnectionInterface $connection = null,
202
        CacheInterface $cache = null,
203
        LoggerInterface $logger = null,
204
        array $config = []
205
    ): callable {
206
207
        $builder = new ReadById(
208
            $identifier,
209
            $connection ?: HubSpot::getConnection(),
210
            $cache ?: HubSpot::getCache(),
211
            $logger ?: HubSpot::getLogger(),
212
            $config
213
        );
214
215
        return $builder->build();
216
    }
217
218
    /*******************************************
219
     * READ BY EMAIL
220
     *******************************************/
221
222
    /**
223
     * @param ConnectionInterface|null $connection
224
     * @param CacheInterface|null $cache
225
     * @param string $identifier
226
     * @param LoggerInterface|null $logger
227
     * @param array $config
228
     * @return ResponseInterface
229
     */
230
    public static function readByEmail(
231
        string $identifier,
232
        ConnectionInterface $connection = null,
233
        CacheInterface $cache = null,
234
        LoggerInterface $logger = null,
235
        array $config = []
236
    ): ResponseInterface {
237
        return static::readByEmailRelay(
238
            $identifier,
239
            $connection,
240
            $cache,
241
            $logger,
242
            $config
243
        )();
244
    }
245
246
    /**
247
     * @param ConnectionInterface|null $connection
248
     * @param CacheInterface|null $cache
249
     * @param string $identifier
250
     * @param LoggerInterface|null $logger
251
     * @param array $config
252
     * @return callable
253
     */
254
    public static function readByEmailRelay(
255
        string $identifier,
256
        ConnectionInterface $connection = null,
257
        CacheInterface $cache = null,
258
        LoggerInterface $logger = null,
259
        array $config = []
260
    ): callable {
261
262
        $builder = new ReadByEmail(
263
            $identifier,
264
            $connection ?: HubSpot::getConnection(),
265
            $cache ?: HubSpot::getCache(),
266
            $logger ?: HubSpot::getLogger(),
267
            $config
268
        );
269
270
        return $builder->build();
271
    }
272
273
    /*******************************************
274
     * READ BY TOKEN
275
     *******************************************/
276
277
    /**
278
     * @param ConnectionInterface|null $connection
279
     * @param CacheInterface|null $cache
280
     * @param string $identifier
281
     * @param LoggerInterface|null $logger
282
     * @param array $config
283
     * @return ResponseInterface
284
     */
285
    public static function readByToken(
286
        string $identifier,
287
        ConnectionInterface $connection = null,
288
        CacheInterface $cache = null,
289
        LoggerInterface $logger = null,
290
        array $config = []
291
    ): ResponseInterface {
292
        return static::readByTokenRelay(
293
            $identifier,
294
            $connection,
295
            $cache,
296
            $logger,
297
            $config
298
        )();
299
    }
300
301
    /**
302
     * @param ConnectionInterface|null $connection
303
     * @param CacheInterface|null $cache
304
     * @param string $identifier
305
     * @param LoggerInterface|null $logger
306
     * @param array $config
307
     * @return callable
308
     */
309
    public static function readByTokenRelay(
310
        string $identifier,
311
        ConnectionInterface $connection = null,
312
        CacheInterface $cache = null,
313
        LoggerInterface $logger = null,
314
        array $config = []
315
    ): callable {
316
317
        $builder = new ReadByToken(
318
            $identifier,
319
            $connection ?: HubSpot::getConnection(),
320
            $cache ?: HubSpot::getCache(),
321
            $logger ?: HubSpot::getLogger(),
322
            $config
323
        );
324
325
        return $builder->build();
326
    }
327
328
    /*******************************************
329
     * UPDATE
330
     *******************************************/
331
332
    /**
333
     * @param string $identifier
334
     * @param array $payload
335
     * @param ConnectionInterface|null $connection
336
     * @param CacheInterface|null $cache
337
     * @param LoggerInterface|null $logger
338
     * @param array $config
339
     * @return ResponseInterface
340
     */
341
    public static function update(
342
        array $payload,
343
        string $identifier,
344
        ConnectionInterface $connection = null,
345
        CacheInterface $cache = null,
346
        LoggerInterface $logger = null,
347
        array $config = []
348
    ): ResponseInterface {
349
        return static::updateRelay(
350
            $payload,
351
            $identifier,
352
            $connection,
353
            $cache,
354
            $logger,
355
            $config
356
        )();
357
    }
358
359
    /**
360
     * @param string $identifier
361
     * @param array $payload
362
     * @param ConnectionInterface|null $connection
363
     * @param CacheInterface|null $cache
364
     * @param LoggerInterface|null $logger
365
     * @param array $config
366
     * @return callable
367
     */
368
    public static function updateRelay(
369
        array $payload,
370
        string $identifier,
371
        ConnectionInterface $connection = null,
372
        CacheInterface $cache = null,
373
        LoggerInterface $logger = null,
374
        array $config = []
375
    ): callable {
376
377
        $builder = new Update(
378
            $identifier,
379
            $payload,
380
            $connection ?: HubSpot::getConnection(),
381
            $cache ?: HubSpot::getCache(),
382
            $logger ?: HubSpot::getLogger(),
383
            $config
384
        );
385
386
        return $builder->build();
387
    }
388
389
390
    /*******************************************
391
     * UPSERT
392
     *******************************************/
393
394
    /**
395
     * @param ConnectionInterface|null $connection
396
     * @param CacheInterface|null $cache
397
     * @param array $payload
398
     * @param string|null $identifier
399
     * @param LoggerInterface|null $logger
400
     * @param array $config
401
     * @return ResponseInterface
402
     */
403
    public static function upsert(
404
        array $payload,
405
        string $identifier = null,
406
        ConnectionInterface $connection = null,
407
        CacheInterface $cache = null,
408
        LoggerInterface $logger = null,
409
        array $config = []
410
    ): ResponseInterface {
411
        return static::upsertRelay(
412
            $payload,
413
            $identifier,
414
            $connection,
415
            $cache,
416
            $logger,
417
            $config
418
        )();
419
    }
420
421
    /**
422
     * @param ConnectionInterface|null $connection
423
     * @param CacheInterface|null $cache
424
     * @param array $payload
425
     * @param string|null $identifier
426
     * @param LoggerInterface|null $logger
427
     * @param array $config
428
     * @return callable
429
     */
430
    public static function upsertRelay(
431
        array $payload,
432
        string $identifier = null,
433
        ConnectionInterface $connection = null,
434
        CacheInterface $cache = null,
435
        LoggerInterface $logger = null,
436
        array $config = []
437
    ): callable {
438
439
        if (!static::upsertHasId($identifier)) {
440
            return static::createRelay(
441
                $payload,
442
                $connection,
443
                $logger,
444
                $config
445
            );
446
        }
447
448
        return static::updateRelay(
449
            $payload,
450
            $identifier,
451
            $connection,
452
            $cache,
453
            $logger,
454
            $config
455
        );
456
    }
457
458
459
    /*******************************************
460
     * DELETE
461
     *******************************************/
462
463
    /**
464
     * @param string $identifier
465
     * @param ConnectionInterface|null $connection
466
     * @param CacheInterface|null $cache
467
     * @param LoggerInterface $logger
468
     * @param array $config
469
     * @return ResponseInterface
470
     */
471
    public static function delete(
472
        string $identifier,
473
        ConnectionInterface $connection = null,
474
        CacheInterface $cache = null,
475
        LoggerInterface $logger = null,
476
        array $config = []
477
    ): ResponseInterface {
478
        return static::deleteRelay(
479
            $identifier,
480
            $connection,
481
            $cache,
482
            $logger,
483
            $config
484
        )();
485
    }
486
487
    /**
488
     * @param string $identifier
489
     * @param ConnectionInterface|null $connection
490
     * @param CacheInterface|null $cache
491
     * @param LoggerInterface $logger
492
     * @param array $config
493
     * @return callable
494
     */
495
    public static function deleteRelay(
496
        string $identifier,
497
        ConnectionInterface $connection = null,
498
        CacheInterface $cache = null,
499
        LoggerInterface $logger = null,
500
        array $config = []
501
    ): callable {
502
503
        $builder = new Delete(
504
            $identifier,
505
            $connection ?: HubSpot::getConnection(),
506
            $cache ?: HubSpot::getCache(),
507
            $logger ?: HubSpot::getLogger(),
508
            $config
509
        );
510
511
        return $builder->build();
512
    }
513
514
515
    /*******************************************
516
     * BATCH
517
     *******************************************/
518
519
    /**
520
     * @param ConnectionInterface $connection
521
     * @param array $payload
522
     * @param LoggerInterface $logger
523
     * @param array $config
524
     * @return ResponseInterface
525
     */
526
    public static function batch(
527
        array $payload,
528
        ConnectionInterface $connection = null,
529
        LoggerInterface $logger = null,
530
        array $config = []
531
    ): ResponseInterface {
532
        return static::batchRelay(
533
            $payload,
534
            $connection,
535
            $logger,
536
            $config
537
        )();
538
    }
539
540
    /**
541
     * @param ConnectionInterface $connection
542
     * @param array $payload
543
     * @param LoggerInterface $logger
544
     * @param array $config
545
     * @return callable
546
     */
547
    public static function batchRelay(
548
        array $payload,
549
        ConnectionInterface $connection = null,
550
        LoggerInterface $logger = null,
551
        array $config = []
552
    ): callable {
553
554
        $builder = new Batch(
555
            $payload,
556
            $connection ?: HubSpot::getConnection(),
557
            $logger ?: HubSpot::getLogger(),
558
            $config
559
        );
560
561
        return $builder->build();
562
    }
563
}
564