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\connections\ConnectionInterface; |
16
|
|
|
use flipbox\hubspot\criteria\ContactAccessor; |
17
|
|
|
use flipbox\hubspot\criteria\ContactBatchMutatorInterface; |
18
|
|
|
use flipbox\hubspot\criteria\ContactMutator; |
19
|
|
|
use flipbox\hubspot\criteria\ObjectAccessorInterface; |
20
|
|
|
use flipbox\hubspot\criteria\ObjectMutatorInterface; |
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 ObjectAccessorInterface |
75
|
|
|
*/ |
76
|
|
|
public function getAccessorCriteria(array $config = []): ObjectAccessorInterface |
77
|
|
|
{ |
78
|
|
|
return new ContactAccessor($config); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $config |
83
|
|
|
* @return ObjectMutatorInterface |
84
|
|
|
*/ |
85
|
|
|
public function getMutatorCriteria(array $config = []): ObjectMutatorInterface |
86
|
|
|
{ |
87
|
|
|
return new ContactMutator($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
|
|
|
return $this->handleSyncUpResponse( |
189
|
|
|
$httpResponse, |
190
|
|
|
$element, |
191
|
|
|
$field |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/******************************************* |
196
|
|
|
* BATCH |
197
|
|
|
*******************************************/ |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param ContactBatchMutatorInterface $criteria |
202
|
|
|
* @return ResponseInterface |
203
|
|
|
* @throws \yii\base\InvalidConfigException |
204
|
|
|
*/ |
205
|
|
|
public function httpBatch( |
206
|
|
|
ContactBatchMutatorInterface $criteria |
207
|
|
|
): ResponseInterface { |
208
|
|
|
return $this->rawHttpBatch( |
209
|
|
|
$criteria->getPayload(), |
210
|
|
|
$criteria->getConnection() |
211
|
|
|
)(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param array $payload |
216
|
|
|
* @param ConnectionInterface|string|null $connection |
217
|
|
|
* @return ResponseInterface |
218
|
|
|
* @throws \yii\base\InvalidConfigException |
219
|
|
|
*/ |
220
|
|
|
public function rawHttpBatch( |
221
|
|
|
array $payload, |
222
|
|
|
ConnectionInterface $connection = null |
223
|
|
|
): ResponseInterface { |
224
|
|
|
return $this->rawHttpBatchRelay( |
225
|
|
|
$payload, |
226
|
|
|
$connection |
227
|
|
|
)(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param ContactBatchMutatorInterface $criteria |
232
|
|
|
* @return callable |
233
|
|
|
* @throws \yii\base\InvalidConfigException |
234
|
|
|
*/ |
235
|
|
|
public function httpBatchRelay( |
236
|
|
|
ContactBatchMutatorInterface $criteria |
237
|
|
|
): callable { |
238
|
|
|
return $this->rawHttpBatchRelay( |
239
|
|
|
$criteria->getPayload(), |
240
|
|
|
$criteria->getConnection() |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param array $payload |
246
|
|
|
* @param ConnectionInterface|string|null $connection |
247
|
|
|
* @return callable |
248
|
|
|
* @throws \yii\base\InvalidConfigException |
249
|
|
|
*/ |
250
|
|
|
public function rawHttpBatchRelay( |
251
|
|
|
array $payload, |
252
|
|
|
ConnectionInterface $connection = null |
253
|
|
|
): callable { |
254
|
|
|
$class = static::batchRelayBuilderClass(); |
255
|
|
|
|
256
|
|
|
/** @var RelayBuilderInterface $builder */ |
257
|
|
|
$builder = new $class( |
258
|
|
|
$payload, |
259
|
|
|
ConnectionHelper::resolveConnection($connection), |
260
|
|
|
HubSpot::getInstance()->getPsrLogger() |
261
|
|
|
); |
262
|
|
|
|
263
|
|
|
return $builder->build(); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|