Completed
Push — master ( d8ae00...12ed8a )
by Tobias
02:18
created

Member   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 222
Duplicated Lines 23.87 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 86.49%

Importance

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