|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/hubspot/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/hubspot/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\hubspot\services\resources\traits; |
|
10
|
|
|
|
|
11
|
|
|
use flipbox\hubspot\builders\ObjectBuilderInterface; |
|
12
|
|
|
use flipbox\hubspot\connections\ConnectionInterface; |
|
13
|
|
|
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface; |
|
14
|
|
|
use League\Pipeline\PipelineBuilderInterface; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Flipbox Factory <[email protected]> |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
trait UpsertObjectTrait |
|
23
|
|
|
{ |
|
24
|
|
|
use UpdateObjectTrait, |
|
25
|
|
|
CreateObjectTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string|null $identifier |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function upsertHasId(string $identifier = null): bool |
|
32
|
|
|
{ |
|
33
|
|
|
return empty($identifier); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param ObjectBuilderInterface $builder |
|
38
|
|
|
* @param ConnectionInterface|null $connection |
|
39
|
|
|
* @param CacheInterface|null $cache |
|
40
|
|
|
* @param TransformerCollectionInterface|null $transformer |
|
41
|
|
|
* @param null $source |
|
42
|
|
|
* @return mixed |
|
43
|
|
|
* @throws \yii\base\InvalidConfigException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function upsert( |
|
46
|
|
|
ObjectBuilderInterface $builder, |
|
47
|
|
|
ConnectionInterface $connection = null, |
|
48
|
|
|
CacheInterface $cache = null, |
|
49
|
|
|
TransformerCollectionInterface $transformer = null, |
|
50
|
|
|
$source = null |
|
51
|
|
|
) { |
|
52
|
|
|
return $this->rawUpsert( |
|
53
|
|
|
$builder->getPayload(), |
|
54
|
|
|
$builder->getId(), |
|
55
|
|
|
$connection, |
|
56
|
|
|
$cache, |
|
57
|
|
|
$transformer, |
|
58
|
|
|
$source |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param array $payload |
|
64
|
|
|
* @param string|null $identifier |
|
65
|
|
|
* @param ConnectionInterface|null $connection |
|
66
|
|
|
* @param CacheInterface|null $cache |
|
67
|
|
|
* @param TransformerCollectionInterface|null $transformer |
|
68
|
|
|
* @param null $source |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
* @throws \yii\base\InvalidConfigException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function rawUpsert( |
|
73
|
|
|
array $payload, |
|
74
|
|
|
string $identifier = null, |
|
75
|
|
|
ConnectionInterface $connection = null, |
|
76
|
|
|
CacheInterface $cache = null, |
|
77
|
|
|
TransformerCollectionInterface $transformer = null, |
|
78
|
|
|
$source = null |
|
79
|
|
|
) { |
|
80
|
|
|
if (!$this->upsertHasId($identifier)) { |
|
81
|
|
|
return $this->rawCreate( |
|
82
|
|
|
$payload, |
|
83
|
|
|
$connection, |
|
84
|
|
|
$transformer, |
|
85
|
|
|
$source |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->rawUpdate( |
|
90
|
|
|
$identifier, |
|
91
|
|
|
$payload, |
|
92
|
|
|
$connection, |
|
93
|
|
|
$cache, |
|
94
|
|
|
$transformer, |
|
95
|
|
|
$source |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param ObjectBuilderInterface $builder |
|
101
|
|
|
* @param ConnectionInterface|null $connection |
|
102
|
|
|
* @param CacheInterface|null $cache |
|
103
|
|
|
* @param TransformerCollectionInterface|null $transformer |
|
104
|
|
|
* @return PipelineBuilderInterface |
|
105
|
|
|
* @throws \yii\base\InvalidConfigException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function upsertPipeline( |
|
108
|
|
|
ObjectBuilderInterface $builder, |
|
109
|
|
|
ConnectionInterface $connection = null, |
|
110
|
|
|
CacheInterface $cache = null, |
|
111
|
|
|
TransformerCollectionInterface $transformer = null |
|
112
|
|
|
): PipelineBuilderInterface { |
|
113
|
|
|
return $this->rawUpsertPipeline( |
|
114
|
|
|
$builder->getPayload(), |
|
115
|
|
|
$builder->getId(), |
|
116
|
|
|
$connection, |
|
117
|
|
|
$cache, |
|
118
|
|
|
$transformer |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param array $payload |
|
124
|
|
|
* @param string|null $identifier |
|
125
|
|
|
* @param ConnectionInterface|null $connection |
|
126
|
|
|
* @param CacheInterface|null $cache |
|
127
|
|
|
* @param TransformerCollectionInterface|null $transformer |
|
128
|
|
|
* @return PipelineBuilderInterface |
|
129
|
|
|
* @throws \yii\base\InvalidConfigException |
|
130
|
|
|
*/ |
|
131
|
|
|
public function rawUpsertPipeline( |
|
132
|
|
|
array $payload, |
|
133
|
|
|
string $identifier = null, |
|
134
|
|
|
ConnectionInterface $connection = null, |
|
135
|
|
|
CacheInterface $cache = null, |
|
136
|
|
|
TransformerCollectionInterface $transformer = null |
|
137
|
|
|
): PipelineBuilderInterface { |
|
138
|
|
|
if (!$this->upsertHasId($identifier)) { |
|
139
|
|
|
return $this->rawCreatePipeline( |
|
140
|
|
|
$payload, |
|
141
|
|
|
$connection |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this->rawUpdatePipeline( |
|
146
|
|
|
$identifier, |
|
147
|
|
|
$payload, |
|
148
|
|
|
$connection, |
|
149
|
|
|
$cache, |
|
150
|
|
|
$transformer |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param ObjectBuilderInterface $builder |
|
156
|
|
|
* @param ConnectionInterface|null $connection |
|
157
|
|
|
* @return callable |
|
158
|
|
|
* @throws \yii\base\InvalidConfigException |
|
159
|
|
|
*/ |
|
160
|
|
|
public function httpUpsertRelay( |
|
161
|
|
|
ObjectBuilderInterface $builder, |
|
162
|
|
|
ConnectionInterface $connection = null |
|
163
|
|
|
): callable { |
|
164
|
|
|
return $this->rawHttpUpsertRelay( |
|
165
|
|
|
$builder->getPayload(), |
|
166
|
|
|
$connection |
|
|
|
|
|
|
167
|
|
|
); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param array $payload |
|
172
|
|
|
* @param string|null $identifier |
|
173
|
|
|
* @param ConnectionInterface|null $connection |
|
174
|
|
|
* @param CacheInterface|null $cache |
|
175
|
|
|
* @return callable |
|
176
|
|
|
* @throws \yii\base\InvalidConfigException |
|
177
|
|
|
*/ |
|
178
|
|
|
public function rawHttpUpsertRelay( |
|
179
|
|
|
array $payload, |
|
180
|
|
|
string $identifier = null, |
|
181
|
|
|
ConnectionInterface $connection = null, |
|
182
|
|
|
CacheInterface $cache = null |
|
183
|
|
|
): callable { |
|
184
|
|
|
if (!$this->upsertHasId($identifier)) { |
|
185
|
|
|
return $this->rawHttpCreateRelay( |
|
186
|
|
|
$payload, |
|
187
|
|
|
$connection |
|
188
|
|
|
); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $this->rawHttpUpdateRelay( |
|
192
|
|
|
$identifier, |
|
193
|
|
|
$payload, |
|
194
|
|
|
$connection, |
|
195
|
|
|
$cache |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param ObjectBuilderInterface $builder |
|
201
|
|
|
* @param ConnectionInterface|null $connection |
|
202
|
|
|
* @param CacheInterface|null $cache |
|
203
|
|
|
* @return ResponseInterface |
|
204
|
|
|
* @throws \yii\base\InvalidConfigException |
|
205
|
|
|
*/ |
|
206
|
|
|
public function httpUpsert( |
|
207
|
|
|
ObjectBuilderInterface $builder, |
|
208
|
|
|
ConnectionInterface $connection = null, |
|
209
|
|
|
CacheInterface $cache = null |
|
210
|
|
|
): ResponseInterface { |
|
211
|
|
|
return $this->rawHttpUpsert( |
|
212
|
|
|
$builder->getPayload(), |
|
213
|
|
|
$builder->getId(), |
|
214
|
|
|
$connection, |
|
215
|
|
|
$cache |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param array $payload |
|
221
|
|
|
* @param string|null $identifier |
|
222
|
|
|
* @param ConnectionInterface|null $connection |
|
223
|
|
|
* @param CacheInterface|null $cache |
|
224
|
|
|
* @return ResponseInterface |
|
225
|
|
|
* @throws \yii\base\InvalidConfigException |
|
226
|
|
|
*/ |
|
227
|
|
|
public function rawHttpUpsert( |
|
228
|
|
|
array $payload, |
|
229
|
|
|
string $identifier = null, |
|
230
|
|
|
ConnectionInterface $connection = null, |
|
231
|
|
|
CacheInterface $cache = null |
|
232
|
|
|
): ResponseInterface { |
|
233
|
|
|
if (!$this->upsertHasId($identifier)) { |
|
234
|
|
|
return $this->rawHttpCreate( |
|
235
|
|
|
$payload, |
|
236
|
|
|
$connection |
|
237
|
|
|
); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $this->rawHttpUpdate( |
|
241
|
|
|
$identifier, |
|
242
|
|
|
$payload, |
|
243
|
|
|
$connection, |
|
|
|
|
|
|
244
|
|
|
$cache |
|
|
|
|
|
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.