Completed
Push — master ( f9691c...4f8ea6 )
by Gaël
14:13
created

MailjetDriver::addMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 3
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 $email
70
     * @param array $options
71
     * @param string $listName
72
     * @return Response
73
     * @throws ApiError
74
     * @throws InvalidNewsletterList
75
     */
76
    public function addMember(string $email, array $options = [], string $listName = '')
77
    {
78
        return $this->subscribe($email, array_merge([
79
            'IsExcludedFromCampaigns' => true,
80
        ], $options), $listName);
81
    }
82
83
    /**
84
     * @param string $listName
85
     * @param array $parameters
86
     * @return array
87
     * @throws ApiError
88
     * @throws InvalidNewsletterList
89
     */
90
    public function getMembers(string $listName = '', array $parameters = [])
91
    {
92
        $listId = $this->lists->findByName($listName)->getId();
93
94
        $body = [
95
            'ContactsList' => $listId
96
        ];
97
98
        $body = array_merge($body, $parameters);
99
100
        $response = $this->client->get(Resources::$Contact, $body);
101
102
        if (! $response->success()) {
103
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
104
        }
105
106
        return $response->getData();
107
    }
108
109
    /**
110
     * @param string $email
111
     * @param string $listName
112
     * @return array
113
     * @throws ApiError
114
     */
115
    public function getMember(string $email, string $listName = '')
116
    {
117
        $response = $this->client->get(Resources::$Contact, ['id' => $email]);
118
119
        if (! $response->success()) {
120
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
121
        }
122
123
        return $response->getData();
124
    }
125
126
127
    /**
128
     * @param string $email
129
     * @param string $listName
130
     * @return bool
131
     * @throws ApiError
132
     * @throws InvalidNewsletterList
133
     */
134 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...
135
    {
136
        $listId = $this->lists->findByName($listName)->getId();
137
138
        $response = $this->client->get(Resources::$Contact, ['ContactsList' => $listId]);
139
140
        if (! $response->success()) {
141
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
142
        }
143
144
        $contacts = $response->getData();
145
146
        foreach ($contacts as $contact) {
147
            if ($contact['Email'] === $email) {
148
                return true;
149
            }
150
        }
151
152
        return false;
153
    }
154
155
    /**
156
     * @param string $email
157
     * @param string $listName
158
     * @return bool
159
     * @throws ApiError
160
     * @throws InvalidNewsletterList
161
     */
162 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...
163
    {
164
        $listId = $this->lists->findByName($listName)->getId();
165
166
        $response = $this->client->get(Resources::$ContactGetcontactslists, ['id' => $email]);
167
168
        if (! $response->success()) {
169
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
170
        }
171
172
        $contactLists = $response->getData();
173
174
        foreach ($contactLists as $list) {
175
            if ($list['ListID'] === $listId && $list['IsUnsub'] !== true) {
176
                return true;
177
            }
178
        }
179
180
        return false;
181
    }
182
183
    /**
184
     * @param string $email
185
     * @param string $listName
186
     * @return Response
187
     * @throws ApiError
188
     * @throws InvalidNewsletterList
189
     */
190 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...
191
    {
192
        $list = $this->lists->findByName($listName);
193
194
        $body = [
195
            'Email' => $email,
196
            'Action' => 'unsub',
197
        ];
198
199
        $response = $this->client->post(Resources::$ContactslistManagecontact, ['id' => $list->getId(), 'body' => $body]);
200
201
        if (! $response->success()) {
202
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
203
        }
204
205
        return $response;
206
    }
207
208
209
    /**
210
     * @param string $email
211
     * @param string $listName
212
     * @return Response
213
     * @throws ApiError
214
     * @throws InvalidNewsletterList
215
     */
216 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...
217
    {
218
        $list = $this->lists->findByName($listName);
219
220
        $body = [
221
            'Email' => $email,
222
            'Action' => 'remove',
223
        ];
224
225
        $response = $this->client->post(Resources::$ContactslistManagecontact, [
226
                'id' => $list->getId(),
227
                'body' => $body
228
            ]
229
        );
230
231
        if (! $response->success()) {
232
            throw ApiError::responseError($response->getReasonPhrase(), 'mailjet', $response->getStatus());
233
        }
234
235
        return $response;
236
    }
237
238
    public function getApi()
239
    {
240
        return $this->client;
241
    }
242
}
243