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\craft\hubspot\fields; |
10
|
|
|
|
11
|
|
|
use craft\base\ElementInterface; |
12
|
|
|
use craft\helpers\Json; |
13
|
|
|
use flipbox\craft\hubspot\criteria\ContactCriteria; |
14
|
|
|
use flipbox\craft\hubspot\HubSpot; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
class Contacts extends Objects |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
public function getObjectLabel(): string |
27
|
|
|
{ |
28
|
|
|
return 'Contact'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public static function displayName(): string |
35
|
|
|
{ |
36
|
|
|
return HubSpot::t('HubSpot: Contacts'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public static function defaultSelectionLabel(): string |
43
|
|
|
{ |
44
|
|
|
return HubSpot::t('Add a HubSpot Contact'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
* @throws \flipbox\craft\integration\exceptions\ConnectionNotFound |
50
|
|
|
*/ |
51
|
|
|
protected function upsertToHubSpot( |
52
|
|
|
array $payload, |
53
|
|
|
string $id = null |
54
|
|
|
): ResponseInterface { |
55
|
|
|
return (new ContactCriteria([ |
56
|
|
|
'connection' => $this->getConnection(), |
57
|
|
|
'cache' => $this->getCache(), |
58
|
|
|
'payload' => $payload, |
59
|
|
|
'id' => $id |
60
|
|
|
]))->upsert(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
* @throws \flipbox\craft\integration\exceptions\ConnectionNotFound |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
|
|
public function readFromHubSpot( |
69
|
|
|
string $id |
70
|
|
|
): ResponseInterface { |
71
|
|
|
return (new ContactCriteria([ |
72
|
|
|
'connection' => $this->getConnection(), |
73
|
|
|
'cache' => $this->getCache(), |
74
|
|
|
'id' => $id |
75
|
|
|
]))->read(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
* @return bool |
81
|
|
|
* @throws \Throwable |
82
|
|
|
*/ |
83
|
|
|
protected function handleSyncToHubSpotResponse( |
84
|
|
|
ResponseInterface $response, |
85
|
|
|
ElementInterface $element, |
86
|
|
|
string $objectId = null, |
87
|
|
|
$transformer = null |
88
|
|
|
): bool { |
89
|
|
|
|
90
|
|
|
// 409 = failure because contact already exists |
91
|
|
|
if ($response->getStatusCode() === 409) { |
92
|
|
|
$data = Json::decodeIfJson( |
93
|
|
|
$response->getBody()->getContents() |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$objectId = $data['identityProfile']['vid'] ?? null; |
97
|
|
|
|
98
|
|
|
if ($objectId !==null && $this->addAssociation($element, $objectId)) { |
99
|
|
|
return $this->syncToHubSpot($element, $objectId, $transformer); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$response->getBody()->rewind(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return parent::handleSyncToHubSpotResponse($response, $element, $objectId); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param ResponseInterface $response |
110
|
|
|
* @return string|null |
111
|
|
|
*/ |
112
|
|
|
protected function getObjectIdFromResponse(ResponseInterface $response) |
113
|
|
|
{ |
114
|
|
|
$data = Json::decodeIfJson( |
115
|
|
|
$response->getBody()->getContents() |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$id = $data['vid'] ?? null; |
119
|
|
|
|
120
|
|
|
return $id ? (string)$id : null; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|