Completed
Push — master ( 8187a4...e80003 )
by Tobias
03:13
created

Domain   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 253
Duplicated Lines 5.93 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 15
loc 253
ccs 87
cts 87
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 14 1
A show() 0 8 1
A create() 0 19 1
A delete() 0 8 1
A credentials() 15 15 1
A createCredential() 0 16 1
A updateCredential() 0 15 1
A deleteCredential() 0 15 1
A connection() 0 8 1
B updateConnection() 0 20 5
A verify() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * Copyright (C) 2013-2016 Mailgun
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Mailgun\Api;
11
12
use Mailgun\Assert;
13
use Mailgun\Model\Domain\ConnectionResponse;
14
use Mailgun\Model\Domain\CreateCredentialResponse;
15
use Mailgun\Model\Domain\CreateResponse;
16
use Mailgun\Model\Domain\CredentialResponse;
17
use Mailgun\Model\Domain\DeleteCredentialResponse;
18
use Mailgun\Model\Domain\DeleteResponse;
19
use Mailgun\Model\Domain\IndexResponse;
20
use Mailgun\Model\Domain\ShowResponse;
21
use Mailgun\Model\Domain\UpdateConnectionResponse;
22
use Mailgun\Model\Domain\UpdateCredentialResponse;
23
use Mailgun\Model\Domain\VerifyResponse;
24
use Psr\Http\Message\ResponseInterface;
25
26
/**
27
 * {@link https://documentation.mailgun.com/api-domains.html}.
28
 *
29
 * @author Sean Johnson <[email protected]>
30
 */
31
class Domain extends HttpApi
32
{
33
    /**
34
     * Returns a list of domains on the account.
35
     *
36
     * @param int $limit
37
     * @param int $skip
38
     *
39
     * @return IndexResponse
40
     */
41 1
    public function index($limit = 100, $skip = 0)
42
    {
43 1
        Assert::integer($limit);
44 1
        Assert::integer($skip);
45
46
        $params = [
47 1
            'limit' => $limit,
48 1
            'skip' => $skip,
49 1
        ];
50
51 1
        $response = $this->httpGet('/v3/domains', $params);
52
53 1
        return $this->hydrateResponse($response, IndexResponse::class);
54
    }
55
56
    /**
57
     * Returns a single domain.
58
     *
59
     * @param string $domain Name of the domain.
60
     *
61
     * @return ShowResponse|array|ResponseInterface
62
     */
63 1
    public function show($domain)
64
    {
65 1
        Assert::stringNotEmpty($domain);
66
67 1
        $response = $this->httpGet(sprintf('/v3/domains/%s', $domain));
68
69 1
        return $this->hydrateResponse($response, ShowResponse::class);
70
    }
71
72
    /**
73
     * Creates a new domain for the account.
74
     * See below for spam filtering parameter information.
75
     * {@link https://documentation.mailgun.com/user_manual.html#um-spam-filter}.
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
     *
82
     * @return CreateResponse|array|ResponseInterface
83
     */
84 2
    public function create($domain, $smtpPass, $spamAction, $wildcard)
85
    {
86 2
        Assert::stringNotEmpty($domain);
87 2
        Assert::stringNotEmpty($smtpPass);
88
        // TODO(sean.johnson): Extended spam filter input validation.
89 2
        Assert::stringNotEmpty($spamAction);
90 2
        Assert::boolean($wildcard);
91
92
        $params = [
93 2
            'name' => $domain,
94 2
            'smtp_password' => $smtpPass,
95 2
            'spam_action' => $spamAction,
96 2
            'wildcard' => $wildcard,
97 2
        ];
98
99 2
        $response = $this->httpPost('/v3/domains', $params);
100
101 2
        return $this->hydrateResponse($response, CreateResponse::class);
102
    }
103
104
    /**
105
     * Removes a domain from the account.
106
     * WARNING: This action is irreversible! Be cautious!
107
     *
108
     * @param string $domain Name of the domain.
109
     *
110
     * @return DeleteResponse|array|ResponseInterface
111
     */
112 2
    public function delete($domain)
113
    {
114 2
        Assert::stringNotEmpty($domain);
115
116 2
        $response = $this->httpDelete(sprintf('/v3/domains/%s', $domain));
117
118 2
        return $this->hydrateResponse($response, DeleteResponse::class);
119
    }
120
121
    /**
122
     * Returns a list of SMTP credentials for the specified domain.
123
     *
124
     * @param string $domain Name of the domain.
125
     * @param int    $limit  Number of credentials to return
126
     * @param int    $skip   Number of credentials to omit from the list
127
     *
128
     * @return CredentialResponse
129
     */
130 2 View Code Duplication
    public function credentials($domain, $limit = 100, $skip = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132 2
        Assert::stringNotEmpty($domain);
133 2
        Assert::integer($limit);
134 2
        Assert::integer($skip);
135
136
        $params = [
137 2
            'limit' => $limit,
138 2
            'skip' => $skip,
139 2
        ];
140
141 2
        $response = $this->httpGet(sprintf('/v3/domains/%s/credentials', $domain), $params);
142
143 2
        return $this->hydrateResponse($response, CredentialResponse::class);
144
    }
145
146
    /**
147
     * Create a new SMTP credential pair for the specified domain.
148
     *
149
     * @param string $domain   Name of the domain.
150
     * @param string $login    SMTP Username.
151
     * @param string $password SMTP Password. Length min 5, max 32.
152
     *
153
     * @return CreateCredentialResponse|array|ResponseInterface
154
     */
155 3
    public function createCredential($domain, $login, $password)
156
    {
157 3
        Assert::stringNotEmpty($domain);
158 3
        Assert::stringNotEmpty($login);
159 3
        Assert::stringNotEmpty($password);
160 3
        Assert::lengthBetween($password, 5, 32, 'SMTP password must be between 5 and 32 characters.');
161
162
        $params = [
163 1
            'login' => $login,
164 1
            'password' => $password,
165 1
        ];
166
167 1
        $response = $this->httpPost(sprintf('/v3/domains/%s/credentials', $domain), $params);
168
169 1
        return $this->hydrateResponse($response, CreateCredentialResponse::class);
170
    }
171
172
    /**
173
     * Update a set of SMTP credentials for the specified domain.
174
     *
175
     * @param string $domain Name of the domain.
176
     * @param string $login  SMTP Username.
177
     * @param string $pass   New SMTP Password. Length min 5, max 32.
178
     *
179
     * @return UpdateCredentialResponse|array|ResponseInterface
180
     */
181 3
    public function updateCredential($domain, $login, $pass)
182
    {
183 3
        Assert::stringNotEmpty($domain);
184 3
        Assert::stringNotEmpty($login);
185 3
        Assert::stringNotEmpty($pass);
186 3
        Assert::lengthBetween($pass, 5, 32, 'SMTP password must be between 5 and 32 characters.');
187
188
        $params = [
189 1
            'password' => $pass,
190 1
        ];
191
192 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/credentials/%s', $domain, $login), $params);
193
194 1
        return $this->hydrateResponse($response, UpdateCredentialResponse::class);
195
    }
196
197
    /**
198
     * Remove a set of SMTP credentials from the specified domain.
199
     *
200
     * @param string $domain Name of the domain.
201
     * @param string $login  SMTP Username.
202
     *
203
     * @return DeleteCredentialResponse|array|ResponseInterface
204
     */
205 2
    public function deleteCredential($domain, $login)
206
    {
207 2
        Assert::stringNotEmpty($domain);
208 2
        Assert::stringNotEmpty($login);
209
210 2
        $response = $this->httpDelete(
211 2
            sprintf(
212 2
                '/v3/domains/%s/credentials/%s',
213 2
                $domain,
214
                $login
215 2
            )
216 2
        );
217
218 2
        return $this->hydrateResponse($response, DeleteCredentialResponse::class);
219
    }
220
221
    /**
222
     * Returns delivery connection settings for the specified domain.
223
     *
224
     * @param string $domain Name of the domain.
225
     *
226
     * @return ConnectionResponse|ResponseInterface
227
     */
228 1
    public function connection($domain)
229
    {
230 1
        Assert::stringNotEmpty($domain);
231
232 1
        $response = $this->httpGet(sprintf('/v3/domains/%s/connection', $domain));
233
234 1
        return $this->hydrateResponse($response, ConnectionResponse::class);
235
    }
236
237
    /**
238
     * Updates the specified delivery connection settings for the specified domain.
239
     * If a parameter is passed in as null, it will not be updated.
240
     *
241
     * @param string    $domain     Name of the domain.
242
     * @param bool|null $requireTLS Enforces that messages are sent only over a TLS connection.
243
     * @param bool|null $noVerify   Disables TLS certificate and hostname verification.
244
     *
245
     * @return UpdateConnectionResponse|array|ResponseInterface
246
     */
247 1
    public function updateConnection($domain, $requireTLS, $noVerify)
248
    {
249 1
        Assert::stringNotEmpty($domain);
250 1
        Assert::nullOrBoolean($requireTLS);
251 1
        Assert::nullOrBoolean($noVerify);
252
253 1
        $params = [];
254
255 1
        if (null !== $requireTLS) {
256 1
            $params['require_tls'] = $requireTLS ? 'true' : 'false';
257 1
        }
258
259 1
        if (null !== $noVerify) {
260 1
            $params['skip_verification'] = $noVerify ? 'true' : 'false';
261 1
        }
262
263 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/connection', $domain), $params);
264
265 1
        return $this->hydrateResponse($response, UpdateConnectionResponse::class);
266
    }
267
268
    /**
269
     * Returns a single domain.
270
     *
271
     * @param string $domain Name of the domain.
272
     *
273
     * @return VerifyResponse|array|ResponseInterface
274
     */
275 1
    public function verify($domain)
276
    {
277 1
        Assert::stringNotEmpty($domain);
278
279 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/verify', $domain));
280
281 1
        return $this->hydrateResponse($response, VerifyResponse::class);
282
    }
283
}
284