|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright (C) 2013 Mailgun |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailgun\Api; |
|
13
|
|
|
|
|
14
|
|
|
use Mailgun\Api\MailingList\Member; |
|
15
|
|
|
use Mailgun\Assert; |
|
16
|
|
|
use Mailgun\Model\EmailValidation\ValidateResponse; |
|
17
|
|
|
use Mailgun\Model\MailingList\CreateResponse; |
|
18
|
|
|
use Mailgun\Model\MailingList\DeleteResponse; |
|
19
|
|
|
use Mailgun\Model\MailingList\PagesResponse; |
|
20
|
|
|
use Mailgun\Model\MailingList\ShowResponse; |
|
21
|
|
|
use Mailgun\Model\MailingList\UpdateResponse; |
|
22
|
|
|
use Mailgun\Model\MailingList\ValidationCancelResponse; |
|
23
|
|
|
use Mailgun\Model\MailingList\ValidationStatusResponse; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @see https://documentation.mailgun.com/en/latest/api-mailinglists.html |
|
27
|
|
|
*/ |
|
28
|
|
|
class MailingList extends HttpApi |
|
29
|
|
|
{ |
|
30
|
|
|
public function member(): Member |
|
31
|
|
|
{ |
|
32
|
|
|
return new Member($this->httpClient, $this->requestBuilder, $this->hydrator); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns a paginated list of mailing lists on the domain. |
|
37
|
|
|
* |
|
38
|
|
|
* @param int $limit Maximum number of records to return (optional: 100 by default) |
|
39
|
|
|
* |
|
40
|
|
|
* @return PagesResponse |
|
41
|
|
|
* |
|
42
|
|
|
* @throws \Exception |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function pages(int $limit = 100) |
|
45
|
|
|
{ |
|
46
|
2 |
|
Assert::range($limit, 1, 1000); |
|
47
|
|
|
|
|
48
|
|
|
$params = [ |
|
49
|
1 |
|
'limit' => $limit, |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
1 |
|
$response = $this->httpGet('/v3/lists/pages', $params); |
|
53
|
|
|
|
|
54
|
1 |
|
return $this->hydrateResponse($response, PagesResponse::class); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Creates a new mailing list on the current domain. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $address Address for the new mailing list |
|
61
|
|
|
* @param string $name Name for the new mailing list (optional) |
|
62
|
|
|
* @param string $description Description for the new mailing list (optional) |
|
63
|
|
|
* @param string $accessLevel List access level, one of: readonly (default), members, everyone |
|
64
|
|
|
* |
|
65
|
|
|
* @return CreateResponse |
|
66
|
|
|
* |
|
67
|
|
|
* @throws \Exception |
|
68
|
|
|
*/ |
|
69
|
3 |
|
public function create(string $address, string $name = null, string $description = null, string $accessLevel = 'readonly', string $replyPreference = 'list') |
|
70
|
|
|
{ |
|
71
|
3 |
|
Assert::stringNotEmpty($address); |
|
72
|
1 |
|
Assert::nullOrStringNotEmpty($name); |
|
|
|
|
|
|
73
|
1 |
|
Assert::nullOrStringNotEmpty($description); |
|
|
|
|
|
|
74
|
1 |
|
Assert::oneOf($accessLevel, ['readonly', 'members', 'everyone']); |
|
75
|
1 |
|
Assert::oneOf($replyPreference, ['list', 'sender']); |
|
76
|
|
|
|
|
77
|
|
|
$params = [ |
|
78
|
1 |
|
'address' => $address, |
|
79
|
1 |
|
'name' => $name, |
|
80
|
1 |
|
'description' => $description, |
|
81
|
1 |
|
'access_level' => $accessLevel, |
|
82
|
1 |
|
'reply_preference' => $replyPreference, |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
1 |
|
$response = $this->httpPost('/v3/lists', $params); |
|
86
|
|
|
|
|
87
|
1 |
|
return $this->hydrateResponse($response, CreateResponse::class); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns a single mailing list. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $address Address of the mailing list |
|
94
|
|
|
* |
|
95
|
|
|
* @return ShowResponse |
|
96
|
|
|
* |
|
97
|
|
|
* @throws \Exception |
|
98
|
|
|
*/ |
|
99
|
2 |
View Code Duplication |
public function show(string $address) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
2 |
|
Assert::stringNotEmpty($address); |
|
102
|
|
|
|
|
103
|
1 |
|
$response = $this->httpGet(sprintf('/v3/lists/%s', $address)); |
|
104
|
|
|
|
|
105
|
1 |
|
return $this->hydrateResponse($response, ShowResponse::class); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Updates a mailing list. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $address Address of the mailing list |
|
112
|
|
|
* @param array $parameters Array of field => value pairs to update |
|
113
|
|
|
* |
|
114
|
|
|
* @return UpdateResponse |
|
115
|
|
|
* |
|
116
|
|
|
* @throws \Exception |
|
117
|
|
|
*/ |
|
118
|
2 |
|
public function update(string $address, array $parameters = []) |
|
119
|
|
|
{ |
|
120
|
2 |
|
Assert::stringNotEmpty($address); |
|
121
|
2 |
|
Assert::isArray($parameters); |
|
122
|
|
|
|
|
123
|
2 |
|
foreach ($parameters as $field => $value) { |
|
124
|
|
|
switch ($field) { |
|
125
|
2 |
|
case 'address': |
|
126
|
2 |
|
case 'name': |
|
127
|
2 |
|
case 'description': |
|
128
|
1 |
|
Assert::stringNotEmpty($value); |
|
129
|
|
|
|
|
130
|
1 |
|
break; |
|
131
|
1 |
|
case 'access_level': |
|
132
|
1 |
|
Assert::oneOf($value, ['readonly', 'members', 'everyone']); |
|
133
|
|
|
|
|
134
|
1 |
|
break; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
$response = $this->httpPut(sprintf('/v3/lists/%s', $address), $parameters); |
|
139
|
|
|
|
|
140
|
1 |
|
return $this->hydrateResponse($response, UpdateResponse::class); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Removes a mailing list from the domain. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $address Address of the mailing list |
|
147
|
|
|
* |
|
148
|
|
|
* @return DeleteResponse |
|
149
|
|
|
* |
|
150
|
|
|
* @throws \Exception |
|
151
|
|
|
*/ |
|
152
|
1 |
|
public function delete(string $address) |
|
153
|
|
|
{ |
|
154
|
1 |
|
Assert::stringNotEmpty($address); |
|
155
|
|
|
|
|
156
|
1 |
|
$response = $this->httpDelete(sprintf('/v3/lists/%s', $address)); |
|
157
|
|
|
|
|
158
|
1 |
|
return $this->hydrateResponse($response, DeleteResponse::class); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Validates mailing list. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $address Address of the mailing list |
|
165
|
|
|
* |
|
166
|
|
|
* @return ValidateResponse |
|
167
|
|
|
* |
|
168
|
|
|
* @throws \Exception |
|
169
|
|
|
*/ |
|
170
|
1 |
|
public function validate(string $address) |
|
171
|
|
|
{ |
|
172
|
1 |
|
Assert::stringNotEmpty($address); |
|
173
|
|
|
|
|
174
|
1 |
|
$response = $this->httpPost(sprintf('/v3/lists/%s/validate', $address)); |
|
175
|
|
|
|
|
176
|
1 |
|
return $this->hydrateResponse($response, ValidateResponse::class); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get mailing list validation status. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $address Address of the mailing list |
|
183
|
|
|
* |
|
184
|
|
|
* @return ValidationStatusResponse |
|
185
|
|
|
* |
|
186
|
|
|
* @throws \Exception |
|
187
|
|
|
*/ |
|
188
|
1 |
View Code Duplication |
public function getValidationStatus(string $address) |
|
|
|
|
|
|
189
|
|
|
{ |
|
190
|
1 |
|
Assert::stringNotEmpty($address); |
|
191
|
|
|
|
|
192
|
1 |
|
$response = $this->httpGet(sprintf('/v3/lists/%s/validate', $address)); |
|
193
|
|
|
|
|
194
|
1 |
|
return $this->hydrateResponse($response, ValidationStatusResponse::class); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Cancel mailing list validation. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $address Address of the mailing list |
|
201
|
|
|
* |
|
202
|
|
|
* @return ValidationCancelResponse |
|
203
|
|
|
* |
|
204
|
|
|
* @throws \Exception |
|
205
|
|
|
*/ |
|
206
|
1 |
View Code Duplication |
public function cancelValidation(string $address) |
|
|
|
|
|
|
207
|
|
|
{ |
|
208
|
1 |
|
Assert::stringNotEmpty($address); |
|
209
|
|
|
|
|
210
|
1 |
|
$response = $this->httpDelete(sprintf('/v3/lists/%s/validate', $address)); |
|
211
|
|
|
|
|
212
|
1 |
|
return $this->hydrateResponse($response, ValidationCancelResponse::class); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: