Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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) |
|
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() |
|
123 | |||
124 | /** |
||
125 | * Request AWS to enable DKIM |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | 2 | View Code Duplication | public function enableDkim() |
144 | |||
145 | /** |
||
146 | * Request AWS To disable DKIM |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | 2 | View Code Duplication | public function disableDkim() |
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.