Completed
Push — master ( 7426f2...097ac5 )
by Freek
01:17
created

Newsletter::getMembers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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 $email
142
     * @param string $listName
143
     *
144
     * @return array|false
145
     *
146
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
147
     */
148 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...
149
    {
150
        $list = $this->lists->findByName($listName);
151
152
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", [
153
            'status' => 'unsubscribed',
154
        ]);
155
156
        return $response;
157
    }
158
159
    /**
160
     * Update the email address of an existing list member.
161
     *
162
     * @param string $currentEmailAddress
163
     * @param string $newEmailAddress
164
     * @param string $listName
165
     *
166
     * @return array|false
167
     *
168
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
169
     */
170 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...
171
    {
172
        $list = $this->lists->findByName($listName);
173
174
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($currentEmailAddress)}", [
175
            'email_address' => $newEmailAddress,
176
        ]);
177
178
        return $response;
179
    }
180
181
    /**
182
     * @param $email
183
     * @param string $listName
184
     *
185
     * @return array|false
186
     *
187
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
188
     */
189
    public function delete($email, $listName = '')
190
    {
191
        $list = $this->lists->findByName($listName);
192
193
        $response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
194
195
        return $response;
196
    }
197
198
    /**
199
     * @param string $fromName
200
     * @param string $replyTo
201
     * @param string $subject
202
     * @param string $html
203
     * @param string $listName
204
     * @param array  $options
205
     * @param array  $contentOptions
206
     *
207
     * @return array|bool
208
     *
209
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
210
     */
211
    public function createCampaign($fromName, $replyTo, $subject, $html = '', $listName = '', $options = [], $contentOptions = [])
212
    {
213
        $list = $this->lists->findByName($listName);
214
215
        $defaultOptions = [
216
            'type' => 'regular',
217
            'recipients' => [
218
                'list_id' => $list->getId(),
219
            ],
220
            'settings' => [
221
                'subject_line' => $subject,
222
                'from_name' => $fromName,
223
                'reply_to' => $replyTo,
224
            ],
225
        ];
226
227
        $options = array_merge($defaultOptions, $options);
228
229
        $response = $this->mailChimp->post('campaigns', $options);
230
231
        if (! $this->lastActionSucceeded()) {
232
            return false;
233
        }
234
235
        if ($html === '') {
236
            return $response;
237
        }
238
239
        if (! $this->updateContent($response['id'], $html, $contentOptions)) {
240
            return false;
241
        }
242
243
        return $response;
244
    }
245
246
    /**
247
     * @param $campaignId
248
     * @param $html
249
     * @param array $options
250
     * @return array|bool|false
251
     */
252
    public function updateContent($campaignId, $html, $options = [])
253
    {
254
        $defaultOptions = compact('html');
255
256
        $options = array_merge($defaultOptions, $options);
257
258
        $response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options);
259
260
        if (! $this->lastActionSucceeded()) {
261
            return false;
262
        }
263
264
        return $response;
265
    }
266
267
    /**
268
     * @return \DrewM\MailChimp\MailChimp
269
     */
270
    public function getApi()
271
    {
272
        return $this->mailChimp;
273
    }
274
275
    /**
276
     * @return array|false
277
     */
278
    public function getLastError()
279
    {
280
        return $this->mailChimp->getLastError();
281
    }
282
283
    /**
284
     * @return bool
285
     */
286
    public function lastActionSucceeded()
287
    {
288
        return ! $this->mailChimp->getLastError();
289
    }
290
291
    /**
292
     * @param string $email
293
     *
294
     * @return string
295
     */
296
    protected function getSubscriberHash($email)
297
    {
298
        return $this->mailChimp->subscriberHash($email);
299
    }
300
301
    /**
302
     * @param $email
303
     * @param $mergeFields
304
     * @param $options
305
     * @return array
306
     */
307
    protected function getSubscriptionOptions($email, $mergeFields, $options)
308
    {
309
        $defaultOptions = [
310
            'email_address' => $email,
311
            'status' => 'subscribed',
312
            'email_type' => 'html',
313
        ];
314
315
        if (count($mergeFields)) {
316
            $defaultOptions['merge_fields'] = $mergeFields;
317
        }
318
319
        $options = array_merge($defaultOptions, $options);
320
321
        return $options;
322
    }
323
}
324