Completed
Push — master ( d8ae00...12ed8a )
by Tobias
02:18
created

Complaint::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 15
loc 15
ccs 6
cts 8
cp 0.75
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.0625
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\Suppression;
13
14
use Mailgun\Api\HttpApi;
15
use Mailgun\Api\Pagination;
16
use Mailgun\Assert;
17
use Mailgun\Model\Suppression\Complaint\CreateResponse;
18
use Mailgun\Model\Suppression\Complaint\DeleteResponse;
19
use Mailgun\Model\Suppression\Complaint\IndexResponse;
20
use Mailgun\Model\Suppression\Complaint\ShowResponse;
21
22
/**
23
 * @see https://documentation.mailgun.com/api-suppressions.html#complaints
24
 *
25
 * @author Sean Johnson <[email protected]>
26
 */
27
class Complaint extends HttpApi
28
{
29
    use Pagination;
30
31
    /**
32
     * @param string $domain Domain to get complaints for
33
     * @param int    $limit  optional
34
     *
35
     * @return IndexResponse
36
     */
37 1 View Code Duplication
    public function index(string $domain, 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...
38
    {
39 1
        Assert::stringNotEmpty($domain);
40 1
        Assert::range($limit, 1, 10000, 'Limit parameter must be between 1 and 10000');
41
42
        $params = [
43 1
            'limit' => $limit,
44
        ];
45
46 1
        $response = $this->httpGet(sprintf('/v3/%s/complaints', $domain), $params);
47
48
        return $this->hydrateResponse($response, IndexResponse::class);
49
    }
50
51
    /**
52
     * @param string $domain  Domain to show complaint for
53
     * @param string $address Complaint address
54
     *
55
     * @return ShowResponse
56
     */
57 1
    public function show(string $domain, string $address)
58
    {
59 1
        Assert::stringNotEmpty($domain);
60 1
        Assert::stringNotEmpty($address);
61 1
        $response = $this->httpGet(sprintf('/v3/%s/complaints/%s', $domain, $address));
62
63
        return $this->hydrateResponse($response, ShowResponse::class);
64
    }
65
66
    /**
67
     * @param string $domain    Domain to create complaint for
68
     * @param string $address   Complaint address
69
     * @param string $createdAt (optional) rfc2822 compliant format. (new \DateTime())->format('r')
70
     *
71
     * @return CreateResponse
72
     */
73 1 View Code Duplication
    public function create(string $domain, string $address, string $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...
74
    {
75 1
        Assert::stringNotEmpty($domain);
76 1
        Assert::stringNotEmpty($address);
77
78 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...
79 1
        if (null !== $createdAt) {
80
            Assert::stringNotEmpty($createdAt);
81
            $params['created_at'] = $createdAt;
82
        }
83
84 1
        $response = $this->httpPost(sprintf('/v3/%s/complaints', $domain), $params);
85
86
        return $this->hydrateResponse($response, CreateResponse::class);
87
    }
88
89
    /**
90
     * @param string $domain  Domain to delete complaint for
91
     * @param string $address Complaint address
92
     *
93
     * @return DeleteResponse
94
     */
95 1 View Code Duplication
    public function delete(string $domain, string $address)
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...
96
    {
97 1
        Assert::stringNotEmpty($domain);
98 1
        Assert::stringNotEmpty($address);
99
100 1
        $response = $this->httpDelete(sprintf('/v3/%s/complaints/%s', $domain, $address));
101
102
        return $this->hydrateResponse($response, DeleteResponse::class);
103
    }
104
105
    /**
106
     * @param string $domain Domain to delete all bounces for
107
     *
108
     * @return DeleteResponse
109
     */
110 1
    public function deleteAll(string $domain)
111
    {
112 1
        Assert::stringNotEmpty($domain);
113
114 1
        $response = $this->httpDelete(sprintf('/v3/%s/complaints', $domain));
115
116
        return $this->hydrateResponse($response, DeleteResponse::class);
117
    }
118
}
119