Completed
Push — master ( 4a903b...b725ab )
by Tobias
03:23 queued 53s
created

Domain::credentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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(int $limit = 100, int $skip = 0)
42
    {
43
        $params = [
44 1
            'limit' => $limit,
45 1
            'skip' => $skip,
46
        ];
47
48 1
        $response = $this->httpGet('/v3/domains', $params);
49
50 1
        return $this->hydrateResponse($response, IndexResponse::class);
51
    }
52
53
    /**
54
     * Returns a single domain.
55
     *
56
     * @param string $domain name of the domain
57
     *
58
     * @return ShowResponse|array|ResponseInterface
59
     */
60
    public function show(string $domain)
61
    {
62
        Assert::stringNotEmpty($domain);
63
64
        $response = $this->httpGet(sprintf('/v3/domains/%s', $domain));
65
66
        return $this->hydrateResponse($response, ShowResponse::class);
67
    }
68
69
    /**
70
     * Creates a new domain for the account.
71
     * See below for spam filtering parameter information.
72
     * {@link https://documentation.mailgun.com/user_manual.html#um-spam-filter}.
73
     *
74
     * @see https://documentation.mailgun.com/en/latest/api-domains.html#domains
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
    public function create(string $domain, string $smtpPass = null, string $spamAction = null, bool $wildcard = null)
84
    {
85
        Assert::stringNotEmpty($domain);
86
87
        $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...
88
89
        // If at least smtpPass available, check for the fields spamAction wildcard
90
        if (!empty($smtpPass)) {
91
            // TODO(sean.johnson): Extended spam filter input validation.
92
            Assert::stringNotEmpty($spamAction);
93
            Assert::boolean($wildcard);
94
95
            $params['smtp_password'] = $smtpPass;
96
            $params['spam_action'] = $spamAction;
97
        }
98
99
        $response = $this->httpPost('/v3/domains', $params);
100
101
        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
    public function delete(string $domain)
113
    {
114
        Assert::stringNotEmpty($domain);
115
116
        $response = $this->httpDelete(sprintf('/v3/domains/%s', $domain));
117
118
        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
    public function credentials(string $domain, int $limit = 100, int $skip = 0)
131
    {
132
        Assert::stringNotEmpty($domain);
133
        $params = [
134
            'limit' => $limit,
135
            'skip' => $skip,
136
        ];
137
138
        $response = $this->httpGet(sprintf('/v3/domains/%s/credentials', $domain), $params);
139
140
        return $this->hydrateResponse($response, CredentialResponse::class);
141
    }
142
143
    /**
144
     * Create a new SMTP credential pair for the specified domain.
145
     *
146
     * @param string $domain   name of the domain
147
     * @param string $login    SMTP Username
148
     * @param string $password SMTP Password. Length min 5, max 32.
149
     *
150
     * @return CreateCredentialResponse|array|ResponseInterface
151
     */
152
    public function createCredential(string $domain, string $login, string $password)
153
    {
154
        Assert::stringNotEmpty($domain);
155
        Assert::stringNotEmpty($login);
156
        Assert::stringNotEmpty($password);
157
        Assert::lengthBetween($password, 5, 32, 'SMTP password must be between 5 and 32 characters.');
158
159
        $params = [
160
            'login' => $login,
161
            'password' => $password,
162
        ];
163
164
        $response = $this->httpPost(sprintf('/v3/domains/%s/credentials', $domain), $params);
165
166
        return $this->hydrateResponse($response, CreateCredentialResponse::class);
167
    }
168
169
    /**
170
     * Update a set of SMTP credentials for the specified domain.
171
     *
172
     * @param string $domain name of the domain
173
     * @param string $login  SMTP Username
174
     * @param string $pass   New SMTP Password. Length min 5, max 32.
175
     *
176
     * @return UpdateCredentialResponse|array|ResponseInterface
177
     */
178
    public function updateCredential(string $domain, string $login, string $pass)
179
    {
180
        Assert::stringNotEmpty($domain);
181
        Assert::stringNotEmpty($login);
182
        Assert::stringNotEmpty($pass);
183
        Assert::lengthBetween($pass, 5, 32, 'SMTP password must be between 5 and 32 characters.');
184
185
        $params = [
186
            'password' => $pass,
187
        ];
188
189
        $response = $this->httpPut(sprintf('/v3/domains/%s/credentials/%s', $domain, $login), $params);
190
191
        return $this->hydrateResponse($response, UpdateCredentialResponse::class);
192
    }
193
194
    /**
195
     * Remove a set of SMTP credentials from the specified domain.
196
     *
197
     * @param string $domain name of the domain
198
     * @param string $login  SMTP Username
199
     *
200
     * @return DeleteCredentialResponse|array|ResponseInterface
201
     */
202
    public function deleteCredential(string $domain, string $login)
203
    {
204
        Assert::stringNotEmpty($domain);
205
        Assert::stringNotEmpty($login);
206
207
        $response = $this->httpDelete(
208
            sprintf(
209
                '/v3/domains/%s/credentials/%s',
210
                $domain,
211
                $login
212
            )
213
        );
214
215
        return $this->hydrateResponse($response, DeleteCredentialResponse::class);
216
    }
217
218
    /**
219
     * Returns delivery connection settings for the specified domain.
220
     *
221
     * @param string $domain name of the domain
222
     *
223
     * @return ConnectionResponse|ResponseInterface
224
     */
225
    public function connection(string $domain)
226
    {
227
        Assert::stringNotEmpty($domain);
228
229
        $response = $this->httpGet(sprintf('/v3/domains/%s/connection', $domain));
230
231
        return $this->hydrateResponse($response, ConnectionResponse::class);
232
    }
233
234
    /**
235
     * Updates the specified delivery connection settings for the specified domain.
236
     * If a parameter is passed in as null, it will not be updated.
237
     *
238
     * @param string    $domain     name of the domain
239
     * @param bool|null $requireTLS enforces that messages are sent only over a TLS connection
240
     * @param bool|null $noVerify   disables TLS certificate and hostname verification
241
     *
242
     * @return UpdateConnectionResponse|array|ResponseInterface
243
     */
244
    public function updateConnection(string $domain, ?bool $requireTLS, ?bool $noVerify)
245
    {
246
        Assert::stringNotEmpty($domain);
247
        $params = [];
248
249
        if (null !== $requireTLS) {
250
            $params['require_tls'] = $requireTLS ? 'true' : 'false';
251
        }
252
253
        if (null !== $noVerify) {
254
            $params['skip_verification'] = $noVerify ? 'true' : 'false';
255
        }
256
257
        $response = $this->httpPut(sprintf('/v3/domains/%s/connection', $domain), $params);
258
259
        return $this->hydrateResponse($response, UpdateConnectionResponse::class);
260
    }
261
262
    /**
263
     * Returns a single domain.
264
     *
265
     * @param string $domain name of the domain
266
     *
267
     * @return VerifyResponse|array|ResponseInterface
268
     */
269
    public function verify(string $domain)
270
    {
271
        Assert::stringNotEmpty($domain);
272
273
        $response = $this->httpPut(sprintf('/v3/domains/%s/verify', $domain));
274
275
        return $this->hydrateResponse($response, VerifyResponse::class);
276
    }
277
}
278