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

Member::createMultiple()   C

Complexity

Conditions 11
Paths 7

Size

Total Lines 52

Duplication

Lines 22
Ratio 42.31 %

Code Coverage

Tests 24
CRAP Score 11.055

Importance

Changes 0
Metric Value
dl 22
loc 52
ccs 24
cts 26
cp 0.9231
rs 6.9006
c 0
b 0
f 0
cc 11
nc 7
nop 3
crap 11.055

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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