1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PeterNijssen\Ses\Manager; |
4
|
|
|
|
5
|
|
|
use Aws\Ses\SesClient; |
6
|
|
|
use PeterNijssen\Ses\Exception\InvalidFetchStatusException; |
7
|
|
|
use PeterNijssen\Ses\Model\Dns; |
8
|
|
|
use PeterNijssen\Ses\Model\IdentityInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This service manages AWS SES |
12
|
|
|
*/ |
13
|
|
|
abstract class SesManager |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var SesClient |
17
|
|
|
*/ |
18
|
|
|
protected $sesClient; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var IdentityInterface |
22
|
|
|
*/ |
23
|
|
|
protected $identity; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param SesClient $sesClient |
29
|
|
|
* @param IdentityInterface $identity |
30
|
|
|
*/ |
31
|
17 |
|
public function __construct(SesClient $sesClient, IdentityInterface $identity) |
32
|
|
|
{ |
33
|
17 |
|
$this->sesClient = $sesClient; |
34
|
17 |
|
$this->identity = $identity; |
35
|
17 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Fetch the current status |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
* |
42
|
|
|
* @throws InvalidFetchStatusException |
43
|
|
|
*/ |
44
|
2 |
View Code Duplication |
public function fetchStatus() |
|
|
|
|
45
|
|
|
{ |
46
|
2 |
|
$result = $this->sesClient->getIdentityVerificationAttributes( |
47
|
|
|
[ |
48
|
2 |
|
'Identities' => [$this->identity->getIdentity()], |
49
|
|
|
] |
50
|
2 |
|
); |
51
|
|
|
|
52
|
2 |
|
$status = $result->search("VerificationAttributes.*.VerificationStatus"); |
53
|
|
|
|
54
|
2 |
|
if (!is_array($status)) { |
55
|
|
|
throw new InvalidFetchStatusException("unable to fetch the status. Did you create the identity?"); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
return current($status); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Fetch the current DKIM status |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
* |
66
|
|
|
* @throws InvalidFetchStatusException |
67
|
|
|
*/ |
68
|
2 |
View Code Duplication |
public function fetchDkimStatus() |
|
|
|
|
69
|
|
|
{ |
70
|
2 |
|
$result = $this->sesClient->getIdentityDkimAttributes( |
71
|
|
|
[ |
72
|
2 |
|
'Identities' => [$this->identity->getIdentity()], |
73
|
|
|
] |
74
|
2 |
|
); |
75
|
|
|
|
76
|
2 |
|
$status = $result->search("DkimAttributes.*.DkimVerificationStatus"); |
77
|
|
|
|
78
|
2 |
|
if (!is_array($status)) { |
79
|
|
|
throw new InvalidFetchStatusException("unable to fetch the DKIM status. Did you create the identity?"); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return current($status); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the DNS records for the DKIM settings |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
2 |
|
public function fetchDkimRecords() |
91
|
|
|
{ |
92
|
2 |
|
$result = $this->sesClient->getIdentityDkimAttributes( |
93
|
|
|
[ |
94
|
2 |
|
'Identities' => [$this->identity->getIdentity()], |
95
|
|
|
] |
96
|
2 |
|
); |
97
|
|
|
|
98
|
2 |
|
$records = []; |
99
|
2 |
|
$results = $result->search("DkimAttributes.*.DkimTokens"); |
100
|
2 |
|
if (is_array($results)) { |
101
|
2 |
|
foreach (current($results) as $result) { |
102
|
2 |
|
$name = $result."._domainkey.".$this->identity->getDomain(); |
103
|
2 |
|
$value = $result.".dkim.amazonses.com"; |
104
|
|
|
|
105
|
2 |
|
$records[] = new Dns("CNAME", $name, $value); |
106
|
2 |
|
} |
107
|
2 |
|
} |
108
|
|
|
|
109
|
2 |
|
return $records; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Request AWS to verify DKIM |
114
|
|
|
*/ |
115
|
2 |
|
public function verifyDkim() |
116
|
|
|
{ |
117
|
2 |
|
$this->sesClient->verifyDomainDkim( |
118
|
|
|
[ |
119
|
2 |
|
'Domain' => $this->identity->getDomain(), |
120
|
|
|
] |
121
|
2 |
|
); |
122
|
2 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Request AWS to enable DKIM |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
2 |
View Code Duplication |
public function enableDkim() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
try { |
132
|
2 |
|
$this->sesClient->setIdentityDkimEnabled( |
133
|
|
|
[ |
134
|
2 |
|
'DkimEnabled' => true, |
135
|
2 |
|
'Identity' => $this->identity->getIdentity(), |
136
|
|
|
] |
137
|
2 |
|
); |
138
|
|
|
|
139
|
2 |
|
return true; |
140
|
|
|
} catch (\Exception $e) { |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Request AWS To disable DKIM |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
2 |
View Code Duplication |
public function disableDkim() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
try { |
153
|
2 |
|
$this->sesClient->setIdentityDkimEnabled( |
154
|
|
|
[ |
155
|
2 |
|
'DkimEnabled' => false, |
156
|
2 |
|
'Identity' => $this->identity->getIdentity(), |
157
|
|
|
] |
158
|
2 |
|
); |
159
|
|
|
|
160
|
2 |
|
return true; |
161
|
|
|
} catch (\Exception $e) { |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
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.