Completed
Push — master ( fa02dd...1fd4f2 )
by Tobias
30:52 queued 08:25
created

Domain::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
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 2
     * @return CreateResponse|array|ResponseInterface
85
     */
86 2
    public function create($domain, $smtpPass = NULL, $spamAction = NULL, $wildcard = NULL)
87 2
    {
88
        Assert::stringNotEmpty($domain);
89 2
        
90 2
        $params = ['name'] = $domain;
91
        
92
        // If at least smtpPass available, check for the fields spamAction wildcard
93 2
        if(!empty($smtpPass) {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

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