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