1 | <?php |
||
34 | abstract class Objects extends Integrations implements ObjectsFieldInterface |
||
35 | { |
||
36 | /** |
||
37 | * @inheritdoc |
||
38 | */ |
||
39 | const TRANSLATION_CATEGORY = 'hubspot'; |
||
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | const INPUT_TEMPLATE_PATH = 'hubspot/_components/fieldtypes/Objects/input'; |
||
45 | |||
46 | /** |
||
47 | * @inheritdoc |
||
48 | */ |
||
49 | const INPUT_ITEM_TEMPLATE_PATH = 'hubspot/_components/fieldtypes/Objects/_inputItem'; |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | const SETTINGS_TEMPLATE_PATH = 'hubspot/_components/fieldtypes/Objects/settings'; |
||
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | */ |
||
59 | const ACTION_PREFORM_ACTION_PATH = 'hubspot/cp/fields/perform-action'; |
||
60 | |||
61 | /** |
||
62 | * @inheritdoc |
||
63 | */ |
||
64 | const ACTION_CREATE_ITEM_PATH = 'hubspot/cp/fields/create-item'; |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | const ACTION_ASSOCIATION_ITEM_PATH = 'hubspot/cp/objects/associate'; |
||
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | const ACTION_DISSOCIATION_ITEM_PATH = 'hubspot/cp/objects/dissociate'; |
||
75 | |||
76 | /** |
||
77 | * @inheritdoc |
||
78 | */ |
||
79 | const ACTION_PREFORM_ITEM_ACTION_PATH = 'hubspot/cp/fields/perform-item-action'; |
||
80 | |||
81 | /** |
||
82 | * Indicates whether the full sync operation should be preformed if a matching HubSpot Object was found but not |
||
83 | * currently associated to the element. For example, when attempting to Sync a Craft User to a HubSpot Contact, if |
||
84 | * the HubSpot Contact already exists; true would override data in HubSpot while false would just perform |
||
85 | * an association (note, a subsequent sync operation could be preformed) |
||
86 | * @var bool |
||
87 | * |
||
88 | * @deprecated |
||
89 | */ |
||
90 | public $syncToHubSpotOnMatch = false; |
||
91 | |||
92 | /** |
||
93 | * @inheritdoc |
||
94 | */ |
||
95 | protected $defaultAvailableActions = [ |
||
96 | SyncTo::class |
||
97 | ]; |
||
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | protected $defaultAvailableItemActions = [ |
||
103 | SyncItemFrom::class, |
||
104 | SyncItemTo::class, |
||
105 | ]; |
||
106 | |||
107 | /** |
||
108 | * @param array $payload |
||
109 | * @param string|null $id |
||
110 | * @return ResponseInterface |
||
111 | */ |
||
112 | abstract protected function upsertToHubSpot( |
||
116 | |||
117 | /** |
||
118 | * @param ResponseInterface $response |
||
119 | * @return string|null |
||
120 | */ |
||
121 | abstract protected function getObjectIdFromResponse(ResponseInterface $response); |
||
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | public static function recordClass(): string |
||
130 | |||
131 | /******************************************* |
||
132 | * CONNECTION |
||
133 | *******************************************/ |
||
134 | |||
135 | /** |
||
136 | * @return ConnectionInterface |
||
137 | * @throws \flipbox\craft\integration\exceptions\ConnectionNotFound |
||
138 | */ |
||
139 | public function getConnection(): ConnectionInterface |
||
143 | |||
144 | /******************************************* |
||
145 | * CACHE |
||
146 | *******************************************/ |
||
147 | |||
148 | /** |
||
149 | * @return CacheInterface |
||
150 | */ |
||
151 | public function getCache(): CacheInterface |
||
155 | |||
156 | |||
157 | /******************************************* |
||
158 | * SYNC TO |
||
159 | *******************************************/ |
||
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | public function syncToHubSpot( |
||
165 | ElementInterface $element, |
||
166 | string $objectId = null, |
||
167 | $transformer = null |
||
168 | ): bool { |
||
169 | /** @var Element $element */ |
||
170 | |||
171 | $id = $objectId ?: $this->resolveObjectIdFromElement($element); |
||
172 | |||
173 | // Get callable used to create payload |
||
174 | if (null === ($transformer = TransformerHelper::resolveTransformer($transformer))) { |
||
175 | $transformer = HubSpot::getInstance()->getSettings()->getSyncUpsertPayloadTransformer(); |
||
176 | } |
||
177 | |||
178 | // Create payload |
||
179 | $payload = call_user_func_array( |
||
180 | $transformer, |
||
181 | [ |
||
182 | $element, |
||
183 | $this, |
||
184 | $id |
||
185 | ] |
||
186 | ); |
||
187 | |||
188 | $response = $this->upsertToHubSpot($payload, $id); |
||
189 | |||
190 | return $this->handleSyncToHubSpotResponse( |
||
191 | $response, |
||
192 | $element, |
||
193 | $id, |
||
194 | $transformer |
||
195 | ); |
||
196 | } |
||
197 | |||
198 | /******************************************* |
||
199 | * SYNC FROM |
||
200 | *******************************************/ |
||
201 | |||
202 | /** |
||
203 | * @@inheritdoc |
||
204 | * @throws \Throwable |
||
205 | * @throws \craft\errors\ElementNotFoundException |
||
206 | * @throws \yii\base\Exception |
||
207 | */ |
||
208 | public function syncFromHubSpot( |
||
260 | |||
261 | /** |
||
262 | * @param ElementInterface|Element $element |
||
263 | * @param string $id |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function addAssociation( |
||
267 | ElementInterface $element, |
||
268 | string $id |
||
269 | ) { |
||
270 | /** @var IntegrationAssociationQuery $query */ |
||
271 | if (null === ($query = $element->getFieldValue($this->handle))) { |
||
272 | HubSpot::warning("Field is not available on element."); |
||
273 | return false; |
||
274 | }; |
||
275 | |||
276 | $associations = ArrayHelper::index($query->all(), 'objectId'); |
||
277 | |||
278 | if (!array_key_exists($id, $associations)) { |
||
279 | $associations[$id] = $association = new ObjectAssociation([ |
||
280 | 'element' => $element, |
||
281 | 'field' => $this, |
||
282 | 'siteId' => SiteHelper::ensureSiteId($element->siteId), |
||
283 | 'objectId' => $id |
||
284 | ]); |
||
285 | |||
286 | $query->setCachedResult(array_values($associations)); |
||
287 | |||
288 | return $association->save(); |
||
289 | } |
||
290 | |||
291 | return true; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param ResponseInterface $response |
||
296 | * @param ElementInterface $element |
||
297 | * @param string|null $objectId |
||
298 | * @param string|null|callable $transformer |
||
299 | * @return bool |
||
300 | */ |
||
301 | protected function handleSyncToHubSpotResponse( |
||
302 | ResponseInterface $response, |
||
303 | ElementInterface $element, |
||
304 | string $objectId = null, |
||
305 | $transformer = null |
||
306 | ): bool { |
||
307 | |||
308 | /** @var Element $element */ |
||
309 | |||
310 | if (!($response->getStatusCode() >= 200 && $response->getStatusCode() <= 299)) { |
||
311 | call_user_func_array( |
||
312 | new PopulateElementErrorsFromUpsertResponse(), |
||
313 | [ |
||
314 | $response, |
||
315 | $element, |
||
316 | $this, |
||
317 | $objectId |
||
318 | ] |
||
319 | ); |
||
320 | return false; |
||
321 | } |
||
322 | |||
323 | if (empty($objectId)) { |
||
324 | if (null === ($objectId = $this->getObjectIdFromResponse($response))) { |
||
325 | HubSpot::error("Unable to determine object id from response"); |
||
326 | return false; |
||
327 | }; |
||
328 | |||
329 | return $this->addAssociation($element, $objectId); |
||
330 | } |
||
331 | |||
332 | return true; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @param ElementInterface|Element $element |
||
337 | * @return null|string |
||
338 | */ |
||
339 | public function resolveObjectIdFromElement( |
||
347 | |||
348 | /** |
||
349 | * @param int $elementId |
||
350 | * @param int|null $siteId |
||
351 | * @return bool|false|string|null |
||
352 | */ |
||
353 | public function resolveObjectIdFromElementId( |
||
380 | |||
381 | /** |
||
382 | * @param string $objectId |
||
383 | * @param int|null $siteId |
||
384 | * @return bool|false|string|null |
||
385 | */ |
||
386 | public function resolveElementIdFromObjectId( |
||
413 | } |
||
414 |