|
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\ContactBuilder; |
|
16
|
|
|
use flipbox\hubspot\builders\ObjectBuilderInterface; |
|
17
|
|
|
use flipbox\hubspot\connections\ConnectionInterface; |
|
18
|
|
|
use flipbox\hubspot\criteria\ContactCriteria; |
|
19
|
|
|
use flipbox\hubspot\criteria\ObjectCriteriaInterface; |
|
20
|
|
|
use flipbox\hubspot\fields\Objects; |
|
21
|
|
|
use flipbox\hubspot\HubSpot; |
|
22
|
|
|
use flipbox\hubspot\pipeline\Resource; |
|
23
|
|
|
use flipbox\hubspot\pipeline\stages\ElementAssociationStage; |
|
24
|
|
|
use flipbox\hubspot\transformers\collections\DynamicTransformerCollection; |
|
25
|
|
|
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface; |
|
26
|
|
|
use flipbox\hubspot\transformers\DynamicModelSuccess; |
|
27
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Create; |
|
28
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Delete; |
|
29
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\ReadById; |
|
30
|
|
|
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Update; |
|
31
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
32
|
|
|
use yii\base\Component; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @author Flipbox Factory <[email protected]> |
|
36
|
|
|
* @since 1.0.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
class Contacts extends Component implements CRUDInterface |
|
39
|
|
|
{ |
|
40
|
|
|
use traits\SyncByElementTrait, |
|
41
|
|
|
traits\ReadObjectTrait, |
|
42
|
|
|
traits\UpsertObjectTrait, |
|
43
|
|
|
traits\DeleteObjectTrait; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The HubSpot Resource name |
|
47
|
|
|
*/ |
|
48
|
|
|
const HUBSPOT_RESOURCE = 'contacts'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function defaultTransformer() |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
|
|
'class' => DynamicTransformerCollection::class, |
|
57
|
|
|
'handle' => self::HUBSPOT_RESOURCE, |
|
58
|
|
|
'transformers' => [ |
|
59
|
|
|
TransformerCollectionInterface::SUCCESS_KEY => [ |
|
60
|
|
|
'class' => DynamicModelSuccess::class, |
|
61
|
|
|
'resource' => self::HUBSPOT_RESOURCE |
|
62
|
|
|
] |
|
63
|
|
|
] |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $config |
|
69
|
|
|
* @return ObjectCriteriaInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getCriteria(array $config = []): ObjectCriteriaInterface |
|
72
|
|
|
{ |
|
73
|
|
|
return new ContactCriteria($config); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param array $config |
|
78
|
|
|
* @return ObjectBuilderInterface |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getBuilder(array $config = []): ObjectBuilderInterface |
|
81
|
|
|
{ |
|
82
|
|
|
return new ContactBuilder($config); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
88
|
|
|
protected static function createRelayBuilderClass(): string |
|
89
|
|
|
{ |
|
90
|
|
|
return Create::class; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @inheritdoc |
|
95
|
|
|
*/ |
|
96
|
|
|
protected static function readRelayBuilderClass(): string |
|
97
|
|
|
{ |
|
98
|
|
|
return ReadById::class; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @inheritdoc |
|
103
|
|
|
*/ |
|
104
|
|
|
protected static function updateRelayBuilderClass(): string |
|
105
|
|
|
{ |
|
106
|
|
|
return Update::class; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @inheritdoc |
|
111
|
|
|
*/ |
|
112
|
|
|
protected static function deleteRelayBuilderClass(): string |
|
113
|
|
|
{ |
|
114
|
|
|
return Delete::class; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/******************************************* |
|
118
|
|
|
* SYNC |
|
119
|
|
|
*******************************************/ |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @inheritdoc |
|
123
|
|
|
* @throws \yii\base\InvalidConfigException |
|
124
|
|
|
*/ |
|
125
|
|
|
public function syncUp( |
|
126
|
|
|
ElementInterface $element, |
|
127
|
|
|
Objects $field, |
|
128
|
|
|
ConnectionInterface $connection = null, |
|
129
|
|
|
CacheInterface $cache = null |
|
130
|
|
|
): bool { |
|
131
|
|
|
/** @var Element $element */ |
|
132
|
|
|
$httpResponse = $this->rawHttpUpsert( |
|
133
|
|
|
$this->transformElementPayload($element, $field), |
|
134
|
|
|
$this->transformElementId($element, $field), |
|
135
|
|
|
$connection, |
|
136
|
|
|
$cache |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
if ($httpResponse->getStatusCode() === 409) { |
|
140
|
|
|
$data = Json::decodeIfJson( |
|
141
|
|
|
$httpResponse->getBody()->getContents() |
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
|
|
$contactId = ArrayHelper::getValue($data, 'identityProfile.vid'); |
|
145
|
|
|
|
|
146
|
|
|
if (!HubSpot::getInstance()->getObjectAssociations()->associateByIds( |
|
147
|
|
|
$contactId, |
|
148
|
|
|
$element->getId(), |
|
149
|
|
|
$field->id, |
|
150
|
|
|
$element->siteId |
|
151
|
|
|
)) { |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if ($field->syncToHubSpotOnMatch === true) { |
|
156
|
|
|
return $this->syncUp( |
|
157
|
|
|
$element, |
|
158
|
|
|
$field, |
|
159
|
|
|
$connection, |
|
160
|
|
|
$cache |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return true; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
(new Resource( |
|
168
|
|
|
function () use ($httpResponse) { |
|
169
|
|
|
return $httpResponse; |
|
170
|
|
|
}, |
|
171
|
|
|
null, |
|
172
|
|
|
HubSpot::getInstance()->getPsrLogger() |
|
173
|
|
|
))->build()->pipe( |
|
174
|
|
|
new ElementAssociationStage($field) |
|
175
|
|
|
)(null, $element); |
|
176
|
|
|
|
|
177
|
|
|
return !$element->hasErrors(); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|