|
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; |
|
10
|
|
|
|
|
11
|
|
|
use craft\base\Element; |
|
12
|
|
|
use craft\base\ElementInterface; |
|
13
|
|
|
use craft\helpers\ArrayHelper; |
|
14
|
|
|
use craft\helpers\Json; |
|
15
|
|
|
use flipbox\hubspot\builders\ContactBatchBuilderInterface; |
|
16
|
|
|
use flipbox\hubspot\builders\ContactBuilder; |
|
17
|
|
|
use flipbox\hubspot\builders\ObjectBuilderInterface; |
|
18
|
|
|
use flipbox\hubspot\connections\ConnectionInterface; |
|
19
|
|
|
use flipbox\hubspot\criteria\ContactCriteria; |
|
20
|
|
|
use flipbox\hubspot\criteria\ObjectCriteriaInterface; |
|
21
|
|
|
use flipbox\hubspot\fields\Objects; |
|
22
|
|
|
use flipbox\hubspot\helpers\CacheHelper; |
|
23
|
|
|
use flipbox\hubspot\helpers\ConnectionHelper; |
|
24
|
|
|
use flipbox\hubspot\HubSpot; |
|
25
|
|
|
use flipbox\hubspot\pipeline\Resource; |
|
26
|
|
|
use flipbox\hubspot\pipeline\stages\ElementAssociationStage; |
|
27
|
|
|
use flipbox\hubspot\transformers\collections\DynamicTransformerCollection; |
|
28
|
|
|
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface; |
|
29
|
|
|
use flipbox\hubspot\transformers\DynamicModelSuccess; |
|
30
|
|
|
use Flipbox\Relay\Builder\RelayBuilderInterface; |
|
31
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Batch; |
|
32
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Create; |
|
33
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Delete; |
|
34
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\ReadById; |
|
35
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Update; |
|
36
|
|
|
use League\Pipeline\PipelineBuilderInterface; |
|
37
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
38
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
39
|
|
|
use yii\base\Component; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @author Flipbox Factory <[email protected]> |
|
43
|
|
|
* @since 1.0.0 |
|
44
|
|
|
*/ |
|
45
|
|
|
class Contacts extends Component implements CRUDInterface |
|
46
|
|
|
{ |
|
47
|
|
|
use traits\SyncByElementTrait, |
|
48
|
|
|
traits\ReadObjectTrait, |
|
49
|
|
|
traits\UpsertObjectTrait, |
|
50
|
|
|
traits\DeleteObjectTrait; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The HubSpot Resource name |
|
54
|
|
|
*/ |
|
55
|
|
|
const HUBSPOT_RESOURCE = 'contacts'; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function defaultTransformer() |
|
61
|
|
|
{ |
|
62
|
|
|
return [ |
|
63
|
|
|
'class' => DynamicTransformerCollection::class, |
|
64
|
|
|
'handle' => self::HUBSPOT_RESOURCE, |
|
65
|
|
|
'transformers' => [ |
|
66
|
|
|
TransformerCollectionInterface::SUCCESS_KEY => [ |
|
67
|
|
|
'class' => DynamicModelSuccess::class, |
|
68
|
|
|
'resource' => self::HUBSPOT_RESOURCE |
|
69
|
|
|
] |
|
70
|
|
|
] |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $config |
|
76
|
|
|
* @return ObjectCriteriaInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getCriteria(array $config = []): ObjectCriteriaInterface |
|
79
|
|
|
{ |
|
80
|
|
|
return new ContactCriteria($config); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array $config |
|
85
|
|
|
* @return ObjectBuilderInterface |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getBuilder(array $config = []): ObjectBuilderInterface |
|
88
|
|
|
{ |
|
89
|
|
|
return new ContactBuilder($config); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @inheritdoc |
|
94
|
|
|
*/ |
|
95
|
|
|
protected static function createRelayBuilderClass(): string |
|
96
|
|
|
{ |
|
97
|
|
|
return Create::class; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritdoc |
|
102
|
|
|
*/ |
|
103
|
|
|
protected static function readRelayBuilderClass(): string |
|
104
|
|
|
{ |
|
105
|
|
|
return ReadById::class; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritdoc |
|
110
|
|
|
*/ |
|
111
|
|
|
protected static function updateRelayBuilderClass(): string |
|
112
|
|
|
{ |
|
113
|
|
|
return Update::class; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @inheritdoc |
|
118
|
|
|
*/ |
|
119
|
|
|
protected static function deleteRelayBuilderClass(): string |
|
120
|
|
|
{ |
|
121
|
|
|
return Delete::class; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritdoc |
|
126
|
|
|
*/ |
|
127
|
|
|
protected static function batchRelayBuilderClass(): string |
|
128
|
|
|
{ |
|
129
|
|
|
return Batch::class; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/******************************************* |
|
133
|
|
|
* SYNC (OVERRIDE) |
|
134
|
|
|
*******************************************/ |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param ElementInterface $element |
|
138
|
|
|
* @param Objects $field |
|
139
|
|
|
* @param array $payload |
|
140
|
|
|
* @param string $id |
|
141
|
|
|
* @param ConnectionInterface|null $connection |
|
142
|
|
|
* @param CacheInterface|null $cache |
|
143
|
|
|
* @return bool |
|
144
|
|
|
* @throws \yii\base\InvalidConfigException |
|
145
|
|
|
*/ |
|
146
|
|
|
public function rawSyncUp( |
|
147
|
|
|
ElementInterface $element, |
|
148
|
|
|
Objects $field, |
|
149
|
|
|
array $payload, |
|
150
|
|
|
string $id = null, |
|
151
|
|
|
ConnectionInterface $connection = null, |
|
152
|
|
|
CacheInterface $cache = null |
|
153
|
|
|
): bool { |
|
154
|
|
|
/** @var Element $element */ |
|
155
|
|
|
$httpResponse = $this->rawHttpUpsert( |
|
156
|
|
|
$payload, |
|
157
|
|
|
$id, |
|
158
|
|
|
$connection, |
|
159
|
|
|
$cache |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
if ($httpResponse->getStatusCode() === 409) { |
|
163
|
|
|
$data = Json::decodeIfJson( |
|
164
|
|
|
$httpResponse->getBody()->getContents() |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
$contactId = ArrayHelper::getValue($data, 'identityProfile.vid'); |
|
168
|
|
|
|
|
169
|
|
|
if (!HubSpot::getInstance()->getObjectAssociations()->associateByIds( |
|
170
|
|
|
$contactId, |
|
171
|
|
|
$element->getId(), |
|
172
|
|
|
$field->id, |
|
173
|
|
|
$element->siteId |
|
174
|
|
|
)) { |
|
175
|
|
|
return false; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if ($field->syncToHubSpotOnMatch === true) { |
|
179
|
|
|
return $this->syncUp( |
|
180
|
|
|
$element, |
|
181
|
|
|
$field, |
|
182
|
|
|
$connection, |
|
183
|
|
|
$cache |
|
184
|
|
|
); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return true; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
(new Resource( |
|
191
|
|
|
function () use ($httpResponse) { |
|
192
|
|
|
return $httpResponse; |
|
193
|
|
|
}, |
|
194
|
|
|
null, |
|
195
|
|
|
HubSpot::getInstance()->getPsrLogger() |
|
196
|
|
|
))->build()->pipe( |
|
197
|
|
|
new ElementAssociationStage($field) |
|
198
|
|
|
)(null, $element); |
|
199
|
|
|
|
|
200
|
|
|
return !$element->hasErrors(); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/******************************************* |
|
204
|
|
|
* BATCH |
|
205
|
|
|
*******************************************/ |
|
206
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param ContactBatchBuilderInterface $batch |
|
211
|
|
|
* @param ConnectionInterface $connection = null |
|
212
|
|
|
* @return ResponseInterface |
|
213
|
|
|
* @throws \yii\base\InvalidConfigException |
|
214
|
|
|
*/ |
|
215
|
|
|
public function httpBatch( |
|
216
|
|
|
ContactBatchBuilderInterface $batch, |
|
217
|
|
|
ConnectionInterface $connection = null |
|
218
|
|
|
): ResponseInterface { |
|
219
|
|
|
return $this->rawHttpBatch( |
|
220
|
|
|
$batch->getPayload(), |
|
221
|
|
|
$connection |
|
222
|
|
|
)(); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param array $payload |
|
227
|
|
|
* @param ConnectionInterface|string|null $connection |
|
228
|
|
|
* @return ResponseInterface |
|
229
|
|
|
* @throws \yii\base\InvalidConfigException |
|
230
|
|
|
*/ |
|
231
|
|
|
public function rawHttpBatch( |
|
232
|
|
|
array $payload, |
|
233
|
|
|
ConnectionInterface $connection = null |
|
234
|
|
|
): ResponseInterface { |
|
235
|
|
|
return $this->rawHttpBatchRelay( |
|
236
|
|
|
$payload, |
|
237
|
|
|
$connection |
|
238
|
|
|
)(); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param ContactBatchBuilderInterface $batch |
|
243
|
|
|
* @param ConnectionInterface|string|null $connection |
|
244
|
|
|
* @return callable |
|
245
|
|
|
* @throws \yii\base\InvalidConfigException |
|
246
|
|
|
*/ |
|
247
|
|
|
public function httpBatchRelay( |
|
248
|
|
|
ContactBatchBuilderInterface $batch, |
|
249
|
|
|
ConnectionInterface $connection = null |
|
250
|
|
|
): callable { |
|
251
|
|
|
return $this->rawHttpBatchRelay( |
|
252
|
|
|
$batch->getPayload(), |
|
253
|
|
|
$connection |
|
254
|
|
|
); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @param array $payload |
|
259
|
|
|
* @param ConnectionInterface|string|null $connection |
|
260
|
|
|
* @return callable |
|
261
|
|
|
* @throws \yii\base\InvalidConfigException |
|
262
|
|
|
*/ |
|
263
|
|
|
public function rawHttpBatchRelay( |
|
264
|
|
|
array $payload, |
|
265
|
|
|
ConnectionInterface $connection = null |
|
266
|
|
|
): callable { |
|
267
|
|
|
$class = static::batchRelayBuilderClass(); |
|
268
|
|
|
|
|
269
|
|
|
/** @var RelayBuilderInterface $builder */ |
|
270
|
|
|
$builder = new $class( |
|
271
|
|
|
$payload, |
|
272
|
|
|
ConnectionHelper::resolveConnection($connection), |
|
273
|
|
|
HubSpot::getInstance()->getPsrLogger() |
|
274
|
|
|
); |
|
275
|
|
|
|
|
276
|
|
|
return $builder->build(); |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|