1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace flipbox\hubspot\modules\resources\services; |
4
|
|
|
|
5
|
|
|
use craft\elements\User; |
6
|
|
|
use craft\helpers\Json; |
7
|
|
|
use flipbox\hubspot\authentication\AuthenticationStrategyInterface; |
8
|
|
|
use flipbox\hubspot\cache\CacheStrategyInterface; |
9
|
|
|
use flipbox\hubspot\HubSpot; |
10
|
|
|
use Flipbox\Transform\Factory; |
11
|
|
|
use Flipbox\Transform\Transformers\TransformerInterface; |
12
|
|
|
use flipbox\transformer\Transformer; |
13
|
|
|
use yii\base\Component; |
14
|
|
|
|
15
|
|
|
class Contacts extends AbstractResource |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param $data |
20
|
|
|
* @param callable|TransformerInterface $transformer |
21
|
|
|
* @param AuthenticationStrategyInterface|null $authenticationStrategy |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function create( |
25
|
|
|
$data, |
26
|
|
|
callable $transformer, |
27
|
|
|
AuthenticationStrategyInterface $authenticationStrategy = null |
28
|
|
|
) { |
29
|
|
|
$payload = Factory::item($transformer, $data); |
30
|
|
|
|
31
|
|
|
$response = HubSpot::getInstance()->http()->contacts()->create( |
32
|
|
|
$payload, |
33
|
|
|
$authenticationStrategy |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
// Interpret response |
37
|
|
|
if ($response->getStatusCode() !== 200) { |
38
|
|
|
$body = Json::decodeIfJson($response->getBody()->getContents()); |
39
|
|
|
|
40
|
|
|
HubSpot::warning( |
41
|
|
|
sprintf( |
42
|
|
|
"Unable to create user: %s, errors: %s", |
43
|
|
|
Json::encode($payload), |
44
|
|
|
Json::encode($body) |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
return [ |
49
|
|
|
false, |
50
|
|
|
$body |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return [ |
55
|
|
|
true, |
56
|
|
|
Json::decodeIfJson($response->getBody()->getContents()) |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $email |
62
|
|
|
* @param $data |
63
|
|
|
* @param callable|TransformerInterface $transformer |
64
|
|
|
* @param AuthenticationStrategyInterface|null $authenticationStrategy |
65
|
|
|
* @return bool|array |
66
|
|
|
*/ |
67
|
|
|
public function updateByEmail( |
68
|
|
|
string $email, |
69
|
|
|
$data, |
70
|
|
|
callable $transformer, |
71
|
|
|
AuthenticationStrategyInterface $authenticationStrategy = null |
72
|
|
|
) { |
73
|
|
|
$payload = Factory::item($transformer, $data); |
74
|
|
|
|
75
|
|
|
$response = HubSpot::getInstance()->http()->contacts()->updateByEmail( |
76
|
|
|
$email, |
77
|
|
|
$payload, |
78
|
|
|
$authenticationStrategy |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
// Interpret response |
82
|
|
|
if ($response->getStatusCode() !== 204) { |
83
|
|
|
$body = Json::decodeIfJson($response->getBody()->getContents()); |
84
|
|
|
|
85
|
|
|
HubSpot::warning( |
86
|
|
|
sprintf( |
87
|
|
|
"Unable to create user email: %s, errors: %s", |
88
|
|
|
$email, |
89
|
|
|
Json::encode($payload), |
90
|
|
|
Json::encode($body) |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
return $body; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param int $id |
101
|
|
|
* @param $data |
102
|
|
|
* @param callable|TransformerInterface $transformer |
103
|
|
|
* @param AuthenticationStrategyInterface|null $authenticationStrategy |
104
|
|
|
* @return bool|array |
105
|
|
|
*/ |
106
|
|
|
public function updateById( |
107
|
|
|
int $id, |
108
|
|
|
$data, |
109
|
|
|
callable $transformer, |
110
|
|
|
AuthenticationStrategyInterface $authenticationStrategy = null |
111
|
|
|
) { |
112
|
|
|
$payload = Factory::item($transformer, $data); |
113
|
|
|
|
114
|
|
|
$response = HubSpot::getInstance()->http()->contacts()->updateById( |
115
|
|
|
$id, |
116
|
|
|
$payload, |
117
|
|
|
$authenticationStrategy |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
// Interpret response |
121
|
|
|
if ($response->getStatusCode() !== 204) { |
122
|
|
|
$body = Json::decodeIfJson($response->getBody()->getContents()); |
123
|
|
|
|
124
|
|
|
HubSpot::warning( |
125
|
|
|
sprintf( |
126
|
|
|
"Unable to create user id %s: %s, errors: %s", |
127
|
|
|
$id, |
128
|
|
|
Json::encode($payload), |
129
|
|
|
Json::encode($body) |
130
|
|
|
) |
131
|
|
|
); |
132
|
|
|
return $body; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param int $id |
|
|
|
|
140
|
|
|
* @param $data |
141
|
|
|
* @param callable|TransformerInterface $transformer |
142
|
|
|
* @param AuthenticationStrategyInterface|null $authenticationStrategy |
143
|
|
|
* @return bool|array |
144
|
|
|
*/ |
145
|
|
|
public function batch( |
146
|
|
|
$data, |
147
|
|
|
callable $transformer, |
148
|
|
|
AuthenticationStrategyInterface $authenticationStrategy = null |
149
|
|
|
) { |
150
|
|
|
$payload = Factory::collection($transformer, $data); |
151
|
|
|
|
152
|
|
|
$response = HubSpot::getInstance()->http()->contacts()->batch( |
153
|
|
|
$payload, |
154
|
|
|
$authenticationStrategy |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
// Interpret response |
158
|
|
|
if ($response->getStatusCode() !== 202) { |
159
|
|
|
$body = Json::decodeIfJson($response->getBody()->getContents()); |
160
|
|
|
|
161
|
|
|
HubSpot::warning( |
162
|
|
|
sprintf( |
163
|
|
|
"Unable to batch users: %s, errors: %s", |
164
|
|
|
Json::encode($payload), |
165
|
|
|
Json::encode($body) |
166
|
|
|
) |
167
|
|
|
); |
168
|
|
|
return $body; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param int $id |
176
|
|
|
* @param callable $transformer |
177
|
|
|
* @param AuthenticationStrategyInterface|null $authenticationStrategy |
178
|
|
|
* @param CacheStrategyInterface|null $cacheStrategy |
179
|
|
|
* @return array|null |
180
|
|
|
*/ |
181
|
|
|
public function getById( |
182
|
|
|
int $id, |
183
|
|
|
callable $transformer, |
184
|
|
|
AuthenticationStrategyInterface $authenticationStrategy = null, |
185
|
|
|
CacheStrategyInterface $cacheStrategy = null |
186
|
|
|
) { |
187
|
|
|
// Get contact |
188
|
|
|
$response = HubSpot::getInstance()->http()->contacts()->getById( |
189
|
|
|
$id, |
190
|
|
|
$authenticationStrategy, |
191
|
|
|
$cacheStrategy |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
if ($response->getStatusCode() !== 200) { |
195
|
|
|
HubSpot::warning( |
196
|
|
|
sprintf( |
197
|
|
|
"Unable to get user with id: %s", |
198
|
|
|
$id |
199
|
|
|
) |
200
|
|
|
); |
201
|
|
|
return null; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return Factory::item($transformer, Json::decodeIfJson($response->getBody()->getContents())); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $email |
209
|
|
|
* @param callable|TransformerInterface $transformer |
210
|
|
|
* @param AuthenticationStrategyInterface|null $authenticationStrategy |
211
|
|
|
* @param CacheStrategyInterface|null $cacheStrategy |
212
|
|
|
* @return array|null |
213
|
|
|
*/ |
214
|
|
|
public function getByEmail( |
215
|
|
|
string $email, |
216
|
|
|
callable $transformer, |
217
|
|
|
AuthenticationStrategyInterface $authenticationStrategy = null, |
218
|
|
|
CacheStrategyInterface $cacheStrategy = null |
219
|
|
|
) { |
220
|
|
|
// Get contact |
221
|
|
|
$response = HubSpot::getInstance()->http()->contacts()->getByEmail( |
222
|
|
|
$email, |
223
|
|
|
$authenticationStrategy, |
224
|
|
|
$cacheStrategy |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
if ($response->getStatusCode() !== 200) { |
229
|
|
|
HubSpot::warning( |
230
|
|
|
sprintf( |
231
|
|
|
"Unable to get user with email: {email}", |
232
|
|
|
$email |
233
|
|
|
) |
234
|
|
|
); |
235
|
|
|
return null; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return Factory::item($transformer, Json::decodeIfJson($response->getBody()->getContents())); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.