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\Suppression; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Mailgun\Api\HttpApi; |
16
|
|
|
use Mailgun\Api\Pagination; |
17
|
|
|
use Mailgun\Assert; |
18
|
|
|
use Mailgun\Model\Suppression\Whitelist\CreateResponse; |
19
|
|
|
use Mailgun\Model\Suppression\Whitelist\DeleteAllResponse; |
20
|
|
|
use Mailgun\Model\Suppression\Whitelist\DeleteResponse; |
21
|
|
|
use Mailgun\Model\Suppression\Whitelist\ImportResponse; |
22
|
|
|
use Mailgun\Model\Suppression\Whitelist\IndexResponse; |
23
|
|
|
use Mailgun\Model\Suppression\Whitelist\ShowResponse; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @see https://documentation.mailgun.com/en/latest/api-suppressions.html#whitelists |
27
|
|
|
* |
28
|
|
|
* @author Artem Bondarenko <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class Whitelist extends HttpApi |
31
|
|
|
{ |
32
|
|
|
use Pagination; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $domain Domain to get whitelist for |
36
|
|
|
* @param int $limit optional |
37
|
|
|
* |
38
|
|
|
* @return IndexResponse |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function index(string $domain, int $limit = 100) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
Assert::stringNotEmpty($domain); |
43
|
|
|
Assert::range($limit, 1, 10000, 'Limit parameter must be between 1 and 10000'); |
44
|
|
|
|
45
|
|
|
$params = [ |
46
|
|
|
'limit' => $limit, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
$response = $this->httpGet(sprintf('/v3/%s/whitelists', $domain), $params); |
50
|
|
|
|
51
|
|
|
return $this->hydrateResponse($response, IndexResponse::class); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $domain Domain to show whitelist for |
56
|
|
|
* @param string $address whitelist address |
57
|
|
|
* |
58
|
|
|
* @return ShowResponse |
59
|
|
|
*/ |
60
|
|
|
public function show(string $domain, string $address) |
61
|
|
|
{ |
62
|
|
|
Assert::stringNotEmpty($domain); |
63
|
|
|
Assert::stringNotEmpty($address); |
64
|
|
|
|
65
|
|
|
$response = $this->httpGet(sprintf('/v3/%s/whitelists/%s', $domain, $address)); |
66
|
|
|
|
67
|
|
|
return $this->hydrateResponse($response, ShowResponse::class); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $domain Domain to create whitelist for |
72
|
|
|
* @param string $address whitelist email address or domain name |
73
|
|
|
* |
74
|
|
|
* @return CreateResponse |
75
|
|
|
*/ |
76
|
|
|
public function create(string $domain, string $address) |
77
|
|
|
{ |
78
|
|
|
Assert::stringNotEmpty($domain); |
79
|
|
|
Assert::stringNotEmpty($address); |
80
|
|
|
|
81
|
|
|
$params = []; |
82
|
|
|
|
83
|
|
|
if (false !== filter_var($address, FILTER_VALIDATE_EMAIL)) { |
84
|
|
|
$params['address'] = $address; |
85
|
|
|
} elseif (false !== filter_var($address, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { |
86
|
|
|
$params['domain'] = $address; |
87
|
|
|
} else { |
88
|
|
|
throw new InvalidArgumentException('Address should be valid email or domain name'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$response = $this->httpPost(sprintf('/v3/%s/whitelists', $domain), $params); |
92
|
|
|
|
93
|
|
|
return $this->hydrateResponse($response, CreateResponse::class); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $domain Domain to create whitelist for |
98
|
|
|
* @param string $filePath csv file path |
99
|
|
|
* |
100
|
|
|
* @return ImportResponse |
101
|
|
|
*/ |
102
|
|
|
public function import(string $domain, string $filePath) |
103
|
|
|
{ |
104
|
|
|
Assert::stringNotEmpty($domain); |
105
|
|
|
Assert::stringNotEmpty($filePath); |
106
|
|
|
Assert::fileExists($filePath); |
107
|
|
|
|
108
|
|
|
$response = $this->httpPost( |
109
|
|
|
sprintf('/v3/%s/whitelists/import', $domain), |
110
|
|
|
['file' => fopen($filePath, 'r')], |
111
|
|
|
[ |
112
|
|
|
'filename' => basename($filePath), |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
return $this->hydrateResponse($response, ImportResponse::class); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $domain Domain to delete whitelist for |
121
|
|
|
* @param string $address whitelist address |
122
|
|
|
* |
123
|
|
|
* @return DeleteResponse |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function delete(string $domain, string $address) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
Assert::stringNotEmpty($domain); |
128
|
|
|
Assert::stringNotEmpty($address); |
129
|
|
|
|
130
|
|
|
$response = $this->httpDelete(sprintf('/v3/%s/whitelists/%s', $domain, $address)); |
131
|
|
|
|
132
|
|
|
return $this->hydrateResponse($response, DeleteResponse::class); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $domain Domain to delete all whitelists for |
137
|
|
|
* |
138
|
|
|
* @return DeleteAllResponse |
139
|
|
|
*/ |
140
|
|
|
public function deleteAll(string $domain) |
141
|
|
|
{ |
142
|
|
|
Assert::stringNotEmpty($domain); |
143
|
|
|
|
144
|
|
|
$response = $this->httpDelete(sprintf('/v3/%s/whitelists', $domain)); |
145
|
|
|
|
146
|
|
|
return $this->hydrateResponse($response, DeleteAllResponse::class); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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.