MailjetDriver::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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