Completed
Push — master ( 453b10...bec1da )
by Tobias
04:06
created

Complaint   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 16.3 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 15
loc 92
ccs 28
cts 31
cp 0.9032
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 13 1
A show() 0 8 1
A create() 15 15 2
A delete() 0 9 1
A deleteAll() 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
/*
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\Suppression;
11
12
use Mailgun\Api\HttpApi;
13
use Mailgun\Api\Pagination;
14
use Mailgun\Assert;
15
use Mailgun\Model\Suppression\Complaint\CreateResponse;
16
use Mailgun\Model\Suppression\Complaint\DeleteResponse;
17
use Mailgun\Model\Suppression\Complaint\IndexResponse;
18
use Mailgun\Model\Suppression\Complaint\ShowResponse;
19
20
/**
21
 * @see https://documentation.mailgun.com/api-suppressions.html#complaints
22
 *
23
 * @author Sean Johnson <[email protected]>
24
 */
25
class Complaint extends HttpApi
26
{
27
    use Pagination;
28
29
    /**
30
     * @param string $domain Domain to get complaints for
31
     * @param int    $limit  optional
32
     *
33
     * @return IndexResponse
34
     */
35 1
    public function index($domain, $limit = 100)
36
    {
37 1
        Assert::stringNotEmpty($domain);
38 1
        Assert::range($limit, 1, 10000, 'Limit parameter must be between 1 and 10000');
39
40
        $params = [
41 1
            'limit' => $limit,
42 1
        ];
43
44 1
        $response = $this->httpGet(sprintf('/v3/%s/complaints', $domain), $params);
45
46 1
        return $this->hydrateResponse($response, IndexResponse::class);
47
    }
48
49
    /**
50
     * @param string $domain  Domain to show complaint for
51
     * @param string $address Complaint address
52
     *
53
     * @return ShowResponse
54
     */
55 1
    public function show($domain, $address)
56
    {
57 1
        Assert::stringNotEmpty($domain);
58 1
        Assert::stringNotEmpty($address);
59 1
        $response = $this->httpGet(sprintf('/v3/%s/complaints/%s', $domain, $address));
60
61 1
        return $this->hydrateResponse($response, ShowResponse::class);
62
    }
63
64
    /**
65
     * @param string $domain    Domain to create complaint for
66
     * @param string $address   Complaint address
67
     * @param string $createdAt (optional) rfc2822 compliant format. (new \DateTime())->format('r')
68
     *
69
     * @return CreateResponse
70
     */
71 1 View Code Duplication
    public function create($domain, $address, $createdAt = null)
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...
72
    {
73 1
        Assert::stringNotEmpty($domain);
74 1
        Assert::stringNotEmpty($address);
75
76 1
        $params['address'] = $address;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
77 1
        if (null !== $createdAt) {
78
            Assert::stringNotEmpty($createdAt);
79
            $params['created_at'] = $createdAt;
80
        }
81
82 1
        $response = $this->httpPost(sprintf('/v3/%s/complaints', $domain), $params);
83
84 1
        return $this->hydrateResponse($response, CreateResponse::class);
85
    }
86
87
    /**
88
     * @param string $domain  Domain to delete complaint for
89
     * @param string $address Complaint address
90
     *
91
     * @return DeleteResponse
92
     */
93 1
    public function delete($domain, $address)
94
    {
95 1
        Assert::stringNotEmpty($domain);
96 1
        Assert::stringNotEmpty($address);
97
98 1
        $response = $this->httpDelete(sprintf('/v3/%s/complaints/%s', $domain, $address));
99
100 1
        return $this->hydrateResponse($response, DeleteResponse::class);
101
    }
102
103
    /**
104
     * @param string $domain Domain to delete all bounces for
105
     *
106
     * @return DeleteResponse
107
     */
108 1
    public function deleteAll($domain)
109
    {
110 1
        Assert::stringNotEmpty($domain);
111
112 1
        $response = $this->httpDelete(sprintf('/v3/%s/complaints', $domain));
113
114 1
        return $this->hydrateResponse($response, DeleteResponse::class);
115
    }
116
}
117