Completed
Pull Request — master (#30)
by
unknown
02:08
created

Newsletter::getMember()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Spatie\Newsletter;
4
5
use DrewM\MailChimp\MailChimp;
6
use Spatie\Newsletter\Exceptions\InvalidSubscribeStatus;
7
8
class Newsletter
9
{
10
    /** @var \DrewM\MailChimp\MailChimp */
11
    protected $mailChimp;
12
13
    /** * @var \Spatie\Newsletter\NewsletterListCollection */
14
    protected $lists;
15
16
    /**
17
     * @param \DrewM\MailChimp\MailChimp                  $mailChimp
18
     * @param \Spatie\Newsletter\NewsletterListCollection $lists
19
     */
20
    public function __construct(MailChimp $mailChimp, NewsletterListCollection $lists)
21
    {
22
        $this->mailChimp = $mailChimp;
23
24
        $this->lists = $lists;
25
    }
26
27
    /**
28
     * @param string $email
29
     * @param array  $mergeFields
30
     * @param string $listName
31
     * @param array  $options
32
     *
33
     * @return array|bool
34
     *
35
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
36
     */
37
    public function subscribe($email, $mergeFields = [], $listName = '', $options = [])
38
    {
39
        $list = $this->lists->findByName($listName);
40
41
        $defaultOptions = [
42
            'email_address' => $email,
43
            'status' => $this->getSubscribeStatus(),
44
            'email_type' => 'html',
45
        ];
46
47
        if (count($mergeFields)) {
48
            $defaultOptions['merge_fields'] = $mergeFields;
49
        }
50
51
        $options = array_merge($defaultOptions, $options);
52
53
        $response = $this->mailChimp->post("lists/{$list->getId()}/members", $options);
54
55
        if (!$this->lastActionSucceeded()) {
56
            return false;
57
        }
58
59
        return $response;
60
    }
61
62
    /**
63
     * @param string $email
64
     * @param string $listName
65
     *
66
     * @return array|bool
67
     *
68
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
69
     */
70
    public function getMember($email, $listName = '')
71
    {
72
        $list = $this->lists->findByName($listName);
73
74
        if (!$this->lastActionSucceeded()) {
75
            return false;
76
        }
77
78
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
79
    }
80
81
    /**
82
     * @param string $email
83
     * @param string $listName
84
     *
85
     * @return bool
86
     */
87
    public function hasMember($email, $listName = '')
88
    {
89
        $response = $this->getMember($email, $listName);
90
91
        if (! isset($response['email_address'])) {
92
            return false;
93
        }
94
95
        if ($response['email_address'] != $email) {
96
            return false;
97
        }
98
99
        return true;
100
    }
101
102
    /**
103
     * @param $email
104
     * @param string $listName
105
     *
106
     * @return array|false
107
     *
108
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
109
     */
110
    public function unsubscribe($email, $listName = '')
111
    {
112
        $list = $this->lists->findByName($listName);
113
114
        $response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
115
116
        return $response;
117
    }
118
119
    /**
120
     * @param string $fromName
121
     * @param string $replyTo
122
     * @param string $subject
123
     * @param string $html
124
     * @param string $listName
125
     * @param array  $options
126
     * @param array  $contentOptions
127
     *
128
     * @return array|bool
129
     *
130
     * @throws \Spatie\Newsletter\Exceptions\InvalidNewsletterList
131
     */
132
    public function createCampaign($fromName, $replyTo, $subject, $html = '', $listName = '', $options = [], $contentOptions = [])
133
    {
134
        $list = $this->lists->findByName($listName);
135
136
        $defaultOptions = [
137
            'type' => 'regular',
138
            'recipients' => [
139
                'list_id' => $list->getId(),
140
            ],
141
            'settings' => [
142
                'subject_line' => $subject,
143
                'from_name' => $fromName,
144
                'reply_to' => $replyTo,
145
            ],
146
        ];
147
148
        $options = array_merge($defaultOptions, $options);
149
150
        $response = $this->mailChimp->post('campaigns', $options);
151
152
        if (!$this->lastActionSucceeded()) {
153
            return false;
154
        }
155
156
        if ($html === '') {
157
            return $response;
158
        }
159
160
        if (!$this->updateContent($response['id'], $html, $contentOptions)) {
161
            return false;
162
        }
163
164
        return $response;
165
    }
166
167
    public function updateContent($campaignId, $html, $options = [])
168
    {
169
        $defaultOptions = compact('html');
170
171
        $options = array_merge($defaultOptions, $options);
172
173
        $response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options);
174
175
        if (!$this->lastActionSucceeded()) {
176
            return false;
177
        }
178
179
        return $response;
180
    }
181
182
    /**
183
     * @return \DrewM\MailChimp\MailChimp
184
     */
185
    public function getApi()
186
    {
187
        return $this->mailChimp;
188
    }
189
190
    /**
191
     * @return array|false
192
     */
193
    public function getLastError()
194
    {
195
        return $this->mailChimp->getLastError();
196
    }
197
198
    /**
199
     * @return bool
200
     */
201
    public function lastActionSucceeded()
202
    {
203
        return !$this->mailChimp->getLastError();
204
    }
205
206
    /**
207
     * @param string $email
208
     *
209
     * @return string
210
     */
211
    protected function getSubscriberHash($email)
212
    {
213
        return $this->mailChimp->subscriberHash($email);
214
    }
215
216
    protected function getSubscribeStatus()
217
    {
218
        $subscribeStatus = config('laravel-newsletter.defaultSubscribeStatus');
219
220
        if ($subscribeStatus == "subscribed" || $configSubscribeStatus == "pending") {
0 ignored issues
show
Bug introduced by
The variable $configSubscribeStatus does not exist. Did you mean $subscribeStatus?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
221
            return $subscribeStatus;
222
        }
223
        else {
224
            throw InvalidSubscribeStatus::invalidStatus($subscribeStatus);
225
        }
226
    }
227
}
228