Completed
Pull Request — master (#158)
by
unknown
01:28
created

Newsletter::updatePending()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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