Completed
Push — master ( 1fd4f2...6dfd2b )
by Tobias
03:37
created

Domain::updateCredential()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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