Completed
Push — master ( 7e438f...abf2a0 )
by Tobias
01:56
created

Domain   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.78%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 247
ccs 67
cts 73
cp 0.9178
rs 10
c 0
b 0
f 0

11 Methods

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