Completed
Push — master ( 7e438f...abf2a0 )
by Tobias
01:56
created

MailingList   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 131
Duplicated Lines 9.16 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 3
dl 12
loc 131
ccs 38
cts 40
cp 0.95
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A member() 0 4 1
A pages() 12 12 1
A create() 0 18 1
A show() 0 8 1
B update() 0 24 6
A delete() 0 8 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;
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 View Code Duplication
    public function pages(int $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...
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')
67
    {
68 3
        Assert::stringNotEmpty($address);
69 1
        Assert::nullOrStringNotEmpty($name);
70 1
        Assert::nullOrStringNotEmpty($description);
71 1
        Assert::oneOf($accessLevel, ['readonly', 'members', 'everyone']);
72
73
        $params = [
74 1
            'address' => $address,
75 1
            'name' => $name,
76 1
            'description' => $description,
77 1
            'access_level' => $accessLevel,
78
        ];
79
80 1
        $response = $this->httpPost('/v3/lists', $params);
81
82 1
        return $this->hydrateResponse($response, CreateResponse::class);
83
    }
84
85
    /**
86
     * Returns a single mailing list.
87
     *
88
     * @param string $address Address of the mailing list
89
     *
90
     * @return ShowResponse
91
     *
92
     * @throws \Exception
93
     */
94 2
    public function show(string $address)
95
    {
96 2
        Assert::stringNotEmpty($address);
97
98 1
        $response = $this->httpGet(sprintf('/v3/lists/%s', $address));
99
100 1
        return $this->hydrateResponse($response, ShowResponse::class);
101
    }
102
103
    /**
104
     * Updates a mailing list.
105
     *
106
     * @param string $address    Address of the mailing list
107
     * @param array  $parameters Array of field => value pairs to update
108
     *
109
     * @return UpdateResponse
110
     *
111
     * @throws \Exception
112
     */
113 2
    public function update(string $address, array $parameters = [])
114
    {
115 2
        Assert::stringNotEmpty($address);
116 2
        Assert::isArray($parameters);
117
118 2
        foreach ($parameters as $field => $value) {
119
            switch ($field) {
120 2
                case 'address':
121 2
                case 'name':
122 2
                case 'description':
123 1
                    Assert::stringNotEmpty($value);
124
125 1
                    break;
126 1
                case 'access_level':
127 1
                    Assert::oneOf($value, ['readonly', 'members', 'everyone']);
128
129 1
                    break;
130
            }
131
        }
132
133 1
        $response = $this->httpPut(sprintf('/v3/lists/%s', $address), $parameters);
134
135 1
        return $this->hydrateResponse($response, UpdateResponse::class);
136
    }
137
138
    /**
139
     * Removes a mailing list from the domain.
140
     *
141
     * @param string $address Address of the mailing list
142
     *
143
     * @return DeleteResponse
144
     *
145
     * @throws \Exception
146
     */
147 1
    public function delete(string $address)
148
    {
149 1
        Assert::stringNotEmpty($address);
150
151 1
        $response = $this->httpDelete(sprintf('/v3/lists/%s', $address));
152
153 1
        return $this->hydrateResponse($response, DeleteResponse::class);
154
    }
155
}
156