Completed
Push — master ( 4641cf...cd08d9 )
by David
02:22 queued 52s
created

MailingList   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 3
dl 0
loc 133
ccs 40
cts 42
cp 0.9524
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A member() 0 4 1
A pages() 0 12 1
A create() 0 20 1
A show() 0 8 1
B update() 0 24 6
A delete() 0 8 1
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;
13
14
use Mailgun\Api\MailingList\Member;
15
use Mailgun\Assert;
16
use Mailgun\Model\MailingList\CreateResponse;
17
use Mailgun\Model\MailingList\DeleteResponse;
18
use Mailgun\Model\MailingList\PagesResponse;
19
use Mailgun\Model\MailingList\ShowResponse;
20
use Mailgun\Model\MailingList\UpdateResponse;
21
22
/**
23
 * @see https://documentation.mailgun.com/en/latest/api-mailinglists.html
24
 */
25
class MailingList extends HttpApi
26
{
27
    public function member(): Member
28
    {
29
        return new Member($this->httpClient, $this->requestBuilder, $this->hydrator);
0 ignored issues
show
Bug introduced by
It seems like $this->hydrator can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
30
    }
31
32
    /**
33
     * Returns a paginated list of mailing lists on the domain.
34
     *
35
     * @param int $limit Maximum number of records to return (optional: 100 by default)
36
     *
37
     * @return PagesResponse
38
     *
39
     * @throws \Exception
40
     */
41 2
    public function pages(int $limit = 100)
42
    {
43 2
        Assert::range($limit, 1, 1000);
44
45
        $params = [
46 1
            'limit' => $limit,
47
        ];
48
49 1
        $response = $this->httpGet('/v3/lists/pages', $params);
50
51 1
        return $this->hydrateResponse($response, PagesResponse::class);
52
    }
53
54
    /**
55
     * Creates a new mailing list on the current domain.
56
     *
57
     * @param string $address     Address for the new mailing list
58
     * @param string $name        Name for the new mailing list (optional)
59
     * @param string $description Description for the new mailing list (optional)
60
     * @param string $accessLevel List access level, one of: readonly (default), members, everyone
61
     *
62
     * @return CreateResponse
63
     *
64
     * @throws \Exception
65
     */
66 3
    public function create(string $address, string $name = null, string $description = null, string $accessLevel = 'readonly', string $replyPreference = 'list')
67
    {
68 3
        Assert::stringNotEmpty($address);
69 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...
70 1
        Assert::nullOrStringNotEmpty($description);
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...
71 1
        Assert::oneOf($accessLevel, ['readonly', 'members', 'everyone']);
72 1
        Assert::oneOf($replyPreference, ['list', 'sender']);
73
74
        $params = [
75 1
            'address' => $address,
76 1
            'name' => $name,
77 1
            'description' => $description,
78 1
            'access_level' => $accessLevel,
79 1
            'reply_preference' => $replyPreference
80
        ];
81
82 1
        $response = $this->httpPost('/v3/lists', $params);
83
84 1
        return $this->hydrateResponse($response, CreateResponse::class);
85
    }
86
87
    /**
88
     * Returns a single mailing list.
89
     *
90
     * @param string $address Address of the mailing list
91
     *
92
     * @return ShowResponse
93
     *
94
     * @throws \Exception
95
     */
96 2
    public function show(string $address)
97
    {
98 2
        Assert::stringNotEmpty($address);
99
100 1
        $response = $this->httpGet(sprintf('/v3/lists/%s', $address));
101
102 1
        return $this->hydrateResponse($response, ShowResponse::class);
103
    }
104
105
    /**
106
     * Updates a mailing list.
107
     *
108
     * @param string $address    Address of the mailing list
109
     * @param array  $parameters Array of field => value pairs to update
110
     *
111
     * @return UpdateResponse
112
     *
113
     * @throws \Exception
114
     */
115 2
    public function update(string $address, array $parameters = [])
116
    {
117 2
        Assert::stringNotEmpty($address);
118 2
        Assert::isArray($parameters);
119
120 2
        foreach ($parameters as $field => $value) {
121
            switch ($field) {
122 2
                case 'address':
123 2
                case 'name':
124 2
                case 'description':
125 1
                    Assert::stringNotEmpty($value);
126
127 1
                    break;
128 1
                case 'access_level':
129 1
                    Assert::oneOf($value, ['readonly', 'members', 'everyone']);
130
131 1
                    break;
132
            }
133
        }
134
135 1
        $response = $this->httpPut(sprintf('/v3/lists/%s', $address), $parameters);
136
137 1
        return $this->hydrateResponse($response, UpdateResponse::class);
138
    }
139
140
    /**
141
     * Removes a mailing list from the domain.
142
     *
143
     * @param string $address Address of the mailing list
144
     *
145
     * @return DeleteResponse
146
     *
147
     * @throws \Exception
148
     */
149 1
    public function delete(string $address)
150
    {
151 1
        Assert::stringNotEmpty($address);
152
153 1
        $response = $this->httpDelete(sprintf('/v3/lists/%s', $address));
154
155 1
        return $this->hydrateResponse($response, DeleteResponse::class);
156
    }
157
}
158