Completed
Push — master ( e6cf7b...67f8fd )
by Yassir
12s
created

DomainRecord::create()   C

Complexity

Conditions 10
Paths 17

Size

Total Lines 47
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 47
rs 5.1578
cc 10
eloc 33
nc 17
nop 10

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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    $flags
66
     * @param int    $tag
67
     * @param int    $ttl
68
     *
69
     * @throws HttpException|InvalidRecordException
70
     *
71
     * @return DomainRecordEntity
72
     */
73
    public function create($domainName, $type, $name, $data, $priority = null, $port = null, $weight = null, $flags = null, $tag = null, $ttl = null)
74
    {
75
        switch ($type = strtoupper($type)) {
76
            case 'A':
77
            case 'AAAA':
78
            case 'CNAME':
79
            case 'TXT':
80
                $content = ['name' => $name, 'type' => $type, 'data' => $data];
81
                break;
82
83
            case 'NS':
84
                $content = ['type' => $type, 'data' => $data];
85
                break;
86
87
            case 'SRV':
88
                $content = [
89
                    'name' => $name,
90
                    'type' => $type,
91
                    'data' => $data,
92
                    'priority' => (int) $priority,
93
                    'port' => (int) $port,
94
                    'weight' => (int) $weight,
95
                ];
96
                break;
97
98
            case 'MX':
99
                $content = ['type' => $type, 'name' => $name, 'data' => $data, 'priority' => $priority];
100
                break;
101
102
            case 'CAA':
103
                $content = ['type' => $type, 'name' => $name, 'data' => $data, 'flags' => $flags, 'tag' => $tag];
104
                break;
105
106
            default:
107
                throw new InvalidRecordException('The domain record type is invalid.');
108
        }
109
110
        if (null !== $ttl) {
111
            $content['ttl'] = $ttl;
112
        }
113
114
        $domainRecord = $this->adapter->post(sprintf('%s/domains/%s/records', $this->endpoint, $domainName), $content);
115
116
        $domainRecord = json_decode($domainRecord);
117
118
        return new DomainRecordEntity($domainRecord->domain_record);
119
    }
120
121
    /**
122
     * @param string      $domainName
123
     * @param int         $recordId
124
     * @param string|null $name
125
     * @param string|null $data
126
     * @param int|null    $priority
127
     * @param int|null    $port
128
     * @param int|null    $weight
129
     * @param int|null    $flags
130
     * @param int|null    $tag
131
     * @param int|null    $ttl
132
     *
133
     * @throws HttpException
134
     *
135
     * @return DomainRecordEntity
136
     */
137
    public function update($domainName, $recordId, $name = null, $data = null, $priority = null, $port = null, $weight = null, $flags = null, $tag = null, $ttl = null)
138
    {
139
        $content = compact('name', 'data', 'priority', 'port', 'weight', 'flags', 'tag', 'ttl');
140
141
        $content = array_filter($content, function ($val) {
142
            return $val !== null;
143
        });
144
145
        return $this->updateFields($domainName, $recordId, $content);
146
    }
147
148
    /**
149
     * @param string $domainName
150
     * @param int    $recordId
151
     * @param string $data
152
     *
153
     * @throws HttpException
154
     *
155
     * @return DomainRecordEntity
156
     */
157
    public function updateData($domainName, $recordId, $data)
158
    {
159
        return $this->updateFields($domainName, $recordId, ['data' => $data]);
160
    }
161
162
    /**
163
     * @param string $domainName
164
     * @param int    $recordId
165
     * @param array  $fields
166
     *
167
     * @throws HttpException
168
     *
169
     * @return DomainRecordEntity
170
     */
171
    public function updateFields($domainName, $recordId, $fields)
172
    {
173
        $domainRecord = $this->adapter->put(sprintf('%s/domains/%s/records/%d', $this->endpoint, $domainName, $recordId), $fields);
174
175
        $domainRecord = json_decode($domainRecord);
176
177
        return new DomainRecordEntity($domainRecord->domain_record);
178
    }
179
180
    /**
181
     * @param string $domainName
182
     * @param int    $recordId
183
     */
184
    public function delete($domainName, $recordId)
185
    {
186
        $this->adapter->delete(sprintf('%s/domains/%s/records/%d', $this->endpoint, $domainName, $recordId));
187
    }
188
}
189