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

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