Completed
Push — master ( 27d579...87c59d )
by David
02:18
created

Domain::updateUnsubscribeTracking()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 4
crap 2
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\TrackingResponse;
24
use Mailgun\Model\Domain\UpdateClickTrackingResponse;
25
use Mailgun\Model\Domain\UpdateConnectionResponse;
26
use Mailgun\Model\Domain\UpdateCredentialResponse;
27
use Mailgun\Model\Domain\UpdateOpenTrackingResponse;
28
use Mailgun\Model\Domain\UpdateUnsubscribeTrackingResponse;
29
use Mailgun\Model\Domain\VerifyResponse;
30
use Psr\Http\Message\ResponseInterface;
31
32
/**
33
 * @see https://documentation.mailgun.com/api-domains.html
34
 *
35
 * @author Sean Johnson <[email protected]>
36
 */
37
class Domain extends HttpApi
38
{
39
    /**
40
     * Returns a list of domains on the account.
41
     *
42
     * @return IndexResponse
43
     */
44 1
    public function index(int $limit = 100, int $skip = 0)
45
    {
46 1
        Assert::range($limit, 1, 1000);
47
48
        $params = [
49 1
            'limit' => $limit,
50 1
            'skip' => $skip,
51
        ];
52
53 1
        $response = $this->httpGet('/v3/domains', $params);
54
55 1
        return $this->hydrateResponse($response, IndexResponse::class);
56
    }
57
58
    /**
59
     * Returns a single domain.
60
     *
61
     * @param string $domain name of the domain
62
     *
63
     * @return ShowResponse|array|ResponseInterface
64
     */
65 1
    public function show(string $domain)
66
    {
67 1
        Assert::stringNotEmpty($domain);
68
69 1
        $response = $this->httpGet(sprintf('/v3/domains/%s', $domain));
70
71 1
        return $this->hydrateResponse($response, ShowResponse::class);
72
    }
73
74
    /**
75
     * Creates a new domain for the account.
76
     * See below for spam filtering parameter information.
77
     * {@link https://documentation.mailgun.com/user_manual.html#um-spam-filter}.
78
     *
79
     * @see https://documentation.mailgun.com/en/latest/api-domains.html#domains
80
     *
81
     * @param string   $domain             name of the domain
82
     * @param string   $smtpPass           password for SMTP authentication
83
     * @param string   $spamAction         `disable` or `tag` - inbound spam filtering
84
     * @param bool     $wildcard           domain will accept email for subdomains
85
     * @param bool     $forceDkimAuthority force DKIM authority
86
     * @param string[] $ips                an array of ips to be assigned to the domain
87
     *
88
     * @return CreateResponse|array|ResponseInterface
89
     */
90 7
    public function create(string $domain, string $smtpPass = null, string $spamAction = null, bool $wildcard = null, bool $forceDkimAuthority = null, ?array $ips = null)
91
    {
92 7
        Assert::stringNotEmpty($domain);
93
94 7
        $params['name'] = $domain;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
95
96 7
        if (!empty($smtpPass)) {
97 6
            Assert::stringNotEmpty($smtpPass);
98
99 6
            $params['smtp_password'] = $smtpPass;
100
        }
101
102 7
        if (!empty($spamAction)) {
103
            // TODO(sean.johnson): Extended spam filter input validation.
104 3
            Assert::stringNotEmpty($spamAction);
105
106 3
            $params['spam_action'] = $spamAction;
107
        }
108
109 7
        if (null !== $wildcard) {
110 2
            Assert::boolean($wildcard);
111
112 2
            $params['wildcard'] = $wildcard ? 'true' : 'false';
113
        }
114
115 7
        if (null !== $forceDkimAuthority) {
116 2
            Assert::boolean($forceDkimAuthority);
117
118 2
            $params['force_dkim_authority'] = $forceDkimAuthority ? 'true' : 'false';
119
        }
120
121 7
        if (null !== $ips) {
122 1
            Assert::isList($ips);
123 1
            Assert::allString($ips);
0 ignored issues
show
Bug introduced by
The method allString() does not exist on Mailgun\Assert. Did you maybe mean string()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
124
125 1
            $params['ips'] = join(',', $ips);
126
        }
127
128 7
        $response = $this->httpPost('/v3/domains', $params);
129
130 7
        return $this->hydrateResponse($response, CreateResponse::class);
131
    }
132
133
    /**
134
     * Removes a domain from the account.
135
     * WARNING: This action is irreversible! Be cautious!
136
     *
137
     * @param string $domain name of the domain
138
     *
139
     * @return DeleteResponse|array|ResponseInterface
140
     */
141 1
    public function delete(string $domain)
142
    {
143 1
        Assert::stringNotEmpty($domain);
144
145 1
        $response = $this->httpDelete(sprintf('/v3/domains/%s', $domain));
146
147 1
        return $this->hydrateResponse($response, DeleteResponse::class);
148
    }
149
150
    /**
151
     * Returns a list of SMTP credentials for the specified domain.
152
     *
153
     * @param string $domain name of the domain
154
     * @param int    $limit  Number of credentials to return
155
     * @param int    $skip   Number of credentials to omit from the list
156
     *
157
     * @return CredentialResponse
158
     */
159
    public function credentials(string $domain, int $limit = 100, int $skip = 0)
160
    {
161
        Assert::stringNotEmpty($domain);
162
        $params = [
163
            'limit' => $limit,
164
            'skip' => $skip,
165
        ];
166
167
        $response = $this->httpGet(sprintf('/v3/domains/%s/credentials', $domain), $params);
168
169
        return $this->hydrateResponse($response, CredentialResponse::class);
170
    }
171
172
    /**
173
     * Create a new SMTP credential pair for the specified domain.
174
     *
175
     * @param string $domain   name of the domain
176
     * @param string $login    SMTP Username
177
     * @param string $password SMTP Password. Length min 5, max 32.
178
     *
179
     * @return CreateCredentialResponse|array|ResponseInterface
180
     */
181 1
    public function createCredential(string $domain, string $login, string $password)
182
    {
183 1
        Assert::stringNotEmpty($domain);
184 1
        Assert::stringNotEmpty($login);
185 1
        Assert::stringNotEmpty($password);
186 1
        Assert::lengthBetween($password, 5, 32, 'SMTP password must be between 5 and 32 characters.');
187
188
        $params = [
189 1
            'login' => $login,
190 1
            'password' => $password,
191
        ];
192
193 1
        $response = $this->httpPost(sprintf('/v3/domains/%s/credentials', $domain), $params);
194
195 1
        return $this->hydrateResponse($response, CreateCredentialResponse::class);
196
    }
197
198
    /**
199
     * Update a set of SMTP credentials for the specified domain.
200
     *
201
     * @param string $domain name of the domain
202
     * @param string $login  SMTP Username
203
     * @param string $pass   New SMTP Password. Length min 5, max 32.
204
     *
205
     * @return UpdateCredentialResponse|array|ResponseInterface
206
     */
207 1
    public function updateCredential(string $domain, string $login, string $pass)
208
    {
209 1
        Assert::stringNotEmpty($domain);
210 1
        Assert::stringNotEmpty($login);
211 1
        Assert::stringNotEmpty($pass);
212 1
        Assert::lengthBetween($pass, 5, 32, 'SMTP password must be between 5 and 32 characters.');
213
214
        $params = [
215 1
            'password' => $pass,
216
        ];
217
218 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/credentials/%s', $domain, $login), $params);
219
220 1
        return $this->hydrateResponse($response, UpdateCredentialResponse::class);
221
    }
222
223
    /**
224
     * Remove a set of SMTP credentials from the specified domain.
225
     *
226
     * @param string $domain name of the domain
227
     * @param string $login  SMTP Username
228
     *
229
     * @return DeleteCredentialResponse|array|ResponseInterface
230
     */
231 1
    public function deleteCredential(string $domain, string $login)
232
    {
233 1
        Assert::stringNotEmpty($domain);
234 1
        Assert::stringNotEmpty($login);
235
236 1
        $response = $this->httpDelete(
237 1
            sprintf(
238 1
                '/v3/domains/%s/credentials/%s',
239 1
                $domain,
240 1
                $login
241
            )
242
        );
243
244 1
        return $this->hydrateResponse($response, DeleteCredentialResponse::class);
245
    }
246
247
    /**
248
     * Returns delivery connection settings for the specified domain.
249
     *
250
     * @param string $domain name of the domain
251
     *
252
     * @return ConnectionResponse|ResponseInterface
253
     */
254 1
    public function connection(string $domain)
255
    {
256 1
        Assert::stringNotEmpty($domain);
257
258 1
        $response = $this->httpGet(sprintf('/v3/domains/%s/connection', $domain));
259
260 1
        return $this->hydrateResponse($response, ConnectionResponse::class);
261
    }
262
263
    /**
264
     * Updates the specified delivery connection settings for the specified domain.
265
     * If a parameter is passed in as null, it will not be updated.
266
     *
267
     * @param string    $domain     name of the domain
268
     * @param bool|null $requireTLS enforces that messages are sent only over a TLS connection
269
     * @param bool|null $noVerify   disables TLS certificate and hostname verification
270
     *
271
     * @return UpdateConnectionResponse|array|ResponseInterface
272
     */
273 1
    public function updateConnection(string $domain, ?bool $requireTLS, ?bool $noVerify)
274
    {
275 1
        Assert::stringNotEmpty($domain);
276 1
        $params = [];
277
278 1
        if (null !== $requireTLS) {
279 1
            $params['require_tls'] = $requireTLS ? 'true' : 'false';
280
        }
281
282 1
        if (null !== $noVerify) {
283 1
            $params['skip_verification'] = $noVerify ? 'true' : 'false';
284
        }
285
286 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/connection', $domain), $params);
287
288 1
        return $this->hydrateResponse($response, UpdateConnectionResponse::class);
289
    }
290
291
    /**
292
     * Returns a single domain.
293
     *
294
     * @param string $domain name of the domain
295
     *
296
     * @return VerifyResponse|array|ResponseInterface
297
     */
298 1
    public function verify(string $domain)
299
    {
300 1
        Assert::stringNotEmpty($domain);
301
302 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/verify', $domain));
303
304 1
        return $this->hydrateResponse($response, VerifyResponse::class);
305
    }
306
307
    /**
308
     * Returns a domain tracking settings.
309
     *
310
     * @param string $domain name of the domain
311
     *
312
     * @return TrackingResponse|array|ResponseInterface
313
     */
314 1
    public function tracking(string $domain)
315
    {
316 1
        Assert::stringNotEmpty($domain);
317
318 1
        $response = $this->httpGet(sprintf('/v3/domains/%s/tracking', $domain));
319
320 1
        return $this->hydrateResponse($response, TrackingResponse::class);
321
    }
322
323
    /**
324
     * Updates a domain click tracking settings.
325
     *
326
     * @param string $domain The name of the domain
327
     * @param string $active The status for this tracking (one of: yes, no)
328
     *
329
     * @return UpdateClickTrackingResponse|array|ResponseInterface
330
     *
331
     * @throws \Exception
332
     */
333 4
    public function updateClickTracking(string $domain, string $active)
334
    {
335 4
        Assert::stringNotEmpty($domain);
336 4
        Assert::stringNotEmpty($active);
337 4
        Assert::oneOf($active, ['yes', 'no', 'htmlonly']);
338
339
        $params = [
340 3
            'active' => $active,
341
        ];
342
343 3
        $response = $this->httpPut(sprintf('/v3/domains/%s/tracking/click', $domain), $params);
344
345 3
        return $this->hydrateResponse($response, UpdateClickTrackingResponse::class);
346
    }
347
348
    /**
349
     * Updates a domain open tracking settings.
350
     *
351
     * @param string $domain The name of the domain
352
     * @param string $active The status for this tracking (one of: yes, no)
353
     *
354
     * @return UpdateOpenTrackingResponse|array|ResponseInterface
355
     */
356 3
    public function updateOpenTracking(string $domain, string $active)
357
    {
358 3
        Assert::stringNotEmpty($domain);
359 3
        Assert::stringNotEmpty($active);
360 3
        Assert::oneOf($active, ['yes', 'no']);
361
362
        $params = [
363 2
            'active' => $active,
364
        ];
365
366 2
        $response = $this->httpPut(sprintf('/v3/domains/%s/tracking/open', $domain), $params);
367
368 2
        return $this->hydrateResponse($response, UpdateOpenTrackingResponse::class);
369
    }
370
371
    /**
372
     * Updates a domain unsubscribe tracking settings.
373
     *
374
     * @param string $domain     The name of the domain
375
     * @param string $active     The status for this tracking (one of: yes, no)
376
     * @param string $htmlFooter The footer for HTML emails
377
     * @param string $textFooter The footer for plain text emails
378
     *
379
     * @return UpdateUnsubscribeTrackingResponse|array|ResponseInterface
380
     *
381
     * @throws \Exception
382
     */
383 3
    public function updateUnsubscribeTracking(string $domain, string $active, string $htmlFooter, string $textFooter)
384
    {
385 3
        Assert::stringNotEmpty($domain);
386 3
        Assert::stringNotEmpty($active);
387 3
        Assert::oneOf($active, ['yes', 'no', 'true', 'false']);
388 2
        Assert::stringNotEmpty($htmlFooter);
389 2
        Assert::nullOrString($textFooter);
0 ignored issues
show
Bug introduced by
The method nullOrString() does not exist on Mailgun\Assert. Did you maybe mean string()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
390
391
        $params = [
392 2
            'active' => (in_array($active, ['yes', 'true'], true)) ? 'true' : 'false',
393 2
            'html_footer' => $htmlFooter,
394 2
            'text_footer' => $textFooter,
395
        ];
396
397 2
        $response = $this->httpPut(sprintf('/v3/domains/%s/tracking/unsubscribe', $domain), $params);
398
399 2
        return $this->hydrateResponse($response, UpdateUnsubscribeTrackingResponse::class);
400
    }
401
}
402