Completed
Push — master ( 4b0611...da5ccd )
by Tobias
06:14 queued 03:25
created

Member::createMultiple()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 55

Duplication

Lines 20
Ratio 36.36 %

Code Coverage

Tests 32
CRAP Score 9.0164

Importance

Changes 0
Metric Value
dl 20
loc 55
ccs 32
cts 34
cp 0.9412
rs 7.4262
c 0
b 0
f 0
cc 9
nc 4
nop 3
crap 9.0164

How to fix   Long Method   

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
/*
4
 * Copyright (C) 2013 Mailgun
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Mailgun\Api\MailingList;
11
12
use Mailgun\Api\HttpApi;
13
use Mailgun\Assert;
14
use Mailgun\Exception\InvalidArgumentException;
15
use Mailgun\Model\MailingList\Member\CreateResponse;
16
use Mailgun\Model\MailingList\Member\DeleteResponse;
17
use Mailgun\Model\MailingList\Member\IndexResponse;
18
use Mailgun\Model\MailingList\Member\ShowResponse;
19
use Mailgun\Model\MailingList\Member\UpdateResponse;
20
use Mailgun\Model\MailingList\UpdateResponse as MailingListUpdateResponse;
21
22
class Member extends HttpApi
23
{
24
    /**
25
     * Returns a paginated list of members of the mailing list.
26
     *
27
     * @param string      $address    Address of the mailing list
28
     * @param int         $limit      Maximum number of records to return (optional: 100 by default)
29
     * @param string|null $subscribed `yes` to lists subscribed, `no` for unsubscribed. list all if null
30
     *
31
     * @return IndexResponse
32
     *
33
     * @throws \Exception
34
     */
35 3
    public function index($address, $limit = 100, $subscribed = null)
36
    {
37 3
        Assert::stringNotEmpty($address);
38 3
        Assert::integer($limit);
39 3
        Assert::greaterThan($limit, 0);
40 3
        Assert::oneOf($subscribed, [null, 'yes', 'no']);
41
42
        $params = [
43 3
            'limit' => $limit,
44 3
            'subscribed' => $subscribed,
45 3
        ];
46
47 3
        $response = $this->httpGet(sprintf('/v3/lists/%s/members/pages', $address), $params);
48
49 3
        return $this->hydrateResponse($response, IndexResponse::class);
50
    }
51
52
    /**
53
     * Shows a single member of the mailing list.
54
     *
55
     * @param string $list    Address of the mailing list
56
     * @param string $address Address of the member
57
     *
58
     * @return ShowResponse
59
     *
60
     * @throws \Exception
61
     */
62
    public function show($list, $address)
63
    {
64
        Assert::stringNotEmpty($list);
65
        Assert::stringNotEmpty($address);
66
67
        $response = $this->httpGet(sprintf('/v3/lists/%s/members/%s', $list, $address));
68
69
        return $this->hydrateResponse($response, ShowResponse::class);
70
    }
71
72
    /**
73
     * Creates (or updates) a member of the mailing list.
74
     *
75
     * @param string $list       Address of the mailing list
76
     * @param string $address    Address for the member
77
     * @param string $name       Name for the member (optional)
78
     * @param array  $vars       Array of field => value pairs to store additional data
79
     * @param string $subscribed `yes` to add as subscribed (default), `no` as unsubscribed
80
     * @param string $upsert     `yes` to update member if present, `no` to raise error in case of a duplicate member (default)
81
     *
82
     * @return CreateResponse
83
     *
84
     * @throws \Exception
85
     */
86 3
    public function create($list, $address, $name = null, array $vars = [], $subscribed = 'yes', $upsert = 'no')
87
    {
88 3
        Assert::stringNotEmpty($list);
89 3
        Assert::stringNotEmpty($address);
90 2
        Assert::nullOrStringNotEmpty($name);
91 2
        Assert::oneOf($subscribed, ['yes', 'no']);
92 1
        Assert::oneOf($upsert, ['yes', 'no']);
93
94
        $params = [
95 1
            'address' => $address,
96 1
            'name' => $name,
97 1
            'vars' => $vars,
98 1
            'subscribed' => $subscribed,
99 1
            'upsert' => $upsert,
100 1
        ];
101
102 1
        $response = $this->httpPost(sprintf('/v3/lists/%s/members', $list), $params);
103
104 1
        return $this->hydrateResponse($response, CreateResponse::class);
105
    }
106
107
    /**
108
     * Adds multiple members (up to 1000) to the mailing list.
109
     *
110
     * @param string $list    Address of the mailing list
111
     * @param array  $members Array of members, each item should be either a single string address or an array of member properties
112
     * @param string $upsert  `yes` to update existing members, `no` (default) to ignore duplicates
113
     *
114
     * @return UpdateResponse
115
     *
116
     * @throws \Exception
117
     */
118 3
    public function createMultiple($list, array $members, $upsert = 'no')
119
    {
120 3
        Assert::stringNotEmpty($list);
121 3
        Assert::isArray($members);
122
123
        // workaround for webmozart/asserts <= 1.2
124 3
        if (count($members) > 1000) {
125 1
            throw new InvalidArgumentException(sprintf('Expected an Array to contain at most %2$d elements. Got: %d',
126 1
                1000,
127 1
                count($members)
128 1
            ));
129
        }
130
131 2
        Assert::oneOf($upsert, ['yes', 'no']);
132
133 2
        foreach ($members as $data) {
134 2
            if (is_string($data)) {
135 2
                Assert::stringNotEmpty($data);
136
                // single address - no additional validation required
137 2
                continue;
138
            }
139
140 2
            Assert::isArray($data);
141
142 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...
143
                switch ($field) {
144 2
                    case 'address':
145 2
                        Assert::stringNotEmpty($value);
146
147 2
                        break;
148 2
                    case 'name':
149 2
                        Assert::string($value);
150
151 2
                        break;
152 2
                    case 'vars':
153
                        Assert::isArray($value);
154
155
                        break;
156 2
                    case 'subscribed':
157 2
                        Assert::oneOf($value, ['yes', 'no']);
158
159 1
                        break;
160
                }
161 2
            }
162 1
        }
163
164
        $params = [
165 1
            'members' => json_encode($members),
166 1
            'upsert' => $upsert,
167 1
        ];
168
169 1
        $response = $this->httpPost(sprintf('/v3/lists/%s/members.json', $list), $params);
170
171 1
        return $this->hydrateResponse($response, MailingListUpdateResponse::class);
172
    }
173
174
    /**
175
     * Updates a member on the mailing list.
176
     *
177
     * @param string $list       Address of the mailing list
178
     * @param string $address    Address of the member
179
     * @param array  $parameters Array of key => value pairs to update
180
     *
181
     * @return UpdateResponse
182
     *
183
     * @throws \Exception
184
     */
185 2
    public function update($list, $address, $parameters = [])
186
    {
187 2
        Assert::stringNotEmpty($list);
188 2
        Assert::stringNotEmpty($address);
189 2
        Assert::isArray($parameters);
190
191 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...
192
            switch ($field) {
193 2
                case 'address':
194 2
                case 'name':
195
                    Assert::stringNotEmpty($value);
196
197
                    break;
198 2
                case 'vars':
199 2
                    Assert::isArray($value);
200
201 1
                    break;
202 1
                case 'subscribed':
203 1
                    Assert::oneOf($value, ['yes', 'no']);
204
205 1
                    break;
206
            }
207 1
        }
208
209 1
        $response = $this->httpPut(sprintf('/v3/lists/%s/members/%s', $list, $address), $parameters);
210
211 1
        return $this->hydrateResponse($response, UpdateResponse::class);
212
    }
213
214
    /**
215
     * Removes a member from the mailing list.
216
     *
217
     * @param string $list    Address of the mailing list
218
     * @param string $address Address of the member
219
     *
220
     * @return DeleteResponse
221
     *
222
     * @throws \Exception
223
     */
224 1
    public function delete($list, $address)
225
    {
226 1
        Assert::stringNotEmpty($list);
227 1
        Assert::stringNotEmpty($address);
228
229 1
        $response = $this->httpDelete(sprintf('/v3/lists/%s/members/%s', $list, $address));
230
231 1
        return $this->hydrateResponse($response, DeleteResponse::class);
232
    }
233
}
234