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