ContactList::upsertRelay()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
ccs 0
cts 25
cp 0
rs 9.488
cc 2
nc 2
nop 6
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\ContactList\Create;
14
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\Delete;
15
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\Read;
16
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\Update;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Log\LoggerInterface;
19
use Psr\SimpleCache\CacheInterface;
20
21
/**
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 2.0.0
24
 */
25
class ContactList
26
{
27
    /**
28
     * @param string|null $identifier
29
     * @return bool
30
     */
31
    protected static function upsertHasId(string $identifier = null): bool
32
    {
33
        return !empty($identifier);
34
    }
35
36
    /*******************************************
37
     * CREATE
38
     *******************************************/
39
40
    /**
41
     * @param array $payload
42
     * @param ConnectionInterface|null $connection
43
     * @param LoggerInterface $logger
44
     * @param array $config
45
     * @return ResponseInterface
46
     */
47
    public static function create(
48
        array $payload,
49
        ConnectionInterface $connection = null,
50
        LoggerInterface $logger = null,
51
        array $config = []
52
    ): ResponseInterface {
53
        return static::createRelay(
54
            $payload,
55
            $connection,
56
            $logger,
57
            $config
58
        )();
59
    }
60
61
    /**
62
     * @param array $payload
63
     * @param ConnectionInterface|null $connection
64
     * @param LoggerInterface $logger
65
     * @param array $config
66
     * @return callable
67
     */
68
    public static function createRelay(
69
        array $payload,
70
        ConnectionInterface $connection = null,
71
        LoggerInterface $logger = null,
72
        array $config = []
73
    ): callable {
74
75
        $builder = new Create(
76
            $payload,
77
            $connection ?: HubSpot::getConnection(),
78
            $logger ?: HubSpot::getLogger(),
79
            $config
80
        );
81
82
        return $builder->build();
83
    }
84
85
86
    /*******************************************
87
     * READ
88
     *******************************************/
89
90
    /**
91
     * @param ConnectionInterface|null $connection
92
     * @param CacheInterface|null $cache
93
     * @param string $identifier
94
     * @param LoggerInterface|null $logger
95
     * @param array $config
96
     * @return ResponseInterface
97
     */
98
    public static function read(
99
        string $identifier,
100
        ConnectionInterface $connection = null,
101
        CacheInterface $cache = null,
102
        LoggerInterface $logger = null,
103
        array $config = []
104
    ): ResponseInterface {
105
        return static::readRelay(
106
            $identifier,
107
            $connection,
108
            $cache,
109
            $logger,
110
            $config
111
        )();
112
    }
113
114
    /**
115
     * @param ConnectionInterface|null $connection
116
     * @param CacheInterface|null $cache
117
     * @param string $identifier
118
     * @param LoggerInterface|null $logger
119
     * @param array $config
120
     * @return callable
121
     */
122
    public static function readRelay(
123
        string $identifier,
124
        ConnectionInterface $connection = null,
125
        CacheInterface $cache = null,
126
        LoggerInterface $logger = null,
127
        array $config = []
128
    ): callable {
129
130
        $builder = new Read(
131
            $identifier,
132
            $connection ?: HubSpot::getConnection(),
133
            $cache ?: HubSpot::getCache(),
134
            $logger ?: HubSpot::getLogger(),
135
            $config
136
        );
137
138
        return $builder->build();
139
    }
140
141
142
    /*******************************************
143
     * UPDATE
144
     *******************************************/
145
146
    /**
147
     * @param string $identifier
148
     * @param array $payload
149
     * @param ConnectionInterface|null $connection
150
     * @param CacheInterface|null $cache
151
     * @param LoggerInterface|null $logger
152
     * @param array $config
153
     * @return ResponseInterface
154
     */
155
    public static function update(
156
        array $payload,
157
        string $identifier,
158
        ConnectionInterface $connection = null,
159
        CacheInterface $cache = null,
160
        LoggerInterface $logger = null,
161
        array $config = []
162
    ): ResponseInterface {
163
        return static::updateRelay(
164
            $payload,
165
            $identifier,
166
            $connection,
167
            $cache,
168
            $logger,
169
            $config
170
        )();
171
    }
172
173
    /**
174
     * @param string $identifier
175
     * @param array $payload
176
     * @param ConnectionInterface|null $connection
177
     * @param CacheInterface|null $cache
178
     * @param LoggerInterface|null $logger
179
     * @param array $config
180
     * @return callable
181
     */
182
    public static function updateRelay(
183
        array $payload,
184
        string $identifier,
185
        ConnectionInterface $connection = null,
186
        CacheInterface $cache = null,
187
        LoggerInterface $logger = null,
188
        array $config = []
189
    ): callable {
190
191
        $builder = new Update(
192
            $identifier,
193
            $payload,
194
            $connection ?: HubSpot::getConnection(),
195
            $cache ?: HubSpot::getCache(),
196
            $logger ?: HubSpot::getLogger(),
197
            $config
198
        );
199
200
        return $builder->build();
201
    }
202
203
204
    /*******************************************
205
     * UPSERT
206
     *******************************************/
207
208
    /**
209
     * @param ConnectionInterface|null $connection
210
     * @param CacheInterface|null $cache
211
     * @param array $payload
212
     * @param string|null $identifier
213
     * @param LoggerInterface|null $logger
214
     * @param array $config
215
     * @return ResponseInterface
216
     */
217
    public static function upsert(
218
        array $payload,
219
        string $identifier = null,
220
        ConnectionInterface $connection = null,
221
        CacheInterface $cache = null,
222
        LoggerInterface $logger = null,
223
        array $config = []
224
    ): ResponseInterface {
225
        return static::upsertRelay(
226
            $payload,
227
            $identifier,
228
            $connection,
229
            $cache,
230
            $logger,
231
            $config
232
        )();
233
    }
234
235
    /**
236
     * @param ConnectionInterface|null $connection
237
     * @param CacheInterface|null $cache
238
     * @param array $payload
239
     * @param string|null $identifier
240
     * @param LoggerInterface|null $logger
241
     * @param array $config
242
     * @return callable
243
     */
244
    public static function upsertRelay(
245
        array $payload,
246
        string $identifier = null,
247
        ConnectionInterface $connection = null,
248
        CacheInterface $cache = null,
249
        LoggerInterface $logger = null,
250
        array $config = []
251
    ): callable {
252
253
        if (!static::upsertHasId($identifier)) {
254
            return static::createRelay(
255
                $payload,
256
                $connection,
257
                $logger,
258
                $config
259
            );
260
        }
261
262
        return static::updateRelay(
263
            $payload,
264
            $identifier,
265
            $connection,
266
            $cache,
267
            $logger,
268
            $config
269
        );
270
    }
271
272
273
    /*******************************************
274
     * DELETE
275
     *******************************************/
276
277
    /**
278
     * @param string $identifier
279
     * @param ConnectionInterface|null $connection
280
     * @param CacheInterface|null $cache
281
     * @param LoggerInterface $logger
282
     * @param array $config
283
     * @return ResponseInterface
284
     */
285
    public static function delete(
286
        string $identifier,
287
        ConnectionInterface $connection = null,
288
        CacheInterface $cache = null,
289
        LoggerInterface $logger = null,
290
        array $config = []
291
    ): ResponseInterface {
292
        return static::deleteRelay(
293
            $identifier,
294
            $connection,
295
            $cache,
296
            $logger,
297
            $config
298
        )();
299
    }
300
301
    /**
302
     * @param string $identifier
303
     * @param ConnectionInterface|null $connection
304
     * @param CacheInterface|null $cache
305
     * @param LoggerInterface $logger
306
     * @param array $config
307
     * @return callable
308
     */
309
    public static function deleteRelay(
310
        string $identifier,
311
        ConnectionInterface $connection = null,
312
        CacheInterface $cache = null,
313
        LoggerInterface $logger = null,
314
        array $config = []
315
    ): callable {
316
317
        $builder = new Delete(
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