Completed
Push — master ( fab86b...27b5c8 )
by Tobias
10:27 queued 07:31
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-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 Psr\Http\Message\ResponseInterface;
24
25
/**
26
 * {@link https://documentation.mailgun.com/api-domains.html}.
27
 *
28
 * @author Sean Johnson <[email protected]>
29
 */
30
class Domain extends HttpApi
31
{
32
    /**
33
     * Returns a list of domains on the account.
34
     *
35
     * @param int $limit
36
     * @param int $skip
37
     *
38
     * @return IndexResponse
39
     */
40 1
    public function index($limit = 100, $skip = 0)
41
    {
42 1
        Assert::integer($limit);
43 1
        Assert::integer($skip);
44
45
        $params = [
46 1
            'limit' => $limit,
47 1
            'skip' => $skip,
48 1
        ];
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($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
     * @param string $domain     Name of the domain.
77
     * @param string $smtpPass   Password for SMTP authentication.
78
     * @param string $spamAction `disable` or `tag` - inbound spam filtering.
79
     * @param bool   $wildcard   Domain will accept email for subdomains.
80
     *
81
     * @return CreateResponse|array|ResponseInterface
82
     */
83 2
    public function create($domain, $smtpPass, $spamAction, $wildcard)
84
    {
85 2
        Assert::stringNotEmpty($domain);
86 2
        Assert::stringNotEmpty($smtpPass);
87
        // TODO(sean.johnson): Extended spam filter input validation.
88 2
        Assert::stringNotEmpty($spamAction);
89 2
        Assert::boolean($wildcard);
90
91
        $params = [
92 2
            'name' => $domain,
93 2
            'smtp_password' => $smtpPass,
94 2
            'spam_action' => $spamAction,
95 2
            'wildcard' => $wildcard,
96 2
        ];
97
98 2
        $response = $this->httpPost('/v3/domains', $params);
99
100 2
        return $this->hydrateResponse($response, CreateResponse::class);
101
    }
102
103
    /**
104
     * Removes a domain from the account.
105
     * WARNING: This action is irreversible! Be cautious!
106
     *
107
     * @param string $domain Name of the domain.
108
     *
109
     * @return DeleteResponse|array|ResponseInterface
110
     */
111 2
    public function delete($domain)
112
    {
113 2
        Assert::stringNotEmpty($domain);
114
115 2
        $response = $this->httpDelete(sprintf('/v3/domains/%s', $domain));
116
117 2
        return $this->hydrateResponse($response, DeleteResponse::class);
118
    }
119
120
    /**
121
     * Returns a list of SMTP credentials for the specified domain.
122
     *
123
     * @param string $domain Name of the domain.
124
     * @param int    $limit  Number of credentials to return
125
     * @param int    $skip   Number of credentials to omit from the list
126
     *
127
     * @return CredentialResponse
128
     */
129 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...
130
    {
131 2
        Assert::stringNotEmpty($domain);
132 2
        Assert::integer($limit);
133 2
        Assert::integer($skip);
134
135
        $params = [
136 2
            'limit' => $limit,
137 2
            'skip' => $skip,
138 2
        ];
139
140 2
        $response = $this->httpGet(sprintf('/v3/domains/%s/credentials', $domain), $params);
141
142 2
        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 3
    public function createCredential($domain, $login, $password)
155
    {
156 3
        Assert::stringNotEmpty($domain);
157 3
        Assert::stringNotEmpty($login);
158 3
        Assert::stringNotEmpty($password);
159 3
        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 1
        ];
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 3
    public function updateCredential($domain, $login, $pass)
181
    {
182 3
        Assert::stringNotEmpty($domain);
183 3
        Assert::stringNotEmpty($login);
184 3
        Assert::stringNotEmpty($pass);
185 3
        Assert::lengthBetween($pass, 5, 32, 'SMTP password must be between 5 and 32 characters.');
186
187
        $params = [
188 1
            'password' => $pass,
189 1
        ];
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 2
    public function deleteCredential($domain, $login)
205
    {
206 2
        Assert::stringNotEmpty($domain);
207 2
        Assert::stringNotEmpty($login);
208
209 2
        $response = $this->httpDelete(
210 2
            sprintf(
211 2
                '/v3/domains/%s/credentials/%s',
212 2
                $domain,
213
                $login
214 2
            )
215 2
        );
216
217 2
        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($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($domain, $requireTLS, $noVerify)
247
    {
248 1
        Assert::stringNotEmpty($domain);
249 1
        Assert::nullOrBoolean($requireTLS);
250 1
        Assert::nullOrBoolean($noVerify);
251
252 1
        $params = [];
253
254 1
        if (null !== $requireTLS) {
255 1
            $params['require_tls'] = $requireTLS ? 'true' : 'false';
256 1
        }
257
258 1
        if (null !== $noVerify) {
259 1
            $params['skip_verification'] = $noVerify ? 'true' : 'false';
260 1
        }
261
262 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/connection', $domain), $params);
263
264 1
        return $this->hydrateResponse($response, UpdateConnectionResponse::class);
265
    }
266
}
267