/
MailChimpSubscriberGateway.php
238 lines (204 loc) · 6.31 KB
/
MailChimpSubscriberGateway.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
namespace MailMotor\Bundle\MailChimpBundle\Gateway;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Collection;
use Mailchimp\Mailchimp;
use MailMotor\Bundle\MailMotorBundle\Gateway\SubscriberGateway;
/**
* MailChimp Subscriber Gateway
*
* @author Jeroen Desloovere <info@jeroendesloovere.be>
*/
class MailChimpSubscriberGateway implements SubscriberGateway
{
/**
* The external MailChimp API
*
* @var Mailchimp
*/
protected $api;
public function __construct(Mailchimp $api)
{
$this->api = $api;
}
public function exists(string $email, string $listId): bool
{
try {
/** @var array $result */
$result = $this->get(
$email,
$listId
);
return (!empty($result));
} catch (\Exception $e) {
return false;
}
}
/**
* Get a subscriber
*
* @param string $email
* @param string $listId
* @return array
*/
private function get(string $email, string $listId): array
{
try {
/** @var Collection $result */
$result = $this->api->request(
'lists/' . $listId . '/members/' . $this->getHashedEmail($email),
array(),
'get'
);
// will return the one and only member array('id', ...) from Collection
return $result->all();
} catch (\Exception $e) {
return [];
}
}
public function getInterests(string $listId): array
{
try {
/** @var Collection $result */
$interestCategories = $this->api->request(
'lists/' . $listId . '/interest-categories',
array(RequestOptions::TIMEOUT => 5),
'get'
);
// Init $interests
$interests = array();
// Loop all interest categories
foreach ($interestCategories->all()['categories'] as $interestCategory) {
// Define interestCategoryItems
$interestCategoryItems = $this->getInterestsForCategoryId(
$interestCategory->id,
$listId
);
// Init children
$children = array();
// Loop interests
foreach ($interestCategoryItems['interests'] as $interestCategoryItem) {
// Add child to interestCategory children
$children[$interestCategoryItem->id] = $interestCategoryItem->name;
}
// Add interestCategory to interests
$interests[$interestCategory->id] = [
'title' => $interestCategory->title,
'children' => $children,
];
}
return $interests;
} catch (\Exception $e) {
return [];
}
}
protected function getInterestsForCategoryId(string $interestCategoryId, string $listId): array
{
try {
/** @var Collection $result */
$result = $this->api->request(
'lists/' . $listId . '/interest-categories/' . $interestCategoryId . '/interests',
array(),
'get'
);
// will return the one and only member array('id', ...) from Collection
return $result->all();
} catch (\Exception $e) {
return [];
}
}
public function hasStatus(string $email, string $listId, string $status): bool
{
$member = $this->get(
$email,
$listId
);
// we have found a member
if (is_array($member) && !empty($member) && array_key_exists('status', $member)) {
return ($member['status'] === $status);
}
// we don't have a member
return false;
}
public function ping(string $listId): bool
{
try {
return $this->api->get('/lists/' . $listId) instanceof Collection;
} catch (\Exception $e) {
return false;
}
}
/**
* Subscribe
*
* @param string $email
* @param string $listId
* @param string $language
* @param array $mergeFields
* @param array $interests The array is like: ['9AS489SQF' => true, '4SDF8S9DF1' => false]
* @param bool $doubleOptin Members need to validate their emailAddress before they get added to the list
* @return boolean
*/
public function subscribe(
string $email,
string $listId,
string $language,
array $mergeFields,
array $interests,
bool $doubleOptin
): bool {
// default status
$status = 'subscribed';
// redefine to pending
if ($doubleOptin) {
$status = 'pending';
}
// init parameters
$parameters = array(
'email_address' => $email,
'status' => $status,
);
// we received a language
if ($language !== null) {
// add language to parameters
$parameters['language'] = $language;
}
// we received merge fields
if (!empty($mergeFields)) {
// add merge fields to parameters
$parameters['merge_fields'] = $mergeFields;
}
// we received interests
if (!empty($interests)) {
// Init interest object
$interestsObject = new \stdClass();
// Loop interests
foreach ($interests as $id => $value) {
$interestsObject->{$id} = (bool) $value;
}
// Add interests to parameters
$parameters['interests'] = $interestsObject;
}
$this->api->request(
'lists/' . $listId . '/members/' . $this->getHashedEmail($email),
$parameters,
'put'
);
return true;
}
public function unsubscribe(string $email, string $listId): bool
{
$this->api->request(
'lists/' . $listId . '/members/' . $this->getHashedEmail($email),
array(
'status' => 'unsubscribed',
),
'patch'
);
return true;
}
protected function getHashedEmail($email): string
{
return md5(strtolower($email));
}
}