Completed
Pull Request — master (#168)
by
unknown
06:02
created

Newsletter::getMembers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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