Completed
Push — master ( 8c959e...6f1105 )
by Yassir
01:57
created

DomainRecord   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 158
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 12 1
A getById() 0 8 1
D create() 0 43 9
A update() 0 9 1
A updateData() 0 4 1
A updateFields() 0 8 1
A delete() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the DigitalOceanV2 library.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DigitalOceanV2\Api;
13
14
use DigitalOceanV2\Entity\DomainRecord as DomainRecordEntity;
15
use DigitalOceanV2\Exception\HttpException;
16
use DigitalOceanV2\Exception\InvalidRecordException;
17
18
/**
19
 * @author Yassir Hannoun <[email protected]>
20
 * @author Graham Campbell <[email protected]>
21
 */
22
class DomainRecord extends AbstractApi
23
{
24
    /**
25
     * @param string $domainName
26
     *
27
     * @return DomainRecordEntity[]
28
     */
29
    public function getAll($domainName)
30
    {
31
        $domainRecords = $this->adapter->get(sprintf('%s/domains/%s/records?per_page=%d', $this->endpoint, $domainName, 200));
32
33
        $domainRecords = json_decode($domainRecords);
34
35
        $this->extractMeta($domainRecords);
36
37
        return array_map(function ($domainRecord) {
38
            return new DomainRecordEntity($domainRecord);
39
        }, $domainRecords->domain_records);
40
    }
41
42
    /**
43
     * @param string $domainName
44
     * @param int    $id
45
     *
46
     * @return DomainRecordEntity
47
     */
48
    public function getById($domainName, $id)
49
    {
50
        $domainRecords = $this->adapter->get(sprintf('%s/domains/%s/records/%d', $this->endpoint, $domainName, $id));
51
52
        $domainRecords = json_decode($domainRecords);
53
54
        return new DomainRecordEntity($domainRecords->domain_record);
55
    }
56
57
    /**
58
     * @param string $domainName
59
     * @param string $type
60
     * @param string $name
61
     * @param string $data
62
     * @param int    $priority
63
     * @param int    $port
64
     * @param int    $weight
65
     * @param int    $ttl
66
     *
67
     * @throws HttpException|InvalidRecordException
68
     *
69
     * @return DomainRecordEntity
70
     */
71
    public function create($domainName, $type, $name, $data, $priority = null, $port = null, $weight = null, $ttl = null)
72
    {
73
        switch ($type = strtoupper($type)) {
74
            case 'A':
75
            case 'AAAA':
76
            case 'CNAME':
77
            case 'TXT':
78
                $content = ['name' => $name, 'type' => $type, 'data' => $data];
79
                break;
80
81
            case 'NS':
82
                $content = ['type' => $type, 'data' => $data];
83
                break;
84
85
            case 'SRV':
86
                $content = [
87
                    'name' => $name,
88
                    'type' => $type,
89
                    'data' => $data,
90
                    'priority' => (int) $priority,
91
                    'port' => (int) $port,
92
                    'weight' => (int) $weight,
93
                ];
94
                break;
95
96
            case 'MX':
97
                $content = ['type' => $type, 'name' => $name, 'data' => $data, 'priority' => $priority];
98
                break;
99
100
            default:
101
                throw new InvalidRecordException('The domain record type is invalid.');
102
        }
103
104
        if (null !== $ttl) {
105
            $content['ttl'] = $ttl;
106
        }
107
108
        $domainRecord = $this->adapter->post(sprintf('%s/domains/%s/records', $this->endpoint, $domainName), $content);
109
110
        $domainRecord = json_decode($domainRecord);
111
112
        return new DomainRecordEntity($domainRecord->domain_record);
113
    }
114
115
    /**
116
     * @param string      $domainName
117
     * @param int         $recordId
118
     * @param string|null $name
119
     * @param string|null $data
120
     * @param int|null    $priority
121
     * @param int|null    $port
122
     * @param int|null    $weight
123
     * @param int|null    $ttl
124
     *
125
     * @throws HttpException
126
     *
127
     * @return DomainRecordEntity
128
     */
129
    public function update($domainName, $recordId, $name = null, $data = null, $priority = null, $port = null, $weight = null, $ttl = null)
0 ignored issues
show
Unused Code introduced by
The parameter $ttl is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
    {
131
        $content = compact('name', 'data', 'priority', 'port', 'weight');
132
        $content = array_filter($content, function ($val) {
133
            return $val !== null;
134
        });
135
136
        return $this->updateFields($domainName, $recordId, $content);
137
    }
138
139
    /**
140
     * @param string $domainName
141
     * @param int    $recordId
142
     * @param string $data
143
     *
144
     * @throws HttpException
145
     *
146
     * @return DomainRecordEntity
147
     */
148
    public function updateData($domainName, $recordId, $data)
149
    {
150
        return $this->updateFields($domainName, $recordId, ['data' => $data]);
151
    }
152
153
    /**
154
     * @param string $domainName
155
     * @param int    $recordId
156
     * @param array  $fields
157
     *
158
     * @throws HttpException
159
     *
160
     * @return DomainRecordEntity
161
     */
162
    public function updateFields($domainName, $recordId, $fields)
163
    {
164
        $domainRecord = $this->adapter->put(sprintf('%s/domains/%s/records/%d', $this->endpoint, $domainName, $recordId), $fields);
165
166
        $domainRecord = json_decode($domainRecord);
167
168
        return new DomainRecordEntity($domainRecord->domain_record);
169
    }
170
171
    /**
172
     * @param string $domainName
173
     * @param int    $recordId
174
     */
175
    public function delete($domainName, $recordId)
176
    {
177
        $this->adapter->delete(sprintf('%s/domains/%s/records/%d', $this->endpoint, $domainName, $recordId));
178
    }
179
}
180