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

Newsletter::sendCampaign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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