DomainRecord::list()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
 *   This file is part of the Vultr PHP library.
4
 *
5
 *   (c) Albert Leitato <[email protected]>
6
 *
7
 *   For the full copyright and license information, please view the LICENSE
8
 *   file that was distributed with this source code.
9
 */
10
namespace Vultr\Api;
11
12
use Vultr\Entity\DomainRecord as DomainRecordEntity;
13
use Vultr\Exceptions\HttpException;
14
use Vultr\Exceptions\InvalidRecordException;
15
16
/**
17
 * @author Albert Leitato <[email protected]>
18
 */
19
class DomainRecord extends AbstractApi
20
{
21
    /**
22
     * List all the records associated with a particular domain.
23
     *
24
     * @param string $domain Domain to list records for
25
     *
26
     * @return DomainRecordEntity
27
     */
28
    public function list($domain)
29
    {
30
        $domainRecords = $this->adapter->get(\sprintf('%s/dns/records?domain=%s', $this->endpoint, $domain));
31
32
        return $this->handleResponse($domainRecords, DomainRecordEntity::class, true);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->handleResponse($d...inRecord::class, true); of type array|object adds the type array to the return on line 32 which is incompatible with the return type documented by Vultr\Api\DomainRecord::list of type Vultr\Entity\DomainRecord.
Loading history...
33
    }
34
35
    /**
36
     * Add a DNS record.
37
     *
38
     * @param string $domain   Domain name to add record to
39
     * @param string $type     Type (A, AAAA, MX, etc) of record
40
     * @param string $name     Name (subdomain) of record
41
     * @param string $data     Data for this record
42
     * @param int    $priority (only required for MX and SRV) Priority of this record (omit the priority from the data)
43
     * @param int    $ttl      TTL of this record
44
     *
45
     * @throws HttpException|InvalidRecordException
46
     */
47
    public function create($domain, $type, $name, $data, $priority = null, $ttl = null)
48
    {
49
        switch ($type = \strtoupper($type)) {
50
            case 'A':
51
            case 'AAAA':
52
            case 'CNAME':
53
            case 'TXT':
54
                $content = compact('type', 'data', 'name');
55
                break;
56
57
            case 'NS':
58
                $content = compact('type', 'data');
59
                break;
60
61
            case 'SRV':
62
            case 'MX':
63
                $content = compact('type', 'data', 'name', 'priority');
64
                break;
65
66
            default:
67
                throw new InvalidRecordException('The domain record type is invalid.');
68
        }
69
        $content['domain'] = $domain;
70
        if (null !== $ttl) {
71
            $content['ttl'] = $ttl;
72
        }
73
74
        return $this->adapter->post(sprintf('%s/dns/create_record', $this->endpoint), http_build_query($content));
75
    }
76
77
    /**
78
     * @param string      $domain   Domain name to update record
79
     * @param int         $recordId ID of record to update
80
     * @param string|null $name     Name (subdomain) of record
81
     * @param string|null $data     Data for this record
82
     * @param int|null    $priority Priority of this record
83
     * @param int|null    $ttl      TTL of this record
84
     *
85
     * @throws HttpException
86
     *
87
     * @return DomainRecordEntity
88
     */
89
    public function update($domain, $recordId, $name = null, $data = null, $priority = null, $ttl = null)
90
    {
91
        $content = \compact('name', 'data', 'priority', 'ttl', 'domain');
92
        $content = \array_filter($content, function ($val) {
93
            return $val !== null;
94
        });
95
        $content['RECORDID'] = $recordId;
96
97
        $this->adapter->post(\sprintf('%s/dns/update_record', $this->endpoint), http_build_query($content));
98
    }
99
100
    /**
101
     * Delete an individual DNS record.
102
     *
103
     * @param string $domain   Domain name to delete record from
104
     * @param int    $recordId ID of record to delete
105
     *
106
     * @throws HttpException
107
     */
108
    public function delete($domain, $recordId)
109
    {
110
        $data = [
111
            'domain'   => $domain,
112
            'RECORDID' => $recordId,
113
        ];
114
        $this->adapter->post(\sprintf('%s/dns/delete_record', $this->endpoint), http_build_query($data));
115
    }
116
}
117