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

Ip   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 23.81%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 82
ccs 5
cts 21
cp 0.2381
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 12 2
A domainIndex() 0 8 1
A show() 0 8 1
A assign() 0 13 1
A unassign() 0 9 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\Assert;
15
use Mailgun\Model\Ip\IndexResponse;
16
use Mailgun\Model\Ip\ShowResponse;
17
use Mailgun\Model\Ip\UpdateResponse;
18
use Psr\Http\Message\ResponseInterface;
19
20
/**
21
 * @see https://documentation.mailgun.com/en/latest/api-ips.html
22
 *
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
class Ip extends HttpApi
26
{
27
    /**
28
     * Returns a list of IPs.
29
     *
30
     * @return IndexResponse|ResponseInterface
31
     */
32
    public function index(?bool $dedicated = null)
33
    {
34
        $params = [];
35
        if (null !== $dedicated) {
36
            Assert::boolean($dedicated);
37
            $params['dedicated'] = $dedicated;
38
        }
39
40
        $response = $this->httpGet('/v3/ips', $params);
41
42
        return $this->hydrateResponse($response, IndexResponse::class);
43
    }
44
45
    /**
46
     * Returns a list of IPs assigned to a domain.
47
     *
48
     * @return IndexResponse|ResponseInterface
49
     */
50
    public function domainIndex(string $domain)
51
    {
52
        Assert::stringNotEmpty($domain);
53
54
        $response = $this->httpGet(sprintf('/v3/domains/%s/ips', $domain));
55
56
        return $this->hydrateResponse($response, IndexResponse::class);
57
    }
58
59
    /**
60
     * Returns a single ip.
61
     *
62
     * @return ShowResponse|ResponseInterface
63
     */
64
    public function show(string $ip)
65
    {
66
        Assert::ip($ip);
67
68
        $response = $this->httpGet(sprintf('/v3/ips/%s', $ip));
69
70
        return $this->hydrateResponse($response, ShowResponse::class);
71
    }
72
73
    /**
74
     * Assign a dedicated IP to the domain specified.
75
     *
76
     * @return UpdateResponse|ResponseInterface
77
     */
78 1
    public function assign(string $domain, string $ip)
79
    {
80 1
        Assert::stringNotEmpty($domain);
81 1
        Assert::ip($ip);
82
83
        $params = [
84 1
            'ip' => $ip,
85
        ];
86
87 1
        $response = $this->httpPost(sprintf('/v3/domains/%s/ips', $domain), $params);
88
89
        return $this->hydrateResponse($response, UpdateResponse::class);
90
    }
91
92
    /**
93
     * Unassign an IP from the domain specified.
94
     *
95
     * @return UpdateResponse|ResponseInterface
96
     */
97
    public function unassign(string $domain, string $ip)
98
    {
99
        Assert::stringNotEmpty($domain);
100
        Assert::ip($ip);
101
102
        $response = $this->httpDelete(sprintf('/v3/domains/%s/ips/%s', $domain, $ip));
103
104
        return $this->hydrateResponse($response, UpdateResponse::class);
105
    }
106
}
107