SesManager   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 153
Duplicated Lines 40.52 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 89.09%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 5
dl 62
loc 153
ccs 49
cts 55
cp 0.8909
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A verifyDkim() 0 8 1
A enableDkim() 15 15 2
A disableDkim() 15 15 2
A fetchStatus() 16 16 2
A fetchDkimStatus() 16 16 2
A fetchDkimRecords() 0 21 3

How to fix   Duplicated Code   

Duplicated Code

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
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()
0 ignored issues
show
Duplication introduced by
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...
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()
0 ignored issues
show
Duplication introduced by
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...
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()
0 ignored issues
show
Duplication introduced by
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...
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()
0 ignored issues
show
Duplication introduced by
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...
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