Skip to content
This repository was archived by the owner on Jan 21, 2023. It is now read-only.

Commit 5386ec5

Browse files
committed
initial commit
1 parent 8a8604c commit 5386ec5

File tree

17 files changed

+558
-0
lines changed

17 files changed

+558
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Created by .ignore support plugin (hsz.mobi)

codeception.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
paths:
2+
tests: tests
3+
output: tests/_output
4+
data: tests/_data
5+
support: tests/_support
6+
envs: tests/_envs
7+
actor_suffix: Tester
8+
extensions:
9+
enabled:
10+
- Codeception\Extension\RunFailed

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "devlib/mailchimp-api",
3+
"description": "Simple MailChimp API Wrapper for PHP 7",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "adrian7",
9+
"email": "adrian.silimon@yahoo.com"
10+
}
11+
],
12+
"minimum-stability": "stable",
13+
"require": {
14+
"adrian7/mailchimp-api-v3": "^1.0"
15+
},
16+
"require-dev": {
17+
"codeception/codeception": "^2.3",
18+
"fzaninotto/faker": "^1.7"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"DevLib\\": "src/"
23+
}
24+
}
25+
}

src/API/MailChimp/MailChimp.php

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
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+
}

tests/_data/.gitkeep

Whitespace-only changes.

tests/_output/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
16+
*
17+
* @SuppressWarnings(PHPMD)
18+
*/
19+
class AcceptanceTester extends \Codeception\Actor
20+
{
21+
use _generated\AcceptanceTesterActions;
22+
23+
/**
24+
* Define custom actions here
25+
*/
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
16+
*
17+
* @SuppressWarnings(PHPMD)
18+
*/
19+
class FunctionalTester extends \Codeception\Actor
20+
{
21+
use _generated\FunctionalTesterActions;
22+
23+
/**
24+
* Define custom actions here
25+
*/
26+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Helper;
3+
4+
// here you can define custom actions
5+
// all public methods declared in helper class will be available in $I
6+
7+
class Acceptance extends \Codeception\Module
8+
{
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Helper;
3+
4+
// here you can define custom actions
5+
// all public methods declared in helper class will be available in $I
6+
7+
class Functional extends \Codeception\Module
8+
{
9+
10+
}

0 commit comments

Comments
 (0)