|
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\Assert; |
|
15
|
|
|
use Mailgun\Model\Domain\ConnectionResponse; |
|
16
|
|
|
use Mailgun\Model\Domain\CreateCredentialResponse; |
|
17
|
|
|
use Mailgun\Model\Domain\CreateResponse; |
|
18
|
|
|
use Mailgun\Model\Domain\CredentialResponse; |
|
19
|
|
|
use Mailgun\Model\Domain\DeleteCredentialResponse; |
|
20
|
|
|
use Mailgun\Model\Domain\DeleteResponse; |
|
21
|
|
|
use Mailgun\Model\Domain\IndexResponse; |
|
22
|
|
|
use Mailgun\Model\Domain\ShowResponse; |
|
23
|
|
|
use Mailgun\Model\Domain\UpdateConnectionResponse; |
|
24
|
|
|
use Mailgun\Model\Domain\UpdateCredentialResponse; |
|
25
|
|
|
use Mailgun\Model\Domain\VerifyResponse; |
|
26
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @see https://documentation.mailgun.com/api-domains.html |
|
30
|
|
|
* |
|
31
|
|
|
* @author Sean Johnson <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class Domain extends HttpApi |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Returns a list of domains on the account. |
|
37
|
|
|
* |
|
38
|
|
|
* @return IndexResponse |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function index(int $limit = 100, int $skip = 0) |
|
41
|
|
|
{ |
|
42
|
1 |
|
Assert::range($limit, 1, 1000); |
|
43
|
|
|
|
|
44
|
|
|
$params = [ |
|
45
|
1 |
|
'limit' => $limit, |
|
46
|
1 |
|
'skip' => $skip, |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
1 |
|
$response = $this->httpGet('/v3/domains', $params); |
|
50
|
|
|
|
|
51
|
1 |
|
return $this->hydrateResponse($response, IndexResponse::class); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns a single domain. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $domain name of the domain |
|
58
|
|
|
* |
|
59
|
|
|
* @return ShowResponse|array|ResponseInterface |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function show(string $domain) |
|
62
|
|
|
{ |
|
63
|
1 |
|
Assert::stringNotEmpty($domain); |
|
64
|
|
|
|
|
65
|
1 |
|
$response = $this->httpGet(sprintf('/v3/domains/%s', $domain)); |
|
66
|
|
|
|
|
67
|
1 |
|
return $this->hydrateResponse($response, ShowResponse::class); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Creates a new domain for the account. |
|
72
|
|
|
* See below for spam filtering parameter information. |
|
73
|
|
|
* {@link https://documentation.mailgun.com/user_manual.html#um-spam-filter}. |
|
74
|
|
|
* |
|
75
|
|
|
* @see https://documentation.mailgun.com/en/latest/api-domains.html#domains |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $domain name of the domain |
|
78
|
|
|
* @param string $smtpPass password for SMTP authentication |
|
79
|
|
|
* @param string $spamAction `disable` or `tag` - inbound spam filtering |
|
80
|
|
|
* @param bool $wildcard domain will accept email for subdomains |
|
81
|
|
|
* @param bool $forceDkimAuthority force DKIM authority |
|
82
|
|
|
* |
|
83
|
|
|
* @return CreateResponse|array|ResponseInterface |
|
84
|
|
|
*/ |
|
85
|
6 |
|
public function create(string $domain, string $smtpPass = null, string $spamAction = null, bool $wildcard = null, bool $forceDkimAuthority = null) |
|
86
|
|
|
{ |
|
87
|
6 |
|
Assert::stringNotEmpty($domain); |
|
88
|
|
|
|
|
89
|
6 |
|
$params['name'] = $domain; |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
6 |
|
if (!empty($smtpPass)) { |
|
92
|
5 |
|
Assert::stringNotEmpty($smtpPass); |
|
93
|
|
|
|
|
94
|
5 |
|
$params['smtp_password'] = $smtpPass; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
6 |
|
if (!empty($spamAction)) { |
|
98
|
|
|
// TODO(sean.johnson): Extended spam filter input validation. |
|
99
|
3 |
|
Assert::stringNotEmpty($spamAction); |
|
100
|
|
|
|
|
101
|
3 |
|
$params['spam_action'] = $spamAction; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
6 |
|
if (null !== $wildcard) { |
|
105
|
2 |
|
Assert::boolean($wildcard); |
|
106
|
|
|
|
|
107
|
2 |
|
$params['wildcard'] = $wildcard ? 'true' : 'false'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
6 |
|
if (null !== $forceDkimAuthority) { |
|
111
|
2 |
|
Assert::boolean($forceDkimAuthority); |
|
112
|
|
|
|
|
113
|
2 |
|
$params['force_dkim_authority'] = $forceDkimAuthority ? 'true' : 'false'; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
6 |
|
$response = $this->httpPost('/v3/domains', $params); |
|
117
|
|
|
|
|
118
|
6 |
|
return $this->hydrateResponse($response, CreateResponse::class); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Removes a domain from the account. |
|
123
|
|
|
* WARNING: This action is irreversible! Be cautious! |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $domain name of the domain |
|
126
|
|
|
* |
|
127
|
|
|
* @return DeleteResponse|array|ResponseInterface |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function delete(string $domain) |
|
130
|
|
|
{ |
|
131
|
1 |
|
Assert::stringNotEmpty($domain); |
|
132
|
|
|
|
|
133
|
1 |
|
$response = $this->httpDelete(sprintf('/v3/domains/%s', $domain)); |
|
134
|
|
|
|
|
135
|
1 |
|
return $this->hydrateResponse($response, DeleteResponse::class); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Returns a list of SMTP credentials for the specified domain. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $domain name of the domain |
|
142
|
|
|
* @param int $limit Number of credentials to return |
|
143
|
|
|
* @param int $skip Number of credentials to omit from the list |
|
144
|
|
|
* |
|
145
|
|
|
* @return CredentialResponse |
|
146
|
|
|
*/ |
|
147
|
|
|
public function credentials(string $domain, int $limit = 100, int $skip = 0) |
|
148
|
|
|
{ |
|
149
|
|
|
Assert::stringNotEmpty($domain); |
|
150
|
|
|
$params = [ |
|
151
|
|
|
'limit' => $limit, |
|
152
|
|
|
'skip' => $skip, |
|
153
|
|
|
]; |
|
154
|
|
|
|
|
155
|
|
|
$response = $this->httpGet(sprintf('/v3/domains/%s/credentials', $domain), $params); |
|
156
|
|
|
|
|
157
|
|
|
return $this->hydrateResponse($response, CredentialResponse::class); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Create a new SMTP credential pair for the specified domain. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $domain name of the domain |
|
164
|
|
|
* @param string $login SMTP Username |
|
165
|
|
|
* @param string $password SMTP Password. Length min 5, max 32. |
|
166
|
|
|
* |
|
167
|
|
|
* @return CreateCredentialResponse|array|ResponseInterface |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function createCredential(string $domain, string $login, string $password) |
|
170
|
|
|
{ |
|
171
|
1 |
|
Assert::stringNotEmpty($domain); |
|
172
|
1 |
|
Assert::stringNotEmpty($login); |
|
173
|
1 |
|
Assert::stringNotEmpty($password); |
|
174
|
1 |
|
Assert::lengthBetween($password, 5, 32, 'SMTP password must be between 5 and 32 characters.'); |
|
175
|
|
|
|
|
176
|
|
|
$params = [ |
|
177
|
1 |
|
'login' => $login, |
|
178
|
1 |
|
'password' => $password, |
|
179
|
|
|
]; |
|
180
|
|
|
|
|
181
|
1 |
|
$response = $this->httpPost(sprintf('/v3/domains/%s/credentials', $domain), $params); |
|
182
|
|
|
|
|
183
|
1 |
|
return $this->hydrateResponse($response, CreateCredentialResponse::class); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Update a set of SMTP credentials for the specified domain. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $domain name of the domain |
|
190
|
|
|
* @param string $login SMTP Username |
|
191
|
|
|
* @param string $pass New SMTP Password. Length min 5, max 32. |
|
192
|
|
|
* |
|
193
|
|
|
* @return UpdateCredentialResponse|array|ResponseInterface |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public function updateCredential(string $domain, string $login, string $pass) |
|
196
|
|
|
{ |
|
197
|
1 |
|
Assert::stringNotEmpty($domain); |
|
198
|
1 |
|
Assert::stringNotEmpty($login); |
|
199
|
1 |
|
Assert::stringNotEmpty($pass); |
|
200
|
1 |
|
Assert::lengthBetween($pass, 5, 32, 'SMTP password must be between 5 and 32 characters.'); |
|
201
|
|
|
|
|
202
|
|
|
$params = [ |
|
203
|
1 |
|
'password' => $pass, |
|
204
|
|
|
]; |
|
205
|
|
|
|
|
206
|
1 |
|
$response = $this->httpPut(sprintf('/v3/domains/%s/credentials/%s', $domain, $login), $params); |
|
207
|
|
|
|
|
208
|
1 |
|
return $this->hydrateResponse($response, UpdateCredentialResponse::class); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Remove a set of SMTP credentials from the specified domain. |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $domain name of the domain |
|
215
|
|
|
* @param string $login SMTP Username |
|
216
|
|
|
* |
|
217
|
|
|
* @return DeleteCredentialResponse|array|ResponseInterface |
|
218
|
|
|
*/ |
|
219
|
1 |
|
public function deleteCredential(string $domain, string $login) |
|
220
|
|
|
{ |
|
221
|
1 |
|
Assert::stringNotEmpty($domain); |
|
222
|
1 |
|
Assert::stringNotEmpty($login); |
|
223
|
|
|
|
|
224
|
1 |
|
$response = $this->httpDelete( |
|
225
|
1 |
|
sprintf( |
|
226
|
1 |
|
'/v3/domains/%s/credentials/%s', |
|
227
|
1 |
|
$domain, |
|
228
|
1 |
|
$login |
|
229
|
|
|
) |
|
230
|
|
|
); |
|
231
|
|
|
|
|
232
|
1 |
|
return $this->hydrateResponse($response, DeleteCredentialResponse::class); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Returns delivery connection settings for the specified domain. |
|
237
|
|
|
* |
|
238
|
|
|
* @param string $domain name of the domain |
|
239
|
|
|
* |
|
240
|
|
|
* @return ConnectionResponse|ResponseInterface |
|
241
|
|
|
*/ |
|
242
|
1 |
|
public function connection(string $domain) |
|
243
|
|
|
{ |
|
244
|
1 |
|
Assert::stringNotEmpty($domain); |
|
245
|
|
|
|
|
246
|
1 |
|
$response = $this->httpGet(sprintf('/v3/domains/%s/connection', $domain)); |
|
247
|
|
|
|
|
248
|
1 |
|
return $this->hydrateResponse($response, ConnectionResponse::class); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Updates the specified delivery connection settings for the specified domain. |
|
253
|
|
|
* If a parameter is passed in as null, it will not be updated. |
|
254
|
|
|
* |
|
255
|
|
|
* @param string $domain name of the domain |
|
256
|
|
|
* @param bool|null $requireTLS enforces that messages are sent only over a TLS connection |
|
257
|
|
|
* @param bool|null $noVerify disables TLS certificate and hostname verification |
|
258
|
|
|
* |
|
259
|
|
|
* @return UpdateConnectionResponse|array|ResponseInterface |
|
260
|
|
|
*/ |
|
261
|
1 |
|
public function updateConnection(string $domain, ?bool $requireTLS, ?bool $noVerify) |
|
262
|
|
|
{ |
|
263
|
1 |
|
Assert::stringNotEmpty($domain); |
|
264
|
1 |
|
$params = []; |
|
265
|
|
|
|
|
266
|
1 |
|
if (null !== $requireTLS) { |
|
267
|
1 |
|
$params['require_tls'] = $requireTLS ? 'true' : 'false'; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
1 |
|
if (null !== $noVerify) { |
|
271
|
1 |
|
$params['skip_verification'] = $noVerify ? 'true' : 'false'; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
1 |
|
$response = $this->httpPut(sprintf('/v3/domains/%s/connection', $domain), $params); |
|
275
|
|
|
|
|
276
|
1 |
|
return $this->hydrateResponse($response, UpdateConnectionResponse::class); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Returns a single domain. |
|
281
|
|
|
* |
|
282
|
|
|
* @param string $domain name of the domain |
|
283
|
|
|
* |
|
284
|
|
|
* @return VerifyResponse|array|ResponseInterface |
|
285
|
|
|
*/ |
|
286
|
1 |
|
public function verify(string $domain) |
|
287
|
|
|
{ |
|
288
|
1 |
|
Assert::stringNotEmpty($domain); |
|
289
|
|
|
|
|
290
|
1 |
|
$response = $this->httpPut(sprintf('/v3/domains/%s/verify', $domain)); |
|
291
|
|
|
|
|
292
|
1 |
|
return $this->hydrateResponse($response, VerifyResponse::class); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.