1+ <?php
2+ /**
3+ * SMS Signin Gateway - Mailchimp API integration class
4+ * @author adrian7 (adrian@studentmoneysaver.co.uk)
5+ * @version 1.1
6+ */
7+
8+ namespace DevLib \API \MailChimp ;
9+
10+ use MailChimp \MailChimpAPI ;
11+ use MailChimp \MailChimpAPIException ;
12+
13+ class MailChimp{
14+
15+ const LIST_MEMBERS_RESOURCE_URI = '/lists/<list_id>/members/ ' ;
16+ const LIST_SINGLE_MEMBER_RESOURCE_URI = '/lists/<list_id>/members/<id> ' ;
17+
18+ const STATUS_SUBSCRIBED = 'subscribed ' ;
19+ const STATUS_UNSUBSCRIBED = 'unsubscribed ' ;
20+
21+ /**
22+ * MailChimp api wrapper
23+ * @var null|MailChimpAPI
24+ */
25+ protected $ wrapper = NULL ;
26+
27+ /**
28+ * Default MailChimp list id
29+ * @var null|string
30+ */
31+ protected $ default_list_id = NULL ;
32+
33+ /**
34+ * MailChimp constructor.
35+ *
36+ * @param null $api_key
37+ * @param null $default_list_id
38+ */
39+ public function __construct ($ api_key , $ default_list_id =NULL ){
40+
41+ //TODO improve implementation
42+ $ this ->default_list_id = $ default_list_id ?: NULL ;
43+
44+ $ this ->wrapper = new MailChimpAPI ($ api_key );
45+
46+ }
47+
48+ /**
49+ * Retrieve list member identified by email
50+ * @param null|string $list_id
51+ * @param string $email
52+ *
53+ * @return mixed
54+ */
55+ public function member ($ email , $ list_id =NULL ){
56+
57+ $ list_id = $ list_id ?: $ this ->default_list_id ;
58+
59+ $ uri = self ::getResourceURI (self ::LIST_SINGLE_MEMBER_RESOURCE_URI , [
60+ 'list_id ' => $ list_id ,
61+ 'id ' => md5 ( $ email )
62+ ]);
63+
64+
65+ return $ this ->wrapper ->get ( $ uri );
66+
67+ }
68+
69+ /**
70+ * Subscribes/updates member identified by email to list
71+ *
72+ * @param $email
73+ * @param array $merge
74+ * @param null $list_id
75+ * @param bool $double_optin
76+ *
77+ * @return bool|mixed
78+ * @throws MailChimpAPIException
79+ */
80+ public function subscribe ($ email , $ merge =[], $ list_id =NULL , $ double_optin =FALSE ){
81+
82+ $ list_id = $ list_id ?: $ this ->default_list_id ;
83+ $ merge = empty ($ merge ) ? new \stdClass () : $ this ->sanitizeMergeFields ($ merge );
84+
85+ //update existing member
86+ try {
87+
88+ $ uri = $ this ->getResourceURI (self ::LIST_SINGLE_MEMBER_RESOURCE_URI , [
89+ 'list_id ' => $ list_id ,
90+ 'id ' => md5 ( $ email )
91+ ]);
92+
93+ $ member = $ this ->member ($ email , $ list_id );
94+
95+ if ( $ member )
96+ //member already subscribed, do update
97+ return $ this ->wrapper ->put ($ uri , [
98+ 'status ' => self ::STATUS_SUBSCRIBED ,
99+ 'email_address ' => $ email ,
100+ 'double_optin ' => $ double_optin ,
101+ 'merge_fields ' => $ merge
102+ ]);
103+
104+ }catch (MailChimpAPIException $ e ){
105+
106+ if ( 404 != $ e ->getStatusCode () )
107+ throw $ e ; //some other error
108+
109+ }
110+
111+ //submit a new member
112+ try {
113+
114+ $ uri = $ this ->getResourceURI (self ::LIST_MEMBERS_RESOURCE_URI , [
115+ 'list_id ' => $ list_id ,
116+ ]);
117+
118+ return $ this ->wrapper ->post ($ uri , [
119+ 'status ' => self ::STATUS_SUBSCRIBED ,
120+ 'email_address ' => $ email ,
121+ 'double_optin ' => $ double_optin ,
122+ 'merge_fields ' => $ merge
123+ ]);
124+
125+ }
126+ catch (MailChimpAPIException $ e ){
127+ throw $ e ; //could not subscribe member
128+ }
129+
130+ }
131+
132+ public function unsubscribe ($ email , $ list_id =NULL ){
133+
134+ $ list_id = empty ($ list_id ) ? $ this ->default_list_id : NULL ;
135+
136+ try {
137+
138+ $ uri = $ this ->getResourceURI (self ::LIST_SINGLE_MEMBER_RESOURCE_URI , [
139+ 'list_id ' => $ list_id ,
140+ 'id ' => md5 ( $ email )
141+ ]);
142+
143+ //try update
144+ return $ this ->wrapper ->put ($ uri , [
145+ 'status ' => self ::STATUS_UNSUBSCRIBED ,
146+ 'email_address ' => $ email
147+ ]);
148+
149+
150+ }
151+ catch (MailChimpAPIException $ e ){
152+
153+ if ( 404 != $ e ->getStatusCode () )
154+ throw $ e ;
155+
156+ }
157+
158+ }
159+
160+ /**
161+ * Updates member
162+ * @param $email
163+ * @param array $merge
164+ * @param null $list_id
165+ *
166+ * @return bool|mixed
167+ */
168+ public function update ($ email , $ merge =[], $ list_id =NULL ){
169+
170+ $ list_id = empty ($ list_id ) ? $ this ->default_list_id : NULL ;
171+ $ merge = empty ($ merge ) ? new \stdClass () : $ this ->sanitizeMergeFields ($ merge );
172+
173+ $ uri = $ this ->getResourceURI (self ::LIST_SINGLE_MEMBER_RESOURCE_URI , [
174+ 'list_id ' => $ list_id ,
175+ 'id ' => md5 ( $ email )
176+ ]);
177+
178+ //try update
179+ return $ this ->wrapper ->put ($ uri , [
180+ 'email_address ' => $ email ,
181+ 'merge_fields ' => $ merge
182+ ]);
183+
184+ }
185+
186+ /**
187+ * Delete list member
188+ * @param $email
189+ * @param null $list_id
190+ *
191+ * @return mixed
192+ * @throws MailChimpAPIException
193+ */
194+ public function delete ($ email , $ list_id =NULL ){
195+
196+ $ list_id = empty ($ list_id ) ? $ this ->default_list_id : $ list_id ;
197+
198+ try {
199+
200+ $ member = $ this ->member ($ email , $ list_id );
201+
202+ if ( $ member ) { //remove
203+
204+ $ uri = $ this ->getResourceURI (self ::LIST_SINGLE_MEMBER_RESOURCE_URI , [
205+ 'list_id ' => $ list_id ,
206+ 'id ' => md5 ( $ email )
207+ ]);
208+
209+ return $ this ->wrapper ->delete ($ uri );
210+
211+ }
212+
213+ }
214+ catch (MailChimpAPIException $ e ){
215+
216+ if ( 404 != $ e ->getStatusCode () )
217+ throw $ e ;
218+
219+ }
220+
221+ }
222+
223+ /**
224+ * Removes empty values from merge fields
225+ * @param array $fields
226+ * @return array
227+ */
228+ protected function sanitizeMergeFields ($ fields ){
229+
230+ return array_filter ($ fields , function ($ value ){
231+ return ! empty ($ value );
232+ });
233+
234+ }
235+
236+ /**
237+ * Given a template generates a resource uri
238+ * @param $template
239+ * @param array $params
240+ *
241+ * @return mixed
242+ */
243+ protected function getResourceURI ($ template , $ params =[]){
244+
245+ if ( count ($ params ) )
246+ foreach ($ params as $ key =>$ value )
247+ $ template = str_replace ("< {$ key }> " , $ value , $ template );
248+
249+ return $ template ;
250+ }
251+ }
0 commit comments