Completed
Push — master ( 597531...3fc2f8 )
by David
08:11
created

Member   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 212
Duplicated Lines 23.58 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 89.33%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 3
dl 50
loc 212
ccs 67
cts 75
cp 0.8933
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 19 3
A show() 0 9 1
A create() 0 18 3
C createMultiple() 22 52 11
B update() 19 30 7
A delete() 9 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2013 Mailgun
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license. See the LICENSE file for details.
10
 */
11
12
namespace Mailgun\Api\MailingList;
13
14
use Mailgun\Api\HttpApi;
15
use Mailgun\Assert;
16
use Mailgun\Exception\InvalidArgumentException;
17
use Mailgun\Model\MailingList\Member\CreateResponse;
18
use Mailgun\Model\MailingList\Member\DeleteResponse;
19
use Mailgun\Model\MailingList\Member\IndexResponse;
20
use Mailgun\Model\MailingList\Member\ShowResponse;
21
use Mailgun\Model\MailingList\Member\UpdateResponse;
22
use Mailgun\Model\MailingList\UpdateResponse as MailingListUpdateResponse;
23
24
/**
25
 * @see https://documentation.mailgun.com/en/latest/api-mailinglists.html
26
 */
27
class Member extends HttpApi
28
{
29
    /**
30
     * Returns a paginated list of members of the mailing list.
31
     *
32
     * @param string    $address    Address of the mailing list
33
     * @param int       $limit      Maximum number of records to return (optional: 100 by default)
34
     * @param bool|null $subscribed `true` to lists subscribed, `false` for unsubscribed. list all if null
35
     *
36
     * @return IndexResponse
37
     *
38
     * @throws \Exception
39
     */
40 3
    public function index(string $address, int $limit = 100, bool $subscribed = null)
41
    {
42 3
        Assert::stringNotEmpty($address);
43 3
        Assert::greaterThan($limit, 0);
44
45
        $params = [
46 3
            'limit' => $limit,
47
        ];
48
49 3
        if (true === $subscribed) {
50 1
            $params['subscribed'] = 'yes';
51 2
        } elseif (false === $subscribed) {
52 1
            $params['subscribed'] = 'no';
53
        }
54
55 3
        $response = $this->httpGet(sprintf('/v3/lists/%s/members/pages', $address), $params);
56
57 3
        return $this->hydrateResponse($response, IndexResponse::class);
58
    }
59
60
    /**
61
     * Shows a single member of the mailing list.
62
     *
63
     * @param string $list    Address of the mailing list
64
     * @param string $address Address of the member
65
     *
66
     * @return ShowResponse
67
     *
68
     * @throws \Exception
69
     */
70
    public function show(string $list, string $address)
71
    {
72
        Assert::stringNotEmpty($list);
73
        Assert::stringNotEmpty($address);
74
75
        $response = $this->httpGet(sprintf('/v3/lists/%s/members/%s', $list, $address));
76
77
        return $this->hydrateResponse($response, ShowResponse::class);
78
    }
79
80
    /**
81
     * Creates (or updates) a member of the mailing list.
82
     *
83
     * @param string $list       Address of the mailing list
84
     * @param string $address    Address for the member
85
     * @param string $name       Name for the member (optional)
86
     * @param array  $vars       Array of field => value pairs to store additional data
87
     * @param bool   $subscribed `true` to add as subscribed (default), `false` as unsubscribed
88
     * @param bool   $upsert     `true` to update member if present, `false` to raise error in case of a duplicate member (default)
89
     *
90
     * @return CreateResponse
91
     *
92
     * @throws \Exception
93
     */
94 3
    public function create(string $list, string $address, string $name = null, array $vars = [], bool $subscribed = true, bool $upsert = false)
95
    {
96 3
        Assert::stringNotEmpty($list);
97 2
        Assert::stringNotEmpty($address);
98 1
        Assert::nullOrStringNotEmpty($name);
0 ignored issues
show
Bug introduced by
The method nullOrStringNotEmpty() does not exist on Mailgun\Assert. Did you maybe mean notEmpty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
99
100
        $params = [
101 1
            'address' => $address,
102 1
            'name' => $name,
103 1
            'vars' => \json_encode($vars),
104 1
            'subscribed' => $subscribed ? 'yes' : 'no',
105 1
            'upsert' => $upsert ? 'yes' : 'no',
106
        ];
107
108 1
        $response = $this->httpPost(sprintf('/v3/lists/%s/members', $list), $params);
109
110 1
        return $this->hydrateResponse($response, CreateResponse::class);
111
    }
112
113
    /**
114
     * Adds multiple members (up to 1000) to the mailing list.
115
     *
116
     * @param string $list    Address of the mailing list
117
     * @param array  $members Array of members, each item should be either a single string address or an array of member properties
118
     * @param bool   $upsert  `true` to update existing members, `false` (default) to ignore duplicates
119
     *
120
     * @return UpdateResponse
121
     *
122
     * @throws \Exception
123
     */
124 3
    public function createMultiple(string $list, array $members, $upsert = false)
125
    {
126 3
        Assert::stringNotEmpty($list);
127 3
        Assert::isArray($members);
128
129
        // workaround for webmozart/asserts <= 1.2
130 3
        if (count($members) > 1000) {
131 1
            throw new InvalidArgumentException(sprintf('Expected an Array to contain at most %2$d elements. Got: %d', 1000, count($members)));
132
        }
133
134 2
        foreach ($members as $data) {
135 2
            if (is_string($data)) {
136 2
                Assert::stringNotEmpty($data);
137
                // single address - no additional validation required
138 2
                continue;
139
            }
140
141 2
            Assert::isArray($data);
142
143 2 View Code Duplication
            foreach ($data as $field => &$value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
144
                switch ($field) {
145 2
                    case 'address':
146 2
                        Assert::stringNotEmpty($value);
147
148 2
                        break;
149 2
                    case 'vars':
150
                        if (is_array($value)) {
151
                            $value = json_encode($value);
152
                        }
153
                        // We should assert that "vars"'s $value is a string.
154
                        // no break
155 2
                    case 'name':
156 2
                        Assert::string($value);
157
158 2
                        break;
159 2
                    case 'subscribed':
160 2
                        Assert::oneOf($value, ['yes', 'no']);
161
162 2
                        break;
163
                }
164
            }
165
        }
166
167
        $params = [
168 1
            'members' => json_encode($members),
169 1
            'upsert' => $upsert ? 'yes' : 'no',
170
        ];
171
172 1
        $response = $this->httpPost(sprintf('/v3/lists/%s/members.json', $list), $params);
173
174 1
        return $this->hydrateResponse($response, MailingListUpdateResponse::class);
175
    }
176
177
    /**
178
     * Updates a member on the mailing list.
179
     *
180
     * @param string $list       Address of the mailing list
181
     * @param string $address    Address of the member
182
     * @param array  $parameters Array of key => value pairs to update
183
     *
184
     * @return UpdateResponse
185
     *
186
     * @throws \Exception
187
     */
188 2
    public function update(string $list, string $address, array $parameters = [])
189
    {
190 2
        Assert::stringNotEmpty($list);
191 2
        Assert::stringNotEmpty($address);
192 2
        Assert::isArray($parameters);
193
194 2 View Code Duplication
        foreach ($parameters as $field => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
195
            switch ($field) {
196 2
                case 'vars':
197 2
                    if (is_array($value)) {
198
                        $value = json_encode($value);
199
                    }
200
                    // We should assert that "vars"'s $value is a string.
201
                    // no break
202 1
                case 'address':
203 1
                case 'name':
204 2
                    Assert::stringNotEmpty($value);
205
206 1
                    break;
207 1
                case 'subscribed':
208 1
                    Assert::oneOf($value, ['yes', 'no']);
209
210 1
                    break;
211
            }
212
        }
213
214 1
        $response = $this->httpPut(sprintf('/v3/lists/%s/members/%s', $list, $address), $parameters);
215
216 1
        return $this->hydrateResponse($response, UpdateResponse::class);
217
    }
218
219
    /**
220
     * Removes a member from the mailing list.
221
     *
222
     * @param string $list    Address of the mailing list
223
     * @param string $address Address of the member
224
     *
225
     * @return DeleteResponse
226
     *
227
     * @throws \Exception
228
     */
229 1 View Code Duplication
    public function delete(string $list, string $address)
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...
230
    {
231 1
        Assert::stringNotEmpty($list);
232 1
        Assert::stringNotEmpty($address);
233
234 1
        $response = $this->httpDelete(sprintf('/v3/lists/%s/members/%s', $list, $address));
235
236 1
        return $this->hydrateResponse($response, DeleteResponse::class);
237
    }
238
}
239