1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MailMarketing\Drivers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use MailMarketing\Entities\CampaignMonitorResponse; |
7
|
|
|
use MailMarketing\Entities\ResponseInterface; |
8
|
|
|
use MailMarketing\MailMarketingException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @see https://www.campaignmonitor.com/api/ |
12
|
|
|
*/ |
13
|
|
|
class CampaignMonitor implements MailMarketingInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected array $auth; |
17
|
|
|
protected array $config; |
18
|
|
|
|
19
|
2 |
|
public function __construct(array $config) |
20
|
|
|
{ |
21
|
2 |
|
if (!($config['key'] ?? false)) { |
22
|
1 |
|
throw new \InvalidArgumentException('CampaignMonitor API key not present'); |
23
|
|
|
} |
24
|
|
|
|
25
|
1 |
|
$this->auth = ['api_key' => $config['key']]; |
26
|
1 |
|
unset($config['key']); |
27
|
|
|
|
28
|
1 |
|
$this->config = $config; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string|null $type |
34
|
|
|
* @return \CS_REST_Wrapper_Base|\CS_REST_Lists|\CS_REST_Subscribers |
35
|
|
|
*/ |
36
|
1 |
|
public function makeClient(?string $type = null): \CS_REST_Wrapper_Base |
37
|
|
|
{ |
38
|
1 |
|
return match ($type) { |
39
|
1 |
|
'lists' => new \CS_REST_Lists('', $this->auth), |
40
|
|
|
'subscribers' => new \CS_REST_Subscribers('', $this->auth), |
41
|
1 |
|
default => throw new \InvalidArgumentException("Client type [{$type}] is invalid.") |
42
|
1 |
|
}; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
1 |
|
public function client(?string $type = null): mixed |
49
|
|
|
{ |
50
|
1 |
|
return $this->makeClient($type); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
1 |
|
public function addList(string $name, array $data = []): ResponseInterface |
57
|
|
|
{ |
58
|
1 |
|
$client = Arr::get($data, 'client_id') ?: Arr::get($this->config, 'client_id'); |
59
|
1 |
|
if (!$client) { |
60
|
|
|
throw new \InvalidArgumentException('Client is required.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$listConfig = Arr::get($this->config, 'list', []); |
64
|
1 |
|
if (!is_array($listConfig)) { |
65
|
|
|
$listConfig = []; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$config = array_merge($listConfig, $data, ['Title' => $name]); |
69
|
|
|
|
70
|
|
|
|
71
|
1 |
|
return CampaignMonitorResponse::init($this->client('lists')->create($client, $config)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function addMembersToList($listId, $members, array $data = []): ResponseInterface |
75
|
|
|
{ |
76
|
|
|
$subscribers = []; |
77
|
|
|
foreach ($members as $member) { |
78
|
|
|
$subscribers[] = [ |
79
|
|
|
'EmailAddress' => Arr::get($member, 'email', ''), |
80
|
|
|
'Name' => implode(' ', array_filter([ |
81
|
|
|
Arr::get($member, 'first_name', ''), |
82
|
|
|
Arr::get($member, 'last_name', ''), |
83
|
|
|
])), |
84
|
|
|
'MobileNumber' => Arr::get($member, 'phone', ''), |
85
|
|
|
'CustomFields' => [ |
86
|
|
|
[ |
87
|
|
|
'Key' => 'address', |
88
|
|
|
'Value' => Arr::get($member, 'address', ''), |
89
|
|
|
'Clear' => !Arr::get($member, 'address', ''), |
90
|
|
|
], |
91
|
|
|
], |
92
|
|
|
'ConsentToTrack' => 'Yes', |
93
|
|
|
'ConsentToSendSms' => 'Yes', |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$client = $this->client('subscribers'); |
98
|
|
|
$client->set_list_id($listId); |
99
|
|
|
|
100
|
|
|
return CampaignMonitorResponse::init($client->import($subscribers, Arr::get($data, 'resubscribe', true))); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function addMemberToList($listId, $member, array $data = []): ResponseInterface |
104
|
|
|
{ |
105
|
|
|
$email = Arr::get($member, 'email'); |
106
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
107
|
|
|
throw new MailMarketingException("Email '{$email}' not valid"); |
108
|
|
|
} |
109
|
|
|
$customFields = []; |
110
|
|
|
$customFieldsData = Arr::get($member, 'custom_fields', null); |
111
|
|
|
if (is_array($customFieldsData) && !empty($customFieldsData)) { |
112
|
|
|
foreach ($customFieldsData as $key => $value) { |
113
|
|
|
$customFields[] = [ |
114
|
|
|
'Key' => $key, |
115
|
|
|
'Value' => $value, |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
$subscriber = [ |
120
|
|
|
'EmailAddress' => $email, |
121
|
|
|
'Name' => implode(' ', array_filter([ |
122
|
|
|
Arr::get($member, 'first_name', ''), |
123
|
|
|
Arr::get($member, 'last_name', ''), |
124
|
|
|
])), |
125
|
|
|
'MobileNumber' => Arr::get($member, 'phone', ''), |
126
|
|
|
'CustomFields' => [ |
127
|
|
|
[ |
128
|
|
|
'Key' => 'address', |
129
|
|
|
'Value' => Arr::get($member, 'address', ''), |
130
|
|
|
'Clear' => !Arr::get($member, 'address', ''), |
131
|
|
|
], |
132
|
|
|
...$customFields, |
133
|
|
|
], |
134
|
|
|
'Resubscribe' => Arr::get($data, 'resubscribe', true), |
135
|
|
|
'ConsentToTrack' => 'Yes', |
136
|
|
|
'ConsentToSendSms' => 'Yes', |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$client = $this->client('subscribers'); |
140
|
|
|
$client->set_list_id($listId); |
141
|
|
|
|
142
|
|
|
return CampaignMonitorResponse::init($client->add($subscriber)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function removeMemberFromList($listId, $member, array $data = []): ResponseInterface |
146
|
|
|
{ |
147
|
|
|
$email = Arr::get($member, 'email'); |
148
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
149
|
|
|
throw new MailMarketingException("Email '{$email}' not valid"); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$client = $this->client('subscribers'); |
153
|
|
|
$client->set_list_id($listId); |
154
|
|
|
|
155
|
|
|
return CampaignMonitorResponse::init($client->delete($email)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getMemberInfoForList($listId, string $email): ResponseInterface |
159
|
|
|
{ |
160
|
|
|
$client = $this->client('subscribers'); |
161
|
|
|
$client->set_list_id($listId); |
162
|
|
|
|
163
|
|
|
return CampaignMonitorResponse::init($client->get($email)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function manageMemberTags($listId, string $email, array $tags): ResponseInterface |
167
|
|
|
{ |
168
|
|
|
throw new MailMarketingException('CampaignMonitor not support tags'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getMemberTags($listId, string $email): ResponseInterface |
172
|
|
|
{ |
173
|
|
|
throw new MailMarketingException('CampaignMonitor not support tags'); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|