Completed
Push — master ( e83cc7...65dda4 )
by Freek
02:35
created

Newsletter::subscribeOrUpdate()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 13
nc 4
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
        $defaultOptions = [
41
            'email_address' => $email,
42
            'status' => 'subscribed',
43
            'email_type' => 'html',
44
        ];
45
46
        if (count($mergeFields)) {
47
            $defaultOptions['merge_fields'] = $mergeFields;
48
        }
49
50
        $options = array_merge($defaultOptions, $options);
51
52
        $response = $this->mailChimp->post("lists/{$list->getId()}/members", $options);
53
54
        if (! $this->lastActionSucceeded()) {
55
            return false;
56
        }
57
58
        return $response;
59
    }
60
61
    /**
62
     * @param string $email
63
     * @param array  $mergeFields
64
     * @param string $listName
65
     * @param array  $options
66
     *
67
     * @return array|bool
68
     *
69
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
70
     */
71 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...
72
    {
73
        $list = $this->lists->findByName($listName);
74
75
        $defaultOptions = [
76
            'email_address' => $email,
77
            'status' => 'subscribed',
78
            'email_type' => 'html',
79
        ];
80
81
        if (count($mergeFields)) {
82
            $defaultOptions['merge_fields'] = $mergeFields;
83
        }
84
85
        $options = array_merge($defaultOptions, $options);
86
87
        $response = $this->mailChimp->put("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", $options);
88
89
        if (! $this->lastActionSucceeded()) {
90
            return false;
91
        }
92
93
        return $response;
94
    }
95
96
    /**
97
     * @param string $email
98
     * @param string $listName
99
     *
100
     * @return array|bool
101
     *
102
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
103
     */
104
    public function getMember($email, $listName = '')
105
    {
106
        $list = $this->lists->findByName($listName);
107
108
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
109
    }
110
111
    /**
112
     * @param string $email
113
     * @param string $listName
114
     *
115
     * @return bool
116
     */
117
    public function hasMember($email, $listName = '')
118
    {
119
        $response = $this->getMember($email, $listName);
120
121
        if (! isset($response['email_address'])) {
122
            return false;
123
        }
124
125
        if (strtolower($response['email_address']) != strtolower($email)) {
126
            return false;
127
        }
128
129
        return true;
130
    }
131
132
    /**
133
     * @param $email
134
     * @param string $listName
135
     *
136
     * @return array|false
137
     *
138
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
139
     */
140
    public function unsubscribe($email, $listName = '')
141
    {
142
        $list = $this->lists->findByName($listName);
143
144
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", [
145
            'status' => 'unsubscribed',
146
        ]);
147
148
        return $response;
149
    }
150
151
    /**
152
     * @param $email
153
     * @param string $listName
154
     *
155
     * @return array|false
156
     *
157
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
158
     */
159
    public function delete($email, $listName = '')
160
    {
161
        $list = $this->lists->findByName($listName);
162
163
        $response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
164
165
        return $response;
166
    }
167
168
    /**
169
     * @param string $fromName
170
     * @param string $replyTo
171
     * @param string $subject
172
     * @param string $html
173
     * @param string $listName
174
     * @param array  $options
175
     * @param array  $contentOptions
176
     *
177
     * @return array|bool
178
     *
179
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
180
     */
181
    public function createCampaign($fromName, $replyTo, $subject, $html = '', $listName = '', $options = [], $contentOptions = [])
182
    {
183
        $list = $this->lists->findByName($listName);
184
185
        $defaultOptions = [
186
            'type' => 'regular',
187
            'recipients' => [
188
                'list_id' => $list->getId(),
189
            ],
190
            'settings' => [
191
                'subject_line' => $subject,
192
                'from_name' => $fromName,
193
                'reply_to' => $replyTo,
194
            ],
195
        ];
196
197
        $options = array_merge($defaultOptions, $options);
198
199
        $response = $this->mailChimp->post('campaigns', $options);
200
201
        if (! $this->lastActionSucceeded()) {
202
            return false;
203
        }
204
205
        if ($html === '') {
206
            return $response;
207
        }
208
209
        if (! $this->updateContent($response['id'], $html, $contentOptions)) {
210
            return false;
211
        }
212
213
        return $response;
214
    }
215
216
    public function updateContent($campaignId, $html, $options = [])
217
    {
218
        $defaultOptions = compact('html');
219
220
        $options = array_merge($defaultOptions, $options);
221
222
        $response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options);
223
224
        if (! $this->lastActionSucceeded()) {
225
            return false;
226
        }
227
228
        return $response;
229
    }
230
231
    /**
232
     * @return \DrewM\MailChimp\MailChimp
233
     */
234
    public function getApi()
235
    {
236
        return $this->mailChimp;
237
    }
238
239
    /**
240
     * @return array|false
241
     */
242
    public function getLastError()
243
    {
244
        return $this->mailChimp->getLastError();
245
    }
246
247
    /**
248
     * @return bool
249
     */
250
    public function lastActionSucceeded()
251
    {
252
        return ! $this->mailChimp->getLastError();
253
    }
254
255
    /**
256
     * @param string $email
257
     *
258
     * @return string
259
     */
260
    protected function getSubscriberHash($email)
261
    {
262
        return $this->mailChimp->subscriberHash($email);
263
    }
264
}
265