Completed
Push — master ( a7dad9...a6210e )
by Freek
01:11
created

Newsletter::isSubscribed()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace Spatie\Newsletter;
4
5
use DrewM\MailChimp\MailChimp;
6
7
class Newsletter
8
{
9
    /** @var \DrewM\MailChimp\MailChimp */
10
    protected $mailChimp;
11
12
    /** * @var \Spatie\Newsletter\NewsletterListCollection */
13
    protected $lists;
14
15
    /**
16
     * @param \DrewM\MailChimp\MailChimp                  $mailChimp
17
     * @param \Spatie\Newsletter\NewsletterListCollection $lists
18
     */
19
    public function __construct(MailChimp $mailChimp, NewsletterListCollection $lists)
20
    {
21
        $this->mailChimp = $mailChimp;
22
23
        $this->lists = $lists;
24
    }
25
26
    /**
27
     * @param string $email
28
     * @param array  $mergeFields
29
     * @param string $listName
30
     * @param array  $options
31
     *
32
     * @return array|bool
33
     *
34
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
35
     */
36 View Code Duplication
    public function subscribe($email, $mergeFields = [], $listName = '', $options = [])
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...
37
    {
38
        $list = $this->lists->findByName($listName);
39
40
        $options = $this->getSubscriptionOptions($email, $mergeFields, $options);
41
42
        $response = $this->mailChimp->post("lists/{$list->getId()}/members", $options);
43
44
        if (! $this->lastActionSucceeded()) {
45
            return false;
46
        }
47
48
        return $response;
49
    }
50
51
    /**
52
     * @param string $email
53
     * @param array  $mergeFields
54
     * @param string $listName
55
     * @param array  $options
56
     *
57
     * @return array|bool
58
     *
59
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
60
     */
61 View Code Duplication
    public function subscribeOrUpdate($email, $mergeFields = [], $listName = '', $options = [])
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...
62
    {
63
        $list = $this->lists->findByName($listName);
64
65
        $options = $this->getSubscriptionOptions($email, $mergeFields, $options);
66
67
        $response = $this->mailChimp->put("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", $options);
68
69
        if (! $this->lastActionSucceeded()) {
70
            return false;
71
        }
72
73
        return $response;
74
    }
75
76
    /**
77
     * @param string $listName
78
     *
79
     * @param array $parameters
80
     * @return array|bool
81
     */
82
    public function getMembers($listName = '', $parameters = [])
83
    {
84
        $list = $this->lists->findByName($listName);
85
86
        return $this->mailChimp->get("lists/{$list->getId()}/members", $parameters);
87
    }
88
89
    /**
90
     * @param string $email
91
     * @param string $listName
92
     *
93
     * @return array|bool
94
     *
95
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
96
     */
97
    public function getMember($email, $listName = '')
98
    {
99
        $list = $this->lists->findByName($listName);
100
101
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
102
    }
103
104
    /**
105
     * @param string $email
106
     * @param string $listName
107
     *
108
     * @return array|bool
109
     *
110
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
111
     */
112
    public function getMemberActivity($email, $listName = '')
113
    {
114
        $list = $this->lists->findByName($listName);
115
116
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/activity");
117
    }
118
119
    /**
120
     * @param string $email
121
     * @param string $listName
122
     *
123
     * @return bool
124
     */
125
    public function hasMember($email, $listName = '')
126
    {
127
        $response = $this->getMember($email, $listName);
128
129
        if (! isset($response['email_address'])) {
130
            return false;
131
        }
132
133
        if (strtolower($response['email_address']) != strtolower($email)) {
134
            return false;
135
        }
136
137
        return true;
138
    }
139
140
    /**
141
     * @param string $email
142
     * @param string $listName
143
     *
144
     * @return bool
145
     */
146
    public function isSubscribed($email, $listName = '')
147
    {
148
        $response = $this->getMember($email, $listName);
149
150
        if (! isset($response)) {
151
            return false;
152
        }
153
154
        if ($response['status'] != 'subscribed') {
155
            return false;
156
        }
157
158
        return true;
159
    }
160
161
    /**
162
     * @param $email
163
     * @param string $listName
164
     *
165
     * @return array|false
166
     *
167
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
168
     */
169 View Code Duplication
    public function unsubscribe($email, $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...
170
    {
171
        $list = $this->lists->findByName($listName);
172
173
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", [
174
            'status' => 'unsubscribed',
175
        ]);
176
177
        return $response;
178
    }
179
180
    /**
181
     * Update the email address of an existing list member.
182
     *
183
     * @param string $currentEmailAddress
184
     * @param string $newEmailAddress
185
     * @param string $listName
186
     *
187
     * @return array|false
188
     *
189
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
190
     */
191 View Code Duplication
    public function updateEmailAddress($currentEmailAddress, $newEmailAddress, $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...
192
    {
193
        $list = $this->lists->findByName($listName);
194
195
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($currentEmailAddress)}", [
196
            'email_address' => $newEmailAddress,
197
        ]);
198
199
        return $response;
200
    }
201
202
    /**
203
     * @param $email
204
     * @param string $listName
205
     *
206
     * @return array|false
207
     *
208
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
209
     */
210
    public function delete($email, $listName = '')
211
    {
212
        $list = $this->lists->findByName($listName);
213
214
        $response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
215
216
        return $response;
217
    }
218
219
    /**
220
     * @param string $fromName
221
     * @param string $replyTo
222
     * @param string $subject
223
     * @param string $html
224
     * @param string $listName
225
     * @param array  $options
226
     * @param array  $contentOptions
227
     *
228
     * @return array|bool
229
     *
230
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
231
     */
232
    public function createCampaign($fromName, $replyTo, $subject, $html = '', $listName = '', $options = [], $contentOptions = [])
233
    {
234
        $list = $this->lists->findByName($listName);
235
236
        $defaultOptions = [
237
            'type' => 'regular',
238
            'recipients' => [
239
                'list_id' => $list->getId(),
240
            ],
241
            'settings' => [
242
                'subject_line' => $subject,
243
                'from_name' => $fromName,
244
                'reply_to' => $replyTo,
245
            ],
246
        ];
247
248
        $options = array_merge($defaultOptions, $options);
249
250
        $response = $this->mailChimp->post('campaigns', $options);
251
252
        if (! $this->lastActionSucceeded()) {
253
            return false;
254
        }
255
256
        if ($html === '') {
257
            return $response;
258
        }
259
260
        if (! $this->updateContent($response['id'], $html, $contentOptions)) {
261
            return false;
262
        }
263
264
        return $response;
265
    }
266
267
    /**
268
     * @param $campaignId
269
     * @param $html
270
     * @param array $options
271
     * @return array|bool|false
272
     */
273
    public function updateContent($campaignId, $html, $options = [])
274
    {
275
        $defaultOptions = compact('html');
276
277
        $options = array_merge($defaultOptions, $options);
278
279
        $response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options);
280
281
        if (! $this->lastActionSucceeded()) {
282
            return false;
283
        }
284
285
        return $response;
286
    }
287
288
    /**
289
     * @return \DrewM\MailChimp\MailChimp
290
     */
291
    public function getApi()
292
    {
293
        return $this->mailChimp;
294
    }
295
296
    /**
297
     * @return array|false
298
     */
299
    public function getLastError()
300
    {
301
        return $this->mailChimp->getLastError();
302
    }
303
304
    /**
305
     * @return bool
306
     */
307
    public function lastActionSucceeded()
308
    {
309
        return $this->mailChimp->success();
310
    }
311
312
    /**
313
     * @param string $email
314
     *
315
     * @return string
316
     */
317
    protected function getSubscriberHash($email)
318
    {
319
        return $this->mailChimp->subscriberHash($email);
320
    }
321
322
    /**
323
     * @param $email
324
     * @param $mergeFields
325
     * @param $options
326
     * @return array
327
     */
328
    protected function getSubscriptionOptions($email, $mergeFields, $options)
329
    {
330
        $defaultOptions = [
331
            'email_address' => $email,
332
            'status' => 'subscribed',
333
            'email_type' => 'html',
334
        ];
335
336
        if (count($mergeFields)) {
337
            $defaultOptions['merge_fields'] = $mergeFields;
338
        }
339
340
        $options = array_merge($defaultOptions, $options);
341
342
        return $options;
343
    }
344
}
345