1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace flipbox\hubspot\modules\resources\services; |
4
|
|
|
|
5
|
|
|
use craft\helpers\Json; |
6
|
|
|
use flipbox\hubspot\authentication\AuthenticationStrategyInterface; |
7
|
|
|
use flipbox\hubspot\cache\CacheStrategyInterface; |
8
|
|
|
use flipbox\hubspot\HubSpot; |
9
|
|
|
use Flipbox\Transform\Factory; |
10
|
|
|
use Flipbox\Transform\Transformers\TransformerInterface; |
11
|
|
|
|
12
|
|
|
class ContactLists extends AbstractResource |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param int $id |
16
|
|
|
* @param callable|TransformerInterface $transformer |
17
|
|
|
* @param AuthenticationStrategyInterface|null $authenticationStrategy |
18
|
|
|
* @param CacheStrategyInterface|null $cacheStrategy |
19
|
|
|
* @return mixed|null |
20
|
|
|
*/ |
21
|
|
|
public function getById( |
22
|
|
|
int $id, |
23
|
|
|
callable $transformer, |
24
|
|
|
AuthenticationStrategyInterface $authenticationStrategy = null, |
25
|
|
|
CacheStrategyInterface $cacheStrategy = null |
26
|
|
|
) { |
27
|
|
|
// Get contact |
28
|
|
|
$response = HubSpot::getInstance()->http()->contactLists()->getById( |
29
|
|
|
$id, |
30
|
|
|
$authenticationStrategy, |
31
|
|
|
$cacheStrategy |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
if ($response->getStatusCode() !== 200) { |
35
|
|
|
HubSpot::warning( |
36
|
|
|
sprintf( |
37
|
|
|
"Unable to get contact list with id: %s", |
38
|
|
|
$id |
39
|
|
|
) |
40
|
|
|
); |
41
|
|
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return Factory::item($transformer, Json::decodeIfJson($response->getBody()->getContents())); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param int $id |
49
|
|
|
* @param callable|TransformerInterface $transformer |
50
|
|
|
* @param AuthenticationStrategyInterface|null $authenticationStrategy |
51
|
|
|
* @param CacheStrategyInterface|null $cacheStrategy |
52
|
|
|
* @return mixed|null |
53
|
|
|
*/ |
54
|
|
|
public function getContacts( |
55
|
|
|
int $id, |
56
|
|
|
callable $transformer, |
57
|
|
|
AuthenticationStrategyInterface $authenticationStrategy = null, |
58
|
|
|
CacheStrategyInterface $cacheStrategy = null |
59
|
|
|
) { |
60
|
|
|
// Get contact |
61
|
|
|
$response = HubSpot::getInstance()->http()->contactLists()->getContacts( |
62
|
|
|
$id, |
63
|
|
|
$authenticationStrategy, |
64
|
|
|
$cacheStrategy |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
if ($response->getStatusCode() !== 200) { |
68
|
|
|
$body = Json::decodeIfJson($response->getBody()->getContents()); |
69
|
|
|
HubSpot::warning( |
70
|
|
|
sprintf( |
71
|
|
|
"Unable to get contact list with id: %s, errors: %s", |
72
|
|
|
$id, |
73
|
|
|
Json::encode($body) |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return Factory::collection( |
80
|
|
|
$transformer, |
81
|
|
|
Json::decodeIfJson($response->getBody()->getContents()) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param int $id |
87
|
|
|
* @param array $vids |
88
|
|
|
* @param array $emails |
89
|
|
|
* @param AuthenticationStrategyInterface|null $authenticationStrategy |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function add( |
93
|
|
|
int $id, |
94
|
|
|
array $vids = [], |
95
|
|
|
array $emails = [], |
96
|
|
|
AuthenticationStrategyInterface $authenticationStrategy = null |
97
|
|
|
) { |
98
|
|
|
$response = HubSpot::getInstance()->http()->contactLists()->add( |
99
|
|
|
$id, |
100
|
|
|
$vids, |
101
|
|
|
$emails, |
102
|
|
|
$authenticationStrategy |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
if ($response->getStatusCode() !== 200) { |
106
|
|
|
$body = Json::decodeIfJson($response->getBody()->getContents()); |
107
|
|
|
HubSpot::warning( |
108
|
|
|
sprintf( |
109
|
|
|
"Unable to get contact list with id: %s, errors: %s", |
110
|
|
|
$id, |
111
|
|
|
Json::encode($body) |
112
|
|
|
) |
113
|
|
|
); |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param int $id |
122
|
|
|
* @param array $vids |
123
|
|
|
* @param array $emails |
124
|
|
|
* @param AuthenticationStrategyInterface|null $authenticationStrategy |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function remove( |
128
|
|
|
int $id, |
129
|
|
|
array $vids = [], |
130
|
|
|
array $emails = [], |
131
|
|
|
AuthenticationStrategyInterface $authenticationStrategy = null |
132
|
|
|
) { |
133
|
|
|
$response = HubSpot::getInstance()->http()->contactLists()->remove( |
134
|
|
|
$id, |
135
|
|
|
$vids, |
136
|
|
|
$emails, |
137
|
|
|
$authenticationStrategy |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
if ($response->getStatusCode() !== 200) { |
141
|
|
|
$body = Json::decodeIfJson($response->getBody()->getContents()); |
142
|
|
|
HubSpot::warning( |
143
|
|
|
sprintf( |
144
|
|
|
"Unable to get contact list with id: %s, errors: %s", |
145
|
|
|
$id, |
146
|
|
|
Json::encode($body) |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|