Completed
Push — master ( 43a76d...75ce5f )
by David
01:41
created

src/Api/EmailValidationV4.php (9 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2013 Mailgun
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license. See the LICENSE file for details.
10
 */
11
12
namespace Mailgun\Api;
13
14
use Exception;
15
use Mailgun\Assert;
16
use Mailgun\Model\EmailValidationV4\CreateBulkJobResponse;
17
use Mailgun\Model\EmailValidationV4\CreateBulkPreviewResponse;
18
use Mailgun\Model\EmailValidationV4\DeleteBulkJobResponse;
19
use Mailgun\Model\EmailValidationV4\GetBulkJobResponse;
20
use Mailgun\Model\EmailValidationV4\GetBulkJobsResponse;
21
use Mailgun\Model\EmailValidationV4\GetBulkPreviewResponse;
22
use Mailgun\Model\EmailValidationV4\GetBulkPreviewsResponse;
23
use Mailgun\Model\EmailValidationV4\PromoteBulkPreviewResponse;
24
use Mailgun\Model\EmailValidationV4\ValidateResponse;
25
use Psr\Http\Message\ResponseInterface;
26
27
/**
28
 * @see https://documentation.mailgun.com/en/latest/api-email-validation.html
29
 */
30
class EmailValidationV4 extends HttpApi
31
{
32
    /**
33
     * Addresses are validated based off defined checks.
34
     *
35
     * @param string $address        An email address to validate. Maximum: 512 characters.
36
     * @param bool   $providerLookup A provider lookup will be performed if Mailgun’s internal analysis is insufficient
37
     *
38
     * @return ValidateResponse|ResponseInterface
39
     *
40
     * @throws Exception Thrown when we don't catch a Client or Server side Exception
41
     */
42 2 View Code Duplication
    public function validate(string $address, bool $providerLookup = true)
0 ignored issues
show
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...
43
    {
44 2
        Assert::stringNotEmpty($address);
45
46
        $params = [
47 2
            'address' => $address,
48 2
            'provider_lookup' => $providerLookup,
49
        ];
50
51 2
        $response = $this->httpGet('/v4/address/validate', $params);
52
53
        return $this->hydrateResponse($response, ValidateResponse::class);
54
    }
55
56
    /**
57
     * @param string $listId   ID given when the list created
58
     * @param mixed  $filePath File path or file content
59
     *
60
     * @return mixed|ResponseInterface
61
     *
62
     * @throws Exception
63
     */
64 1 View Code Duplication
    public function createBulkJob(string $listId, $filePath)
0 ignored issues
show
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...
65
    {
66 1
        Assert::stringNotEmpty($listId);
67
68 1
        if (strlen($filePath) < PHP_MAXPATHLEN && is_file($filePath)) {
69 1
            $fileData = ['filePath' => $filePath];
70
        } else {
71
            $fileData = [
72
                'fileContent' => $filePath,
73
                'filename' => 'file',
74
            ];
75
        }
76
77 1
        $postDataMultipart = [];
78 1
        $postDataMultipart[] = $this->prepareFile('file', $fileData);
79
80 1
        $response = $this->httpPostRaw(sprintf('/v4/address/validate/bulk/%s', $listId), $postDataMultipart);
81 1
        $this->closeResources($postDataMultipart);
82
83
        return $this->hydrateResponse($response, CreateBulkJobResponse::class);
84
    }
85
86
    /**
87
     * @param string $listId ID given when the list created
88
     *
89
     * @return DeleteBulkJobResponse|ResponseInterface
90
     *
91
     * @throws Exception
92
     */
93 1 View Code Duplication
    public function deleteBulkJob(string $listId)
0 ignored issues
show
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...
94
    {
95 1
        Assert::stringNotEmpty($listId);
96
97 1
        $response = $this->httpDelete(sprintf('/v4/address/validate/bulk/%s', $listId));
98
99
        return $this->hydrateResponse($response, DeleteBulkJobResponse::class);
100
    }
101
102
    /**
103
     * @param string $listId ID given when the list created
104
     *
105
     * @return GetBulkJobResponse|ResponseInterface
106
     *
107
     * @throws Exception
108
     */
109 1 View Code Duplication
    public function getBulkJob(string $listId)
0 ignored issues
show
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...
110
    {
111 1
        Assert::stringNotEmpty($listId);
112
113 1
        $response = $this->httpGet(sprintf('/v4/address/validate/bulk/%s', $listId));
114
115
        return $this->hydrateResponse($response, GetBulkJobResponse::class);
116
    }
117
118
    /**
119
     * @param int $limit Jobs limit
120
     *
121
     * @return GetBulkJobsResponse|ResponseInterface
122
     *
123
     * @throws Exception
124
     */
125 1 View Code Duplication
    public function getBulkJobs(int $limit = 500)
0 ignored issues
show
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...
126
    {
127 1
        Assert::greaterThan($limit, 0);
128
129 1
        $response = $this->httpGet('/v4/address/validate/bulk', [
130 1
            'limit' => $limit,
131
        ]);
132
133
        return $this->hydrateResponse($response, GetBulkJobsResponse::class);
134
    }
135
136
    /**
137
     * @param int $limit Previews Limit
138
     *
139
     * @return mixed|ResponseInterface
140
     *
141
     * @throws Exception
142
     */
143 1 View Code Duplication
    public function getBulkPreviews(int $limit = 500)
0 ignored issues
show
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...
144
    {
145 1
        Assert::greaterThan($limit, 0);
146
147 1
        $response = $this->httpGet('/v4/address/validate/preview', [
148 1
            'limit' => $limit,
149
        ]);
150
151
        return $this->hydrateResponse($response, GetBulkPreviewsResponse::class);
152
    }
153
154
    /**
155
     * @param string $previewId ID given when the list created
156
     * @param mixed  $filePath  File path or file content
157
     *
158
     * @return mixed|ResponseInterface
159
     *
160
     * @throws Exception
161
     */
162 1 View Code Duplication
    public function createBulkPreview(string $previewId, $filePath)
0 ignored issues
show
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...
163
    {
164 1
        Assert::stringNotEmpty($previewId);
165
166 1
        if (strlen($filePath) < PHP_MAXPATHLEN && is_file($filePath)) {
167 1
            $fileData = ['filePath' => $filePath];
168
        } else {
169
            $fileData = [
170
                'fileContent' => $filePath,
171
                'filename' => 'file',
172
            ];
173
        }
174
175 1
        $postDataMultipart = [];
176 1
        $postDataMultipart[] = $this->prepareFile('file', $fileData);
177
178 1
        $response = $this->httpPostRaw(sprintf('/v4/address/validate/preview/%s', $previewId), $postDataMultipart);
179 1
        $this->closeResources($postDataMultipart);
180
181
        return $this->hydrateResponse($response, CreateBulkPreviewResponse::class);
182
    }
183
184
    /**
185
     * @param string $previewId ID given when the list created
186
     *
187
     * @return mixed|ResponseInterface
188
     *
189
     * @throws Exception
190
     */
191 1 View Code Duplication
    public function getBulkPreview(string $previewId)
0 ignored issues
show
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...
192
    {
193 1
        Assert::stringNotEmpty($previewId);
194
195 1
        $response = $this->httpGet(sprintf('/v4/address/validate/preview/%s', $previewId));
196
197
        return $this->hydrateResponse($response, GetBulkPreviewResponse::class);
198
    }
199
200
    /**
201
     * @param string $previewId ID given when the list created
202
     *
203
     * @return bool
204
     */
205 1
    public function deleteBulkPreview(string $previewId)
206
    {
207 1
        Assert::stringNotEmpty($previewId);
208
209 1
        $response = $this->httpDelete(sprintf('/v4/address/validate/preview/%s', $previewId));
210
211 1
        return 204 === $response->getStatusCode();
212
    }
213
214
    /**
215
     * @param string $previewId ID given when the list created
216
     *
217
     * @return mixed|ResponseInterface
218
     *
219
     * @throws Exception
220
     */
221 1
    public function promoteBulkPreview(string $previewId)
222
    {
223 1
        Assert::stringNotEmpty($previewId);
224
225 1
        $response = $this->httpPut(sprintf('/v4/address/validate/preview/%s', $previewId));
226
227
        return $this->hydrateResponse($response, PromoteBulkPreviewResponse::class);
228
    }
229
230
    /**
231
     * @param string $fieldName Field Name
232
     * @param array  $filePath  ['fileContent' => 'content'] or ['filePath' => '/foo/bar']
233
     *
234
     * @return array File Data
235
     */
236 2
    private function prepareFile(string $fieldName, array $filePath): array
237
    {
238 2
        $filename = isset($filePath['filename']) ? $filePath['filename'] : null;
239
240 2
        $resource = null;
241
242 2
        if (isset($filePath['fileContent'])) {
243
            // File from memory
244
            $resource = fopen('php://temp', 'r+');
245
            fwrite($resource, $filePath['fileContent']);
246
            rewind($resource);
247 2
        } elseif (isset($filePath['filePath'])) {
248
            // File form path
249 2
            $path = $filePath['filePath'];
250 2
            $resource = fopen($path, 'r');
251
        }
252
253
        return [
254 2
            'name' => $fieldName,
255 2
            'content' => $resource,
256 2
            'filename' => $filename,
257
        ];
258
    }
259
260
    /**
261
     * Close open resources.
262
     *
263
     * @param array $params Resource params
264
     */
265 2 View Code Duplication
    private function closeResources(array $params): void
0 ignored issues
show
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...
266
    {
267 2
        foreach ($params as $param) {
268 2
            if (is_array($param) && array_key_exists('content', $param) && is_resource($param['content'])) {
269 2
                fclose($param['content']);
270
            }
271
        }
272 2
    }
273
}
274