Completed
Push — master ( 6f1f11...46af25 )
by Gaël
01:59
created

MailjetDriver   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 212
Duplicated Lines 36.79 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 78
loc 212
c 0
b 0
f 0
wmc 22
lcom 1
cbo 6
rs 10

10 Methods

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