1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Easychimp; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Mailchimp\Mailchimp; |
7
|
|
|
|
8
|
|
|
class MailingList |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** @var Mailchimp */ |
12
|
|
|
protected $api; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $id; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $support; |
19
|
|
|
|
20
|
|
|
public function __construct(Mailchimp $api, Support $support, string $id) |
21
|
|
|
{ |
22
|
|
|
$this->api = $api; |
23
|
|
|
$this->support = $support; |
|
|
|
|
24
|
|
|
$this->id = $id; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function id() : string |
28
|
|
|
{ |
29
|
|
|
return $this->id; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
|
|
public function exists() : bool |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
|
|
$this->api->get('lists/'.$this->id()); |
39
|
|
|
} catch (\Exception $e) { |
40
|
|
|
if (starts_with($e->getMessage(), '{')) { |
41
|
|
|
$json = json_decode($e->getMessage()); |
42
|
|
|
if ($json->status == 404) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
throw $e; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function isOnList(string $email) : bool |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
try { |
59
|
|
|
$this->api->get('lists/'.$this->id().'/members/'.$this->support->hashEmail($email)); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
# Email address isn't on this list |
64
|
|
|
if (str_contains($e->getMessage(), 'Resource Not Found')) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
throw $e; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $email |
74
|
|
|
* @param string $firstName |
75
|
|
|
* @param string $lastName |
76
|
|
|
* @param array|object $interests Properties/Keys are interest ids and values are boolean |
77
|
|
|
* @param array $extras Additional fields to be passed to the Mailchimp API |
78
|
|
|
* |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
|
|
public function subscribe( |
82
|
|
|
string $email, |
83
|
|
|
string $firstName = null, |
84
|
|
|
string $lastName = null, |
85
|
|
|
$interests = null, |
86
|
|
|
array $extras = [] |
87
|
|
|
) : bool { |
88
|
|
|
$mergeFields = []; |
89
|
|
|
if ($firstName !== null) { |
90
|
|
|
$mergeFields['FNAME'] = $firstName; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($lastName !== null) { |
94
|
|
|
$mergeFields['LNAME'] = $lastName; |
95
|
|
|
} |
96
|
|
|
$data = array_merge([ |
97
|
|
|
'email_address' => $email, |
98
|
|
|
'status' => 'subscribed', |
99
|
|
|
'merge_fields' => (object) $mergeFields |
100
|
|
|
], $extras); |
101
|
|
|
|
102
|
|
|
if ($interests !== null) { |
103
|
|
|
$data['interests'] = (object) $interests; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$result = $this->api->post('lists/'.$this->id().'/members', $data); |
107
|
|
|
|
108
|
|
|
return $result->has('id') && strlen($result->get('id')) > 0; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @link http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members_subscriber_hash |
113
|
|
|
* |
114
|
|
|
* @throws EmailAddressNotSubscribed |
115
|
|
|
* @throws \Exception |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function subscriberInfo(string $email) : Collection |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
try { |
120
|
|
|
return $this->api->get('lists/'.$this->id().'/members/'.$this->support->hashEmail($email)); |
|
|
|
|
121
|
|
|
} catch (\Exception $e) { |
122
|
|
|
# Email address isn't on this list |
123
|
|
|
if (str_contains($e->getMessage(), 'Resource Not Found')) { |
124
|
|
|
throw new EmailAddressNotSubscribed; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
throw $e; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Updates a subscriber if the email address is already |
133
|
|
|
* on the list, or create the subscriber |
134
|
|
|
* |
135
|
|
|
* @param string $email |
136
|
|
|
* @param string $firstName |
137
|
|
|
* @param string $lastName |
138
|
|
|
* @param array|object $interests Properties/Keys are interest ids and values are boolean |
139
|
|
|
* @param array $extras Additional fields to be passed to the Mailchimp API |
140
|
|
|
* |
141
|
|
|
* @link https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-put_lists_list_id_members_subscriber_hash |
142
|
|
|
* |
143
|
|
|
* @throws EmailAddressNotSubscribed |
144
|
|
|
* @throws \Exception |
145
|
|
|
*/ |
146
|
|
|
public function updateSubscriber( |
147
|
|
|
string $email, |
148
|
|
|
string $firstName = null, |
149
|
|
|
string $lastName = null, |
150
|
|
|
$interests = null, |
151
|
|
|
array $extras = [] |
152
|
|
|
) : bool { |
153
|
|
|
$data = array_merge([ |
154
|
|
|
'status_if_new' => 'subscribed', |
155
|
|
|
'email_address' => $email |
156
|
|
|
], $extras); |
157
|
|
|
$mergeFields = []; |
158
|
|
|
if ($firstName !== null) { |
159
|
|
|
$mergeFields['FNAME'] = $firstName; |
160
|
|
|
} |
161
|
|
|
if ($lastName !== null) { |
162
|
|
|
$mergeFields['LNAME'] = $lastName; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (count($mergeFields) > 0) { |
166
|
|
|
$data['merge_fields'] = (object) $mergeFields; |
167
|
|
|
} |
168
|
|
|
if ($interests !== null) { |
169
|
|
|
$data['interests'] = (object) $interests; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$result = $this->api->put('lists/'.$this->id().'/members/'.$this->support->hashEmail($email), $data); |
|
|
|
|
173
|
|
|
|
174
|
|
|
return $result->has('id') && strlen($result->get('id')) > 0; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @throws EmailAddressNotSubscribed |
179
|
|
|
* @throws \Exception |
180
|
|
|
*/ |
181
|
|
|
public function unsubscribe(string $email) : bool |
182
|
|
|
{ |
183
|
|
|
try { |
184
|
|
|
$result = $this->api->patch('lists/'.$this->id().'/members/'.$this->support->hashEmail($email), [ |
|
|
|
|
185
|
|
|
'status' => 'unsubscribed' |
186
|
|
|
]); |
187
|
|
|
|
188
|
|
|
return $result->has('id') && strlen($result->get('id')) > 0; |
189
|
|
|
} catch (\Exception $e) { |
190
|
|
|
# Email address isn't on this list |
191
|
|
|
if (str_contains($e->getMessage(), 'Resource Not Found')) { |
192
|
|
|
throw new EmailAddressNotSubscribed; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
throw $e; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @throws \Exception |
201
|
|
|
*/ |
202
|
|
|
public function interestCategories() : array |
203
|
|
|
{ |
204
|
|
|
$result = $this->api->get('lists/'.$this->id().'/interest-categories'); |
205
|
|
|
|
206
|
|
|
return $result->get('categories'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @throws \Exception |
211
|
|
|
*/ |
212
|
|
|
public function interests(string $interestCategoryId) : array |
213
|
|
|
{ |
214
|
|
|
$result = $this->api->get('lists/'.$this->id().'/interest-categories/'.$interestCategoryId.'/interests'); |
215
|
|
|
|
216
|
|
|
return $result->get('interests'); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..