Completed
Push — master ( 90028d...27d579 )
by David
03:16
created

Domain::verify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
     * @param string[] $ips                an array of ips to be assigned to the domain
83
     *
84
     * @return CreateResponse|array|ResponseInterface
85
     */
86 7
    public function create(string $domain, string $smtpPass = null, string $spamAction = null, bool $wildcard = null, bool $forceDkimAuthority = null, ?array $ips = null)
87
    {
88 7
        Assert::stringNotEmpty($domain);
89
90 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...
91
92 7
        if (!empty($smtpPass)) {
93 6
            Assert::stringNotEmpty($smtpPass);
94
95 6
            $params['smtp_password'] = $smtpPass;
96
        }
97
98 7
        if (!empty($spamAction)) {
99
            // TODO(sean.johnson): Extended spam filter input validation.
100 3
            Assert::stringNotEmpty($spamAction);
101
102 3
            $params['spam_action'] = $spamAction;
103
        }
104
105 7
        if (null !== $wildcard) {
106 2
            Assert::boolean($wildcard);
107
108 2
            $params['wildcard'] = $wildcard ? 'true' : 'false';
109
        }
110
111 7
        if (null !== $forceDkimAuthority) {
112 2
            Assert::boolean($forceDkimAuthority);
113
114 2
            $params['force_dkim_authority'] = $forceDkimAuthority ? 'true' : 'false';
115
        }
116
117 7
        if (null !== $ips) {
118 1
            Assert::isList($ips);
119 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...
120
121 1
            $params['ips'] = join(',', $ips);
122
        }
123
124 7
        $response = $this->httpPost('/v3/domains', $params);
125
126 7
        return $this->hydrateResponse($response, CreateResponse::class);
127
    }
128
129
    /**
130
     * Removes a domain from the account.
131
     * WARNING: This action is irreversible! Be cautious!
132
     *
133
     * @param string $domain name of the domain
134
     *
135
     * @return DeleteResponse|array|ResponseInterface
136
     */
137 1
    public function delete(string $domain)
138
    {
139 1
        Assert::stringNotEmpty($domain);
140
141 1
        $response = $this->httpDelete(sprintf('/v3/domains/%s', $domain));
142
143 1
        return $this->hydrateResponse($response, DeleteResponse::class);
144
    }
145
146
    /**
147
     * Returns a list of SMTP credentials for the specified domain.
148
     *
149
     * @param string $domain name of the domain
150
     * @param int    $limit  Number of credentials to return
151
     * @param int    $skip   Number of credentials to omit from the list
152
     *
153
     * @return CredentialResponse
154
     */
155
    public function credentials(string $domain, int $limit = 100, int $skip = 0)
156
    {
157
        Assert::stringNotEmpty($domain);
158
        $params = [
159
            'limit' => $limit,
160
            'skip' => $skip,
161
        ];
162
163
        $response = $this->httpGet(sprintf('/v3/domains/%s/credentials', $domain), $params);
164
165
        return $this->hydrateResponse($response, CredentialResponse::class);
166
    }
167
168
    /**
169
     * Create a new SMTP credential pair for the specified domain.
170
     *
171
     * @param string $domain   name of the domain
172
     * @param string $login    SMTP Username
173
     * @param string $password SMTP Password. Length min 5, max 32.
174
     *
175
     * @return CreateCredentialResponse|array|ResponseInterface
176
     */
177 1
    public function createCredential(string $domain, string $login, string $password)
178
    {
179 1
        Assert::stringNotEmpty($domain);
180 1
        Assert::stringNotEmpty($login);
181 1
        Assert::stringNotEmpty($password);
182 1
        Assert::lengthBetween($password, 5, 32, 'SMTP password must be between 5 and 32 characters.');
183
184
        $params = [
185 1
            'login' => $login,
186 1
            'password' => $password,
187
        ];
188
189 1
        $response = $this->httpPost(sprintf('/v3/domains/%s/credentials', $domain), $params);
190
191 1
        return $this->hydrateResponse($response, CreateCredentialResponse::class);
192
    }
193
194
    /**
195
     * Update a set of SMTP credentials for the specified domain.
196
     *
197
     * @param string $domain name of the domain
198
     * @param string $login  SMTP Username
199
     * @param string $pass   New SMTP Password. Length min 5, max 32.
200
     *
201
     * @return UpdateCredentialResponse|array|ResponseInterface
202
     */
203 1
    public function updateCredential(string $domain, string $login, string $pass)
204
    {
205 1
        Assert::stringNotEmpty($domain);
206 1
        Assert::stringNotEmpty($login);
207 1
        Assert::stringNotEmpty($pass);
208 1
        Assert::lengthBetween($pass, 5, 32, 'SMTP password must be between 5 and 32 characters.');
209
210
        $params = [
211 1
            'password' => $pass,
212
        ];
213
214 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/credentials/%s', $domain, $login), $params);
215
216 1
        return $this->hydrateResponse($response, UpdateCredentialResponse::class);
217
    }
218
219
    /**
220
     * Remove a set of SMTP credentials from the specified domain.
221
     *
222
     * @param string $domain name of the domain
223
     * @param string $login  SMTP Username
224
     *
225
     * @return DeleteCredentialResponse|array|ResponseInterface
226
     */
227 1
    public function deleteCredential(string $domain, string $login)
228
    {
229 1
        Assert::stringNotEmpty($domain);
230 1
        Assert::stringNotEmpty($login);
231
232 1
        $response = $this->httpDelete(
233 1
            sprintf(
234 1
                '/v3/domains/%s/credentials/%s',
235 1
                $domain,
236 1
                $login
237
            )
238
        );
239
240 1
        return $this->hydrateResponse($response, DeleteCredentialResponse::class);
241
    }
242
243
    /**
244
     * Returns delivery connection settings for the specified domain.
245
     *
246
     * @param string $domain name of the domain
247
     *
248
     * @return ConnectionResponse|ResponseInterface
249
     */
250 1
    public function connection(string $domain)
251
    {
252 1
        Assert::stringNotEmpty($domain);
253
254 1
        $response = $this->httpGet(sprintf('/v3/domains/%s/connection', $domain));
255
256 1
        return $this->hydrateResponse($response, ConnectionResponse::class);
257
    }
258
259
    /**
260
     * Updates the specified delivery connection settings for the specified domain.
261
     * If a parameter is passed in as null, it will not be updated.
262
     *
263
     * @param string    $domain     name of the domain
264
     * @param bool|null $requireTLS enforces that messages are sent only over a TLS connection
265
     * @param bool|null $noVerify   disables TLS certificate and hostname verification
266
     *
267
     * @return UpdateConnectionResponse|array|ResponseInterface
268
     */
269 1
    public function updateConnection(string $domain, ?bool $requireTLS, ?bool $noVerify)
270
    {
271 1
        Assert::stringNotEmpty($domain);
272 1
        $params = [];
273
274 1
        if (null !== $requireTLS) {
275 1
            $params['require_tls'] = $requireTLS ? 'true' : 'false';
276
        }
277
278 1
        if (null !== $noVerify) {
279 1
            $params['skip_verification'] = $noVerify ? 'true' : 'false';
280
        }
281
282 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/connection', $domain), $params);
283
284 1
        return $this->hydrateResponse($response, UpdateConnectionResponse::class);
285
    }
286
287
    /**
288
     * Returns a single domain.
289
     *
290
     * @param string $domain name of the domain
291
     *
292
     * @return VerifyResponse|array|ResponseInterface
293
     */
294 1
    public function verify(string $domain)
295
    {
296 1
        Assert::stringNotEmpty($domain);
297
298 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/verify', $domain));
299
300 1
        return $this->hydrateResponse($response, VerifyResponse::class);
301
    }
302
}
303