Completed
Push — master ( 8b02c2...464e62 )
by Freek
01:15
created

Newsletter::updateEmailAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
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 $email
78
     * @param string $listName
79
     *
80
     * @return array|bool
81
     *
82
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
83
     */
84
    public function getMember($email, $listName = '')
85
    {
86
        $list = $this->lists->findByName($listName);
87
88
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
89
    }
90
91
    /**
92
     * @param string $email
93
     * @param string $listName
94
     *
95
     * @return bool
96
     */
97
    public function hasMember($email, $listName = '')
98
    {
99
        $response = $this->getMember($email, $listName);
100
101
        if (! isset($response['email_address'])) {
102
            return false;
103
        }
104
105
        if (strtolower($response['email_address']) != strtolower($email)) {
106
            return false;
107
        }
108
109
        return true;
110
    }
111
112
    /**
113
     * @param $email
114
     * @param string $listName
115
     *
116
     * @return array|false
117
     *
118
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
119
     */
120 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...
121
    {
122
        $list = $this->lists->findByName($listName);
123
124
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", [
125
            'status' => 'unsubscribed',
126
        ]);
127
128
        return $response;
129
    }
130
131
    /**
132
     * Update the email address of an existing list member.
133
     *
134
     * @param string $currentEmailAddress
135
     * @param string $newEmailAddress
136
     * @param string $listName
137
     *
138
     * @return array|false
139
     *
140
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
141
     */
142 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...
143
    {
144
        $list = $this->lists->findByName($listName);
145
146
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($currentEmailAddress)}", [
147
            'email_address' => $newEmailAddress,
148
        ]);
149
150
        return $response;
151
    }
152
153
    /**
154
     * @param $email
155
     * @param string $listName
156
     *
157
     * @return array|false
158
     *
159
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
160
     */
161
    public function delete($email, $listName = '')
162
    {
163
        $list = $this->lists->findByName($listName);
164
165
        $response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
166
167
        return $response;
168
    }
169
170
    /**
171
     * @param string $fromName
172
     * @param string $replyTo
173
     * @param string $subject
174
     * @param string $html
175
     * @param string $listName
176
     * @param array  $options
177
     * @param array  $contentOptions
178
     *
179
     * @return array|bool
180
     *
181
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
182
     */
183
    public function createCampaign($fromName, $replyTo, $subject, $html = '', $listName = '', $options = [], $contentOptions = [])
184
    {
185
        $list = $this->lists->findByName($listName);
186
187
        $defaultOptions = [
188
            'type' => 'regular',
189
            'recipients' => [
190
                'list_id' => $list->getId(),
191
            ],
192
            'settings' => [
193
                'subject_line' => $subject,
194
                'from_name' => $fromName,
195
                'reply_to' => $replyTo,
196
            ],
197
        ];
198
199
        $options = array_merge($defaultOptions, $options);
200
201
        $response = $this->mailChimp->post('campaigns', $options);
202
203
        if (! $this->lastActionSucceeded()) {
204
            return false;
205
        }
206
207
        if ($html === '') {
208
            return $response;
209
        }
210
211
        if (! $this->updateContent($response['id'], $html, $contentOptions)) {
212
            return false;
213
        }
214
215
        return $response;
216
    }
217
218
    public function updateContent($campaignId, $html, $options = [])
219
    {
220
        $defaultOptions = compact('html');
221
222
        $options = array_merge($defaultOptions, $options);
223
224
        $response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options);
225
226
        if (! $this->lastActionSucceeded()) {
227
            return false;
228
        }
229
230
        return $response;
231
    }
232
233
    /**
234
     * @return \DrewM\MailChimp\MailChimp
235
     */
236
    public function getApi()
237
    {
238
        return $this->mailChimp;
239
    }
240
241
    /**
242
     * @return array|false
243
     */
244
    public function getLastError()
245
    {
246
        return $this->mailChimp->getLastError();
247
    }
248
249
    /**
250
     * @return bool
251
     */
252
    public function lastActionSucceeded()
253
    {
254
        return ! $this->mailChimp->getLastError();
255
    }
256
257
    /**
258
     * @param string $email
259
     *
260
     * @return string
261
     */
262
    protected function getSubscriberHash($email)
263
    {
264
        return $this->mailChimp->subscriberHash($email);
265
    }
266
267
    /**
268
     * @param $email
269
     * @param $mergeFields
270
     * @param $options
271
     * @return array
272
     */
273
    protected function getSubscriptionOptions($email, $mergeFields, $options)
274
    {
275
        $defaultOptions = [
276
            'email_address' => $email,
277
            'status' => 'subscribed',
278
            'email_type' => 'html',
279
        ];
280
281
        if (count($mergeFields)) {
282
            $defaultOptions['merge_fields'] = $mergeFields;
283
        }
284
285
        $options = array_merge($defaultOptions, $options);
286
287
        return $options;
288
    }
289
}
290