Completed
Push — master ( 85169d...dbd430 )
by Freek
01:54
created

Newsletter::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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
    public function subscribe($email, $mergeFields = [], $listName = '', $options = [])
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 string $listName
64
     *
65
     * @return array|bool
66
     *
67
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
68
     */
69
    public function getMember($email, $listName = '')
70
    {
71
        $list = $this->lists->findByName($listName);
72
73
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
74
    }
75
76
    /**
77
     * @param string $email
78
     * @param string $listName
79
     *
80
     * @return bool
81
     */
82
    public function hasMember($email, $listName = '')
83
    {
84
        $response = $this->getMember($email, $listName);
85
86
        if (! isset($response['email_address'])) {
87
            return false;
88
        }
89
90
        if (strtolower($response['email_address']) != strtolower($email)) {
91
            return false;
92
        }
93
94
        return true;
95
    }
96
97
    /**
98
     * @param $email
99
     * @param string $listName
100
     *
101
     * @return array|false
102
     *
103
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
104
     */
105
    public function unsubscribe($email, $listName = '')
106
    {
107
        $list = $this->lists->findByName($listName);
108
109
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", [
110
            'status' => 'unsubscribed',
111
        ]);
112
113
        return $response;
114
    }
115
116
    /**
117
     * @param $email
118
     * @param string $listName
119
     *
120
     * @return array|false
121
     *
122
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
123
     */
124
    public function delete($email, $listName = '')
125
    {
126
        $list = $this->lists->findByName($listName);
127
128
        $response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
129
130
        return $response;
131
    }
132
133
    /**
134
     * @param string $fromName
135
     * @param string $replyTo
136
     * @param string $subject
137
     * @param string $html
138
     * @param string $listName
139
     * @param array  $options
140
     * @param array  $contentOptions
141
     *
142
     * @return array|bool
143
     *
144
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
145
     */
146
    public function createCampaign($fromName, $replyTo, $subject, $html = '', $listName = '', $options = [], $contentOptions = [])
147
    {
148
        $list = $this->lists->findByName($listName);
149
150
        $defaultOptions = [
151
            'type' => 'regular',
152
            'recipients' => [
153
                'list_id' => $list->getId(),
154
            ],
155
            'settings' => [
156
                'subject_line' => $subject,
157
                'from_name' => $fromName,
158
                'reply_to' => $replyTo,
159
            ],
160
        ];
161
162
        $options = array_merge($defaultOptions, $options);
163
164
        $response = $this->mailChimp->post('campaigns', $options);
165
166
        if (! $this->lastActionSucceeded()) {
167
            return false;
168
        }
169
170
        if ($html === '') {
171
            return $response;
172
        }
173
174
        if (! $this->updateContent($response['id'], $html, $contentOptions)) {
175
            return false;
176
        }
177
178
        return $response;
179
    }
180
181
    public function updateContent($campaignId, $html, $options = [])
182
    {
183
        $defaultOptions = compact('html');
184
185
        $options = array_merge($defaultOptions, $options);
186
187
        $response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options);
188
189
        if (! $this->lastActionSucceeded()) {
190
            return false;
191
        }
192
193
        return $response;
194
    }
195
196
    /**
197
     * @return \DrewM\MailChimp\MailChimp
198
     */
199
    public function getApi()
200
    {
201
        return $this->mailChimp;
202
    }
203
204
    /**
205
     * @return array|false
206
     */
207
    public function getLastError()
208
    {
209
        return $this->mailChimp->getLastError();
210
    }
211
212
    /**
213
     * @return bool
214
     */
215
    public function lastActionSucceeded()
216
    {
217
        return ! $this->mailChimp->getLastError();
218
    }
219
220
    /**
221
     * @param string $email
222
     *
223
     * @return string
224
     */
225
    protected function getSubscriberHash($email)
226
    {
227
        return $this->mailChimp->subscriberHash($email);
228
    }
229
}
230