Completed
Pull Request — master (#25)
by Sebastian
03:16
created

Newsletter::createCampaign()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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