Completed
Push — master ( 1a9a95...4ed083 )
by Gaël
10:07
created

MailjetDriver   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 249
Duplicated Lines 32.93 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 82
loc 249
c 0
b 0
f 0
wmc 25
lcom 1
cbo 6
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A subscribe() 0 20 2
A subscribeOrUpdate() 0 4 1
A addMember() 0 6 1
A getMembers() 0 19 2
A getMember() 0 11 2
A hasMember() 21 21 4
A isSubscribed() 21 21 5
A unsubscribe() 18 18 2
A delete() 22 22 2
A getLastError() 0 4 1
A getApi() 0 4 1

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 DansMaCulotte\Newsletter\Drivers;
4
5
use Mailjet\Client;
6
use Mailjet\Response;
7
use DansMaCulotte\Newsletter\Exceptions\ApiError;
8
use DansMaCulotte\Newsletter\Exceptions\InvalidNewsletterList;
9
use DansMaCulotte\Newsletter\NewsletterListCollection;
10
use Mailjet\Resources;
11
12
class MailjetDriver implements Driver
13
{
14
    /** @var Client */
15
    public $client;
16
17
    /** @var NewsletterListCollection */
18
    public $lists;
19
20
    /** @var array */
21
    private $lastError;
22
23
    public function __construct(array $credentials, array $config)
24
    {
25
        $this->client = new Client($credentials['key'], $credentials['secret']);
26
        $connectionTimeout = $config['connection_timeout'];
27
        if ($connectionTimeout) {
28
            $this->client->setConnectionTimeout($connectionTimeout);
29
        }
30
        $this->lists = NewsletterListCollection::createFromConfig($config);
31
    }
32
33
    /**
34
     * @param string $email
35
     * @param array $options
36
     * @param string $listName
37
     * @return Response
38
     * @throws ApiError
39
     * @throws InvalidNewsletterList
40
     */
41
    public function subscribe(string $email, array $options = [], string $listName = '')
42
    {
43
        $list = $this->lists->findByName($listName);
44
45
        $body = [
46
            'Email' => $email,
47
            'Action' => 'addnoforce',
48
        ];
49
50
        $body = array_merge($body, $options);
51
52
        $response = $this->client->post(Resources::$ContactslistManagecontact, ['id' => $list->getId(), 'body' => $body]);
53
54
        if (! $response->success()) {
55
            $this->lastError = $response->getData();
56
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
57
        }
58
59
        return $response;
60
    }
61
62
    /**
63
     * @param string $email
64
     * @param array $options
65
     * @param string $listName
66
     * @return Response
67
     * @throws ApiError
68
     * @throws InvalidNewsletterList
69
     */
70
    public function subscribeOrUpdate(string $email, array $options = [], string $listName = '')
71
    {
72
        return $this->subscribe($email, $options, $listName);
73
    }
74
75
    /**
76
     * @param string $email
77
     * @param array $options
78
     * @param string $listName
79
     * @return Response
80
     * @throws ApiError
81
     * @throws InvalidNewsletterList
82
     */
83
    public function addMember(string $email, array $options = [], string $listName = '')
84
    {
85
        return $this->subscribe($email, array_merge([
86
            'IsExcludedFromCampaigns' => true,
87
        ], $options), $listName);
88
    }
89
90
    /**
91
     * @param string $listName
92
     * @param array $parameters
93
     * @return array
94
     * @throws ApiError
95
     * @throws InvalidNewsletterList
96
     */
97
    public function getMembers(string $listName = '', array $parameters = [])
98
    {
99
        $listId = $this->lists->findByName($listName)->getId();
100
101
        $body = [
102
            'ContactsList' => $listId
103
        ];
104
105
        $body = array_merge($body, $parameters);
106
107
        $response = $this->client->get(Resources::$Contact, $body);
108
109
        if (! $response->success()) {
110
            $this->lastError = $response->getData();
111
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
112
        }
113
114
        return $response->getData();
115
    }
116
117
    /**
118
     * @param string $email
119
     * @param string $listName
120
     * @return array
121
     * @throws ApiError
122
     */
123
    public function getMember(string $email, string $listName = '')
124
    {
125
        $response = $this->client->get(Resources::$Contact, ['id' => $email]);
126
127
        if (! $response->success()) {
128
            $this->lastError = $response->getData();
129
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
130
        }
131
132
        return $response->getData();
133
    }
134
135
136
    /**
137
     * @param string $email
138
     * @param string $listName
139
     * @return bool
140
     * @throws ApiError
141
     * @throws InvalidNewsletterList
142
     */
143 View Code Duplication
    public function hasMember(string $email, string $listName = ''): bool
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...
144
    {
145
        $listId = $this->lists->findByName($listName)->getId();
146
147
        $response = $this->client->get(Resources::$Contact, ['ContactsList' => $listId]);
148
149
        if (! $response->success()) {
150
            $this->lastError = $response->getData();
151
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
152
        }
153
154
        $contacts = $response->getData();
155
156
        foreach ($contacts as $contact) {
157
            if ($contact['Email'] === $email) {
158
                return true;
159
            }
160
        }
161
162
        return false;
163
    }
164
165
    /**
166
     * @param string $email
167
     * @param string $listName
168
     * @return bool
169
     * @throws ApiError
170
     * @throws InvalidNewsletterList
171
     */
172 View Code Duplication
    public function isSubscribed(string $email, string $listName = ''): bool
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...
173
    {
174
        $listId = $this->lists->findByName($listName)->getId();
175
176
        $response = $this->client->get(Resources::$ContactGetcontactslists, ['id' => $email]);
177
178
        if (! $response->success()) {
179
            $this->lastError = $response->getData();
180
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
181
        }
182
183
        $contactLists = $response->getData();
184
185
        foreach ($contactLists as $list) {
186
            if ($list['ListID'] === (int) $listId && $list['IsUnsub'] === false) {
187
                return true;
188
            }
189
        }
190
191
        return false;
192
    }
193
194
    /**
195
     * @param string $email
196
     * @param string $listName
197
     * @return Response
198
     * @throws ApiError
199
     * @throws InvalidNewsletterList
200
     */
201 View Code Duplication
    public function unsubscribe(string $email, string $listName = '')
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...
202
    {
203
        $list = $this->lists->findByName($listName);
204
205
        $body = [
206
            'Email' => $email,
207
            'Action' => 'unsub',
208
        ];
209
210
        $response = $this->client->post(Resources::$ContactslistManagecontact, ['id' => $list->getId(), 'body' => $body]);
211
212
        if (! $response->success()) {
213
            $this->lastError = $response->getData();
214
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
215
        }
216
217
        return $response;
218
    }
219
220
221
    /**
222
     * @param string $email
223
     * @param string $listName
224
     * @return Response
225
     * @throws ApiError
226
     * @throws InvalidNewsletterList
227
     */
228 View Code Duplication
    public function delete(string $email, string $listName = '')
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...
229
    {
230
        $list = $this->lists->findByName($listName);
231
232
        $body = [
233
            'Email' => $email,
234
            'Action' => 'remove',
235
        ];
236
237
        $response = $this->client->post(Resources::$ContactslistManagecontact, [
238
                'id' => $list->getId(),
239
                'body' => $body
240
            ]
241
        );
242
243
        if (! $response->success()) {
244
            $this->lastError = $response->getData();
245
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
246
        }
247
248
        return $response;
249
    }
250
251
    public function getLastError()
252
    {
253
        return $this->lastError;
254
    }
255
256
    public function getApi()
257
    {
258
        return $this->client;
259
    }
260
}
261