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

MailingList::update()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.0106

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 14
cts 15
cp 0.9333
rs 8.9137
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 6.0106
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;
11
12
use Mailgun\Api\MailingList\Member;
13
use Mailgun\Assert;
14
use Mailgun\Model\MailingList\CreateResponse;
15
use Mailgun\Model\MailingList\DeleteResponse;
16
use Mailgun\Model\MailingList\PagesResponse;
17
use Mailgun\Model\MailingList\ShowResponse;
18
use Mailgun\Model\MailingList\UpdateResponse;
19
20
class MailingList extends HttpApi
21
{
22
    /**
23
     * @return Member
24
     */
25
    public function member()
26
    {
27
        return new Member($this->httpClient, $this->requestBuilder, $this->hydrator);
28
    }
29
30
    /**
31
     * Returns a paginated list of mailing lists on the domain.
32
     *
33
     * @param int $limit Maximum number of records to return (optional: 100 by default)
34
     *
35
     * @return PagesResponse
36
     *
37
     * @throws \Exception
38
     */
39 2 View Code Duplication
    public function pages($limit = 100)
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...
40
    {
41 2
        Assert::integer($limit);
42 2
        Assert::greaterThan($limit, 0);
43
44
        $params = [
45 1
            'limit' => $limit,
46 1
        ];
47
48 1
        $response = $this->httpGet('/v3/lists/pages', $params);
49
50 1
        return $this->hydrateResponse($response, PagesResponse::class);
51
    }
52
53
    /**
54
     * Creates a new mailing list on the current domain.
55
     *
56
     * @param string $address     Address for the new mailing list
57
     * @param string $name        Name for the new mailing list (optional)
58
     * @param string $description Description for the new mailing list (optional)
59
     * @param string $accessLevel List access level, one of: readonly (default), members, everyone
60
     *
61
     * @return CreateResponse
62
     *
63
     * @throws \Exception
64
     */
65 3
    public function create($address, $name = null, $description = null, $accessLevel = 'readonly')
66
    {
67 3
        Assert::stringNotEmpty($address);
68 1
        Assert::nullOrStringNotEmpty($name);
69 1
        Assert::nullOrStringNotEmpty($description);
70 1
        Assert::oneOf($accessLevel, ['readonly', 'members', 'everyone']);
71
72
        $params = [
73 1
            'address' => $address,
74 1
            'name' => $name,
75 1
            'description' => $description,
76 1
            'access_level' => $accessLevel,
77 1
        ];
78
79 1
        $response = $this->httpPost('/v3/lists', $params);
80
81 1
        return $this->hydrateResponse($response, CreateResponse::class);
82
    }
83
84
    /**
85
     * Returns a single mailing list.
86
     *
87
     * @param string $address Address of the mailing list
88
     *
89
     * @return ShowResponse
90
     *
91
     * @throws \Exception
92
     */
93 2
    public function show($address)
94
    {
95 2
        Assert::stringNotEmpty($address);
96
97 1
        $response = $this->httpGet(sprintf('/v3/lists/%s', $address));
98
99 1
        return $this->hydrateResponse($response, ShowResponse::class);
100
    }
101
102
    /**
103
     * Updates a mailing list.
104
     *
105
     * @param string $address    Address of the mailing list
106
     * @param array  $parameters Array of field => value pairs to update
107
     *
108
     * @return UpdateResponse
109
     *
110
     * @throws \Exception
111
     */
112 2
    public function update($address, $parameters = [])
113
    {
114 2
        Assert::stringNotEmpty($address);
115 2
        Assert::isArray($parameters);
116
117 2
        foreach ($parameters as $field => $value) {
118
            switch ($field) {
119 2
                case 'address':
120 2
                case 'name':
121 2
                case 'description':
122 1
                    Assert::stringNotEmpty($value);
123
124 1
                    break;
125 1
                case 'access_level':
126 1
                    Assert::oneOf($value, ['readonly', 'members', 'everyone']);
127
128
                    break;
129
            }
130 1
        }
131
132 1
        $response = $this->httpPut(sprintf('/v3/lists/%s', $address), $parameters);
133
134 1
        return $this->hydrateResponse($response, UpdateResponse::class);
135
    }
136
137
    /**
138
     * Removes a mailing list from the domain.
139
     *
140
     * @param string $address Address of the mailing list
141
     *
142
     * @return DeleteResponse
143
     *
144
     * @throws \Exception
145
     */
146 1
    public function delete($address)
147
    {
148 1
        Assert::stringNotEmpty($address);
149
150 1
        $response = $this->httpDelete(sprintf('/v3/lists/%s', $address));
151
152 1
        return $this->hydrateResponse($response, DeleteResponse::class);
153
    }
154
}
155