Completed
Push — master ( 6e9196...c55090 )
by Nate
02:30
created

Contact::readRelay()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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