Completed
Push — master ( 65dda4...839b45 )
by Freek
02:53
created

Newsletter::subscribeOrUpdate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 4
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
    public function unsubscribe($email, $listName = '')
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
     * @param $email
133
     * @param string $listName
134
     *
135
     * @return array|false
136
     *
137
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
138
     */
139
    public function delete($email, $listName = '')
140
    {
141
        $list = $this->lists->findByName($listName);
142
143
        $response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
144
145
        return $response;
146
    }
147
148
    /**
149
     * @param string $fromName
150
     * @param string $replyTo
151
     * @param string $subject
152
     * @param string $html
153
     * @param string $listName
154
     * @param array  $options
155
     * @param array  $contentOptions
156
     *
157
     * @return array|bool
158
     *
159
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
160
     */
161
    public function createCampaign($fromName, $replyTo, $subject, $html = '', $listName = '', $options = [], $contentOptions = [])
162
    {
163
        $list = $this->lists->findByName($listName);
164
165
        $defaultOptions = [
166
            'type' => 'regular',
167
            'recipients' => [
168
                'list_id' => $list->getId(),
169
            ],
170
            'settings' => [
171
                'subject_line' => $subject,
172
                'from_name' => $fromName,
173
                'reply_to' => $replyTo,
174
            ],
175
        ];
176
177
        $options = array_merge($defaultOptions, $options);
178
179
        $response = $this->mailChimp->post('campaigns', $options);
180
181
        if (! $this->lastActionSucceeded()) {
182
            return false;
183
        }
184
185
        if ($html === '') {
186
            return $response;
187
        }
188
189
        if (! $this->updateContent($response['id'], $html, $contentOptions)) {
190
            return false;
191
        }
192
193
        return $response;
194
    }
195
196
    public function updateContent($campaignId, $html, $options = [])
197
    {
198
        $defaultOptions = compact('html');
199
200
        $options = array_merge($defaultOptions, $options);
201
202
        $response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options);
203
204
        if (! $this->lastActionSucceeded()) {
205
            return false;
206
        }
207
208
        return $response;
209
    }
210
211
    /**
212
     * @return \DrewM\MailChimp\MailChimp
213
     */
214
    public function getApi()
215
    {
216
        return $this->mailChimp;
217
    }
218
219
    /**
220
     * @return array|false
221
     */
222
    public function getLastError()
223
    {
224
        return $this->mailChimp->getLastError();
225
    }
226
227
    /**
228
     * @return bool
229
     */
230
    public function lastActionSucceeded()
231
    {
232
        return ! $this->mailChimp->getLastError();
233
    }
234
235
    /**
236
     * @param string $email
237
     *
238
     * @return string
239
     */
240
    protected function getSubscriberHash($email)
241
    {
242
        return $this->mailChimp->subscriberHash($email);
243
    }
244
245
    /**
246
     * @param $email
247
     * @param $mergeFields
248
     * @param $options
249
     * @return array
250
     */
251
    protected function getSubscriptionOptions($email, $mergeFields, $options)
252
    {
253
        $defaultOptions = [
254
            'email_address' => $email,
255
            'status' => 'subscribed',
256
            'email_type' => 'html',
257
        ];
258
259
        if (count($mergeFields)) {
260
            $defaultOptions['merge_fields'] = $mergeFields;
261
        }
262
263
        $options = array_merge($defaultOptions, $options);
264
        return $options;
265
    }
266
}
267