Completed
Push — master ( dc2754...670e2d )
by Nate
02:13
created

Contact::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 0
cts 15
cp 0
rs 9.7666
cc 1
nc 1
nop 5
crap 2
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
            $builder = new ReadById(
134
                $identifier,
135
                $connection ?: HubSpot::getConnection(),
136
                $cache ?: HubSpot::getCache(),
137
                $logger ?: HubSpot::getLogger(),
138
                $config
139
            );
140
141
            return $builder->build();
142
        }
143
144
        $builder = new ReadByEmail(
145
            $identifier,
146
            $connection ?: HubSpot::getConnection(),
147
            $cache ?: HubSpot::getCache(),
148
            $logger ?: HubSpot::getLogger(),
149
            $config
150
        );
151
152
        return $builder->build();
153
    }
154
155
156
    /*******************************************
157
     * UPDATE
158
     *******************************************/
159
160
    /**
161
     * @param string $identifier
162
     * @param array $payload
163
     * @param ConnectionInterface|null $connection
164
     * @param CacheInterface|null $cache
165
     * @param LoggerInterface|null $logger
166
     * @param array $config
167
     * @return ResponseInterface
168
     */
169
    public static function update(
170
        array $payload,
171
        string $identifier,
172
        ConnectionInterface $connection = null,
173
        CacheInterface $cache = null,
174
        LoggerInterface $logger = null,
175
        array $config = []
176
    ): ResponseInterface {
177
        return static::updateRelay(
178
            $payload,
179
            $identifier,
180
            $connection,
181
            $cache,
182
            $logger,
183
            $config
184
        )();
185
    }
186
187
    /**
188
     * @param string $identifier
189
     * @param array $payload
190
     * @param ConnectionInterface|null $connection
191
     * @param CacheInterface|null $cache
192
     * @param LoggerInterface|null $logger
193
     * @param array $config
194
     * @return callable
195
     */
196
    public static function updateRelay(
197
        array $payload,
198
        string $identifier,
199
        ConnectionInterface $connection = null,
200
        CacheInterface $cache = null,
201
        LoggerInterface $logger = null,
202
        array $config = []
203
    ): callable {
204
205
        $builder = new Update(
206
            $identifier,
207
            $payload,
208
            $connection ?: HubSpot::getConnection(),
209
            $cache ?: HubSpot::getCache(),
210
            $logger ?: HubSpot::getLogger(),
211
            $config
212
        );
213
214
        return $builder->build();
215
    }
216
217
218
    /*******************************************
219
     * UPSERT
220
     *******************************************/
221
222
    /**
223
     * @param ConnectionInterface|null $connection
224
     * @param CacheInterface|null $cache
225
     * @param array $payload
226
     * @param string|null $identifier
227
     * @param LoggerInterface|null $logger
228
     * @param array $config
229
     * @return ResponseInterface
230
     */
231
    public static function upsert(
232
        array $payload,
233
        string $identifier = null,
234
        ConnectionInterface $connection = null,
235
        CacheInterface $cache = null,
236
        LoggerInterface $logger = null,
237
        array $config = []
238
    ): ResponseInterface {
239
        return static::upsertRelay(
240
            $payload,
241
            $identifier,
242
            $connection,
243
            $cache,
244
            $logger,
245
            $config
246
        )();
247
    }
248
249
    /**
250
     * @param ConnectionInterface|null $connection
251
     * @param CacheInterface|null $cache
252
     * @param array $payload
253
     * @param string|null $identifier
254
     * @param LoggerInterface|null $logger
255
     * @param array $config
256
     * @return callable
257
     */
258
    public static function upsertRelay(
259
        array $payload,
260
        string $identifier = null,
261
        ConnectionInterface $connection = null,
262
        CacheInterface $cache = null,
263
        LoggerInterface $logger = null,
264
        array $config = []
265
    ): callable {
266
267
        if (!static::upsertHasId($identifier)) {
268
            return static::createRelay(
269
                $payload,
270
                $connection,
271
                $logger,
272
                $config
273
            );
274
        }
275
276
        return static::updateRelay(
277
            $payload,
278
            $identifier,
279
            $connection,
280
            $cache,
281
            $logger,
282
            $config
283
        );
284
    }
285
286
287
    /*******************************************
288
     * DELETE
289
     *******************************************/
290
291
    /**
292
     * @param string $identifier
293
     * @param ConnectionInterface|null $connection
294
     * @param CacheInterface|null $cache
295
     * @param LoggerInterface $logger
296
     * @param array $config
297
     * @return ResponseInterface
298
     */
299
    public static function delete(
300
        string $identifier,
301
        ConnectionInterface $connection = null,
302
        CacheInterface $cache = null,
303
        LoggerInterface $logger = null,
304
        array $config = []
305
    ): ResponseInterface {
306
        return static::deleteRelay(
307
            $identifier,
308
            $connection,
309
            $cache,
310
            $logger,
311
            $config
312
        )();
313
    }
314
315
    /**
316
     * @param string $identifier
317
     * @param ConnectionInterface|null $connection
318
     * @param CacheInterface|null $cache
319
     * @param LoggerInterface $logger
320
     * @param array $config
321
     * @return callable
322
     */
323
    public static function deleteRelay(
324
        string $identifier,
325
        ConnectionInterface $connection = null,
326
        CacheInterface $cache = null,
327
        LoggerInterface $logger = null,
328
        array $config = []
329
    ): callable {
330
331
        $builder = new Delete(
332
            $identifier,
333
            $connection ?: HubSpot::getConnection(),
334
            $cache ?: HubSpot::getCache(),
335
            $logger ?: HubSpot::getLogger(),
336
            $config
337
        );
338
339
        return $builder->build();
340
    }
341
342
343
    /*******************************************
344
     * BATCH
345
     *******************************************/
346
347
    /**
348
     * @param ConnectionInterface $connection
349
     * @param array $payload
350
     * @param LoggerInterface $logger
351
     * @param array $config
352
     * @return ResponseInterface
353
     */
354
    public static function batch(
355
        array $payload,
356
        ConnectionInterface $connection = null,
357
        LoggerInterface $logger = null,
358
        array $config = []
359
    ): ResponseInterface {
360
        return static::batchRelay(
361
            $payload,
362
            $connection,
363
            $logger,
364
            $config
365
        )();
366
    }
367
368
    /**
369
     * @param ConnectionInterface $connection
370
     * @param array $payload
371
     * @param LoggerInterface $logger
372
     * @param array $config
373
     * @return callable
374
     */
375
    public static function batchRelay(
376
        array $payload,
377
        ConnectionInterface $connection = null,
378
        LoggerInterface $logger = null,
379
        array $config = []
380
    ): callable {
381
382
        $builder = new Batch(
383
            $payload,
384
            $connection ?: HubSpot::getConnection(),
385
            $logger ?: HubSpot::getLogger(),
386
            $config
387
        );
388
389
        return $builder->build();
390
    }
391
}
392