Completed
Pull Request — master (#220)
by
unknown
01:35 queued 10s
created

ApiDriver::deletePermanently()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\Newsletter;
4
5
use DrewM\MailChimp\MailChimp;
6
7
class ApiDriver implements Newsletter
8
{
9
    /** @var MailChimp */
10
    protected $mailChimp;
11
12
    /** @var 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(
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
        string $email,
46
        array $mergeFields = [],
47
        string $listName = '',
48
        array $options = []
49
    ) {
50
        $list = $this->lists->findByName($listName);
51
52
        $options = $this->getSubscriptionOptions($email, $mergeFields, $options);
53
54
        $response = $this->mailChimp->put("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}",
55
            $options);
56
57
        if (! $this->lastActionSucceeded()) {
58
            return false;
59
        }
60
61
        return $response;
62
    }
63
64
    public function getMembers(string $listName = '', array $parameters = [])
65
    {
66
        $list = $this->lists->findByName($listName);
67
68
        return $this->mailChimp->get("lists/{$list->getId()}/members", $parameters);
69
    }
70
71
    public function getMember(string $email, string $listName = '')
72
    {
73
        $list = $this->lists->findByName($listName);
74
75
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
76
    }
77
78
    public function getMemberActivity(string $email, string $listName = '')
79
    {
80
        $list = $this->lists->findByName($listName);
81
82
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/activity");
83
    }
84
85
    public function hasMember(string $email, string $listName = ''): bool
86
    {
87
        $response = $this->getMember($email, $listName);
88
89
        if (! isset($response['email_address'])) {
90
            return false;
91
        }
92
93
        if (strtolower($response['email_address']) != strtolower($email)) {
94
            return false;
95
        }
96
97
        return true;
98
    }
99
100
    public function isSubscribed(string $email, string $listName = ''): bool
101
    {
102
        $response = $this->getMember($email, $listName);
103
104
        if (! isset($response)) {
105
            return false;
106
        }
107
108
        if ($response['status'] != 'subscribed') {
109
            return false;
110
        }
111
112
        return true;
113
    }
114
115 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...
116
    {
117
        $list = $this->lists->findByName($listName);
118
119
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", [
120
            'status' => 'unsubscribed',
121
        ]);
122
123
        if (! $this->lastActionSucceeded()) {
124
            return false;
125
        }
126
127
        return $response;
128
    }
129
130 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...
131
    {
132
        $list = $this->lists->findByName($listName);
133
134
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($currentEmailAddress)}",
135
            [
136
                'email_address' => $newEmailAddress,
137
            ]);
138
139
        return $response;
140
    }
141
142 View Code Duplication
    public function delete(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...
143
    {
144
        $list = $this->lists->findByName($listName);
145
146
        $response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
147
148
        return $response;
149
    }
150
151 View Code Duplication
    public function deletePermanently(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...
152
    {
153
        $list = $this->lists->findByName($listName);
154
155
        $response = $this->mailChimp->post("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/actions/delete-permanent");
156
157
        return $response;
158
    }
159
160
    public function getTags(string $email, string $listName = '')
161
    {
162
        $list = $this->lists->findByName($listName);
163
164
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/tags");
165
    }
166
167 View Code Duplication
    public function addTags(array $tags, 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...
168
    {
169
        $list = $this->lists->findByName($listName);
170
171
        $payload = collect($tags)->map(function ($tag) {
172
            return ['name' => $tag, 'status' => 'active'];
173
        })->toArray();
174
175
        return $this->mailChimp->post("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/tags", [
176
            'tags' => $payload,
177
        ]);
178
    }
179
180 View Code Duplication
    public function removeTags(array $tags, 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...
181
    {
182
        $list = $this->lists->findByName($listName);
183
184
        $payload = collect($tags)->map(function ($tag) {
185
            return ['name' => $tag, 'status' => 'inactive'];
186
        })->toArray();
187
188
        return $this->mailChimp->post("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/tags", [
189
            'tags' => $payload,
190
        ]);
191
    }
192
193
    public function createCampaign(
194
        string $fromName,
195
        string $replyTo,
196
        string $subject,
197
        string $html = '',
198
        string $listName = '',
199
        array $options = [],
200
        array $contentOptions = []
201
    ) {
202
        $list = $this->lists->findByName($listName);
203
204
        $defaultOptions = [
205
            'type' => 'regular',
206
            'recipients' => [
207
                'list_id' => $list->getId(),
208
            ],
209
            'settings' => [
210
                'subject_line' => $subject,
211
                'from_name' => $fromName,
212
                'reply_to' => $replyTo,
213
            ],
214
        ];
215
216
        $options = array_merge($defaultOptions, $options);
217
218
        $response = $this->mailChimp->post('campaigns', $options);
219
220
        if (! $this->lastActionSucceeded()) {
221
            return false;
222
        }
223
224
        if ($html === '') {
225
            return $response;
226
        }
227
228
        if (! $this->updateContent($response['id'], $html, $contentOptions)) {
229
            return false;
230
        }
231
232
        return $response;
233
    }
234
235
    public function updateContent(string $campaignId, string $html, array $options = [])
236
    {
237
        $defaultOptions = compact('html');
238
239
        $options = array_merge($defaultOptions, $options);
240
241
        $response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options);
242
243
        if (! $this->lastActionSucceeded()) {
244
            return false;
245
        }
246
247
        return $response;
248
    }
249
250
    public function getApi(): MailChimp
251
    {
252
        return $this->mailChimp;
253
    }
254
255
    /**
256
     * @return array|false
257
     */
258
    public function getLastError()
259
    {
260
        return $this->mailChimp->getLastError();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->mailChimp->getLastError(); of type string|false adds the type string to the return on line 260 which is incompatible with the return type declared by the interface Spatie\Newsletter\Newsletter::getLastError of type array|false.
Loading history...
261
    }
262
263
    public function lastActionSucceeded(): bool
264
    {
265
        return $this->mailChimp->success();
266
    }
267
268
    protected function getSubscriberHash(string $email): string
269
    {
270
        return $this->mailChimp->subscriberHash($email);
271
    }
272
273
    protected function getSubscriptionOptions(string $email, array $mergeFields, array $options): array
274
    {
275
        $defaultOptions = [
276
            'email_address' => $email,
277
            'status' => 'subscribed',
278
            'email_type' => 'html',
279
        ];
280
281
        if (count($mergeFields)) {
282
            $defaultOptions['merge_fields'] = $mergeFields;
283
        }
284
285
        $options = array_merge($defaultOptions, $options);
286
287
        return $options;
288
    }
289
}
290