Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
13 / 13
CRAP
100.00% covered (success)
100.00%
1 / 1
DNSService
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
13 / 13
15
100.00% covered (success)
100.00%
1 / 1
 getDomains
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDomain
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createDomain
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 deleteDomain
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 updateDomain
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSOAInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 updateSOAInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDNSSecInfo
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 createRecord
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRecords
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRecord
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 updateRecord
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deleteRecord
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Vultr\VultrPhp\Services\DNS;
6
7use Vultr\VultrPhp\Services\VultrService;
8use Vultr\VultrPhp\Util\ListOptions;
9use Vultr\VultrPhp\Util\VultrUtil;
10use Vultr\VultrPhp\VultrClientException;
11
12/**
13 * DNS service handler, for dns endpoints.
14 *
15 * @see https://www.vultr.com/api/#tag/dns
16 */
17class DNSService extends VultrService
18{
19    /**
20     * List DNS domains on the account.
21     *
22     * @see https://www.vultr.com/api/#tag/dns/operation/list-dns-domains
23     * @param $options - ListOptions|null - Interact via reference.
24     * @throws DNSException
25     * @return Domain[]
26     */
27    public function getDomains(?ListOptions &$options = null) : array
28    {
29        return $this->getListObjects('domains', new Domain(), $options);
30    }
31
32    /**
33     * Get a specific domain on the account.
34     *
35     * @see https://www.vultr.com/api/#tag/dns/operation/get-dns-domain
36     * @param $domain - string - Example: example.com
37     * @throws DNSException
38     * @return Domain
39     */
40    public function getDomain(string $domain) : Domain
41    {
42        return $this->getObject('domains/'.$domain, new Domain());
43    }
44
45    /**
46     * Create a DNS domain. If no ip address is supplied a domain with no records will be created.
47     *
48     * @see https://www.vultr.com/api/#operation/create-dns-domain
49     * @param $domain - string - Example: example.com
50     * @param $dns_sec - string
51     * @param $ip - string
52     * @throws DNSException
53     * @return void
54     */
55    public function createDomain(string $domain, string $dns_sec = 'disabled', string $ip = '')
56    {
57        $params = [
58            'domain'  => $domain,
59            'dns_sec' => $dns_sec
60        ];
61
62        if ($ip != '')
63        {
64            $params['ip'] = $ip;
65        }
66
67        return $this->createObject('domains', new Domain(), $params);
68    }
69
70    /**
71     * Delete the domain and all of its records.
72     *
73     * @see https://www.vultr.com/api/#tag/dns/operation/delete-dns-domain
74     * @param $domain - string - Example: example.com
75     * @throws DNSException
76     * @return void
77     */
78    public function deleteDomain(string $domain) : void
79    {
80        $this->deleteObject('domains/'.$domain, new Domain());
81    }
82
83    /**
84     * Update the domain to enabled/disable other options.
85     *
86     * @see https://www.vultr.com/api/#tag/dns/operation/update-dns-domain
87     * @throws DNSException
88     * @return void
89     */
90    public function updateDomain(Domain $domain) : void
91    {
92        $this->patchObject('domains/'.$domain->getDomain(), $domain);
93    }
94
95    /**
96     * Get SOA(start of authority) information for the domain name.
97     *
98     * @see https://www.vultr.com/api/#tag/dns/operation/get-dns-domain-soa
99     * @param $domain - string - Example: example.com
100     * @throws DNSException
101     * @return DNSSOA
102     */
103    public function getSOAInfo(string $domain) : DNSSOA
104    {
105        return $this->getObject('domains/'.$domain.'/soa', new DNSSOA());
106    }
107
108    /**
109     * Update the SOA information on the domain name. All attributes are optional.
110     *
111     * @see https://www.vultr.com/api/#tag/dns/operation/update-dns-domain-soa
112     * @param $domain - string - Example: example.com
113     * @param $soa - DNSSOA
114     * @throws DNSException
115     * @return void
116     */
117    public function updateSOAInfo(string $domain, DNSSOA $soa) : void
118    {
119        $this->patchObject('domains/'.$domain.'/soa', $soa);
120    }
121
122    /**
123     * Get DNSSEC information for the domain name.
124     *
125     * @see https://www.vultr.com/api/#tag/dns/operation/get-dns-domain-dnssec
126     * @param $domain - string - Example: example.com
127     * @throws DNSException
128     * @throws VultrException
129     * @return array
130     */
131    public function getDNSSecInfo(string $domain) : array
132    {
133        $client = $this->getClientHandler();
134
135        try
136        {
137            $response = $client->get('domains/'.$domain.'/dnssec');
138        }
139        catch (VultrClientException $e)
140        {
141            throw new DNSException('Failed to get dns sec information: '.$e->getMessage(), $e->getHTTPCode(), $e);
142        }
143
144        return VultrUtil::decodeJSON((string)$response->getBody(), true)['dns_sec'];
145    }
146
147    /**
148     * Create a DNS record for the domain name.
149     *
150     * @see https://www.vultr.com/api/#tag/dns/operation/create-dns-domain-record
151     * @param $domain - string - Example: example.com
152     * @param $record - Record
153     * @throws DNSException
154     * @return Record
155     */
156    public function createRecord(string $domain, Record $record) : Record
157    {
158        return $this->createObject('domains/'.$domain.'/records', new Record(), $record->getInitializedProps());
159    }
160
161    /**
162     * Get DNS records for a given domain name.
163     *
164     * @see https://www.vultr.com/api/#tag/dns/operation/create-dns-domain-record
165     * @param $domain - string - Example: example.com
166     * @param $options - ListOptions|null - Interact via reference.
167     * @throws DNSException
168     * @return array
169     */
170    public function getRecords(string $domain, ?ListOptions &$options = null) : array
171    {
172        return $this->getListObjects('domains/'.$domain.'/records', new Record(), $options);
173    }
174
175    /**
176     * Get a specific DNS record for a given domain name.
177     *
178     * @see https://www.vultr.com/api/#tag/dns/operation/get-dns-domain-record
179     * @param $domain - string - Example: example.com
180     * @param $record_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60
181     * @throws DNSException
182     * @return array
183     */
184    public function getRecord(string $domain, string $record_id) : Record
185    {
186        return $this->getObject('domains/'.$domain.'/records/'.$record_id, new Record());
187    }
188
189    /**
190     * Update the DNS record for the domain name.
191     *
192     * @see https://www.vultr.com/api/#tag/dns/operation/update-dns-domain-record
193     * @param $domain - string - Example: example.com
194     * @param $record - Record - Fully initialized object.
195     * @throws DNSException
196     * @return void
197     */
198    public function updateRecord(string $domain, Record $record) : void
199    {
200        $this->patchObject('domains/'.$domain.'/records/'.$record->getId(), $record);
201    }
202
203    /**
204     * Delete a DNS record for a given domain name.
205     *
206     * @see https://www.vultr.com/api/#tag/dns/operation/delete-dns-domain-record
207     * @param $domain - string - Example: example.com
208     * @param $record_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60
209     * @throws DNSException
210     * @return void
211     */
212    public function deleteRecord(string $domain, string $record_id) : void
213    {
214        $this->deleteObject('domains/'.$domain.'/records/'.$record_id, new Record());
215    }
216}