1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Easychimp; |
4
|
|
|
|
5
|
|
|
use Mailchimp\Mailchimp; |
6
|
|
|
|
7
|
|
|
class Easychimp |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** @var Mailchimp */ |
11
|
|
|
protected $api; |
12
|
|
|
|
13
|
|
|
public function __construct($apiKey) |
14
|
|
|
{ |
15
|
|
|
$this->api = new Mailchimp($apiKey); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param $listId |
20
|
|
|
* @param $email |
21
|
|
|
* |
22
|
|
|
* @throws \Exception |
23
|
|
|
* |
24
|
|
|
* @return boolean |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
public function isSubscribed($listId, $email) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
try { |
29
|
|
|
$result = $this->api->get('lists/'.$listId.'/members/'.$this->hashEmail($email)); |
30
|
|
|
|
31
|
|
|
return $result->get('status') == 'subscribed'; |
32
|
|
|
} catch (\Exception $e) { |
33
|
|
|
# Email address isn't on this list |
34
|
|
|
if (str_contains($e->getMessage(), 'Resource Not Found')) { |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
throw $e; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $listId |
44
|
|
|
* @param $email |
45
|
|
|
* @param array $extras |
|
|
|
|
46
|
|
|
* |
47
|
|
|
* @throws \Exception |
48
|
|
|
* |
49
|
|
|
* @return boolean |
50
|
|
|
*/ |
51
|
|
|
public function subscribe( |
52
|
|
|
$listId, |
53
|
|
|
$email, |
54
|
|
|
$firstName = null, |
55
|
|
|
$lastName = null, |
56
|
|
|
array $interests = null |
57
|
|
|
) { |
58
|
|
|
$mergeFields = []; |
59
|
|
|
if (!is_null($firstName)) { |
|
|
|
|
60
|
|
|
$mergeFields['FNAME'] = $firstName; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!is_null($lastName)) { |
|
|
|
|
64
|
|
|
$mergeFields['LNAME'] = $lastName; |
65
|
|
|
} |
66
|
|
|
$data = [ |
67
|
|
|
'email_address' => $email, |
68
|
|
|
'status' => 'subscribed', |
69
|
|
|
'merge_fields' => (object) $mergeFields |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
if (!is_null($interests)) { |
|
|
|
|
73
|
|
|
$data['interests'] = (object) array_flip($interests); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$result = $this->api->post('lists/'.$listId.'/members', $data); |
77
|
|
|
|
78
|
|
|
return $result->has('id') && strlen($result->get('id')) > 0; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $listId |
83
|
|
|
* @param $email |
84
|
|
|
* |
85
|
|
|
* @throws \Exception |
86
|
|
|
* |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function unsubscribe($listId, $email) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
$result = $this->api->delete('lists/'.$listId.'/members/'.$this->hashEmail($email)); |
93
|
|
|
|
94
|
|
|
return $result->count() == 0; |
95
|
|
|
} catch (\Exception $e) { |
96
|
|
|
# Email address isn't on this list |
97
|
|
|
if (str_contains($e->getMessage(), 'Resource Not Found')) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
throw $e; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $listId |
107
|
|
|
* |
108
|
|
|
* @throws \Exception |
109
|
|
|
* |
110
|
|
|
* @return [] |
|
|
|
|
111
|
|
|
*/ |
112
|
|
|
public function interestCategories($listId) |
113
|
|
|
{ |
114
|
|
|
$result = $this->api->get('lists/'.$listId.'/interest-categories'); |
115
|
|
|
|
116
|
|
|
return $result->get('categories'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $listId |
121
|
|
|
* @param $interestCategoryId |
122
|
|
|
* |
123
|
|
|
* @throws \Exception |
124
|
|
|
* |
125
|
|
|
* @return [] |
|
|
|
|
126
|
|
|
*/ |
127
|
|
|
public function interests($listId, $interestCategoryId) |
128
|
|
|
{ |
129
|
|
|
$result = $this->api->get('lists/'.$listId.'/interest-categories/'.$interestCategoryId.'/interests'); |
130
|
|
|
|
131
|
|
|
return $result->get('interests'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $email |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
protected function hashEmail($email) |
139
|
|
|
{ |
140
|
|
|
return md5(strtolower($email)); |
141
|
|
|
} |
142
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.