Service::postDomain()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 9.4286
cc 1
eloc 15
nc 1
nop 3
crap 1
1
<?php
2
3
namespace keika299\ConohaAPI\DNS;
4
5
6
use keika299\ConohaAPI\Common\Network\Request;
7
use keika299\ConohaAPI\Common\Service\AbstractService;
8
9
/**
10
 * Class Service
11
 *
12
 * This class connect to ConoHa DNS service.
13
 *
14
 * @package keika299\ConohaAPI\DNS
15
 */
16
class Service extends AbstractService
17
{
18
    /**
19
     * Get hosting domain information.
20
     *
21
     * See https://www.conoha.jp/docs/paas-dns-get-servers-hosting-a-domain.html
22
     *
23
     * @param string $domainId
24
     * @return mixed
25
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
26
     */
27 1
    public function getDomainHostingInfo($domainId)
28
    {
29 1
        $request = (new Request())
30 1
            ->setMethod('GET')
31 1
            ->setBaseURI($this->baseURI)
32 1
            ->setURI('/v1/domains/'. $domainId .'/servers')
33 1
            ->setAccept('application/json')
34 1
            ->setToken($this->token->getToken());
35
36 1
        $response = $request->exec();
37 1
        return $response->getJson();
38
    }
39
40
    /**
41
     * Get hosting domain list.
42
     *
43
     * See https://www.conoha.jp/docs/paas-dns-get-servers-hosting-a-domain.html
44
     *
45
     * @param array $options
46
     * @return mixed
47
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
48
     */
49 1
    public function getDomainList($options = array())
50
    {
51 1
        $request = (new Request())
52 1
            ->setMethod('GET')
53 1
            ->setBaseURI($this->baseURI)
54 1
            ->setURI('/v1/domains')
55 1
            ->setQuery($options)
56 1
            ->setAccept('application/json')
57 1
            ->setToken($this->token->getToken());
58
59 1
        $response = $request->exec();
60 1
        return $response->getJson();
61
    }
62
63
64
    /**
65
     * Create domain.
66
     *
67
     * $name is your domain name.
68
     * This name require to add period that last.
69
     * (ex: example.com.)
70
     *
71
     * $email is domain owner's email address.
72
     *
73
     * $options is ttl, description, and gslb
74
     * 'ttl' is ttl sec (this is int value).
75
     * 'description' is description of this domain.
76
     * 'glsb' is enable or disable GLSB (this is bool).
77
     *
78
     *
79
     * See https://www.conoha.jp/docs/paas-dns-create-domain.html
80
     *
81
     * @param $name
82
     * @param $email
83
     * @param array $options
84
     * @return mixed
85
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
86
     */
87 1
    public function postDomain($name, $email, $options = array())
88
    {
89 1
        $optionArray = array_merge(array(
90 1
            'name' => $name,
91
            'email' => $email
92 1
        ), $this->createDomainInfoArray($options));
93
94 1
        $request = (new Request())
95 1
            ->setMethod('POST')
96 1
            ->setBaseURI($this->baseURI)
97 1
            ->setURI('/v1/domains')
98 1
            ->setAccept('application/json')
99 1
            ->setContentType('application/json')
100 1
            ->setToken($this->client->getToken())
101 1
            ->setJson($optionArray);
102
103 1
        $response = $request->exec();
104 1
        return $response->getJson();
105
    }
106
107
    /**
108
     * Delete domain.
109
     *
110
     * $domainId is domain UUID.
111
     * It can get 'getDomainList' function.
112
     *
113
     * See https://www.conoha.jp/docs/paas-dns-delete-a-domain.html
114
     *
115
     * @param string $domainId
116
     * @return null
117
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
118
     */
119 1
    public function deleteDomain($domainId)
120
    {
121 1
        $request = (new Request())
122 1
            ->setMethod('DELETE')
123 1
            ->setBaseURI($this->baseURI)
124 1
            ->setURI('/v1/domains/' . $domainId)
125 1
            ->setToken($this->token->getToken());
126
127 1
        $request->exec();
128 1
        return null;
129
    }
130
131
    /**
132
     * Get domain information.
133
     *
134
     * $domainId is domain UUID.
135
     * It can get 'getDomainList' function.
136
     *
137
     * See https://www.conoha.jp/docs/paas-dns-get-a-domain.html
138
     *
139
     * @param string $domainId
140
     * @return mixed
141
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
142
     */
143 1
    public function getDomainInfo($domainId)
144
    {
145 1
        $request = (new Request())
146 1
            ->setMethod('GET')
147 1
            ->setBaseURI($this->baseURI)
148 1
            ->setURI('/v1/domains/' . $domainId)
149 1
            ->setAccept('application/json')
150 1
            ->setContentType('application/json')
151 1
            ->setToken($this->token->getToken());
152
153 1
        $response = $request->exec();
154 1
        return $response->getJson();
155
    }
156
157
    /**
158
     * Change domain information.
159
     *
160
     * $options is ttl, emails, description, and gslb
161
     * 'ttl' is ttl sec (this is int value).
162
     * 'email' is domain owner's email address.
163
     * 'description' is description of this domain.
164
     * 'glsb' is enable or disable GLSB (this is bool).
165
     *
166
     * See https://www.conoha.jp/docs/paas-dns-update-a-domain.html
167
     *
168
     * @param string $domainId
169
     * @param array $options
170
     * @return mixed
171
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
172
     */
173 1
    public function putDomainInfo($domainId, $options = array())
174
    {
175 1
        $request = (new Request())
176 1
            ->setMethod('PUT')
177 1
            ->setBaseURI($this->baseURI)
178 1
            ->setURI('/v1/domains/' . $domainId)
179 1
            ->setAccept('application/json')
180 1
            ->setToken($this->client->getToken())
181 1
            ->setJson($this->createDomainInfoArray($options));
182
183 1
        $response = $request->exec();
184 1
        return $response->getJson();
185
    }
186
187
    /**
188
     * Get domain records.
189
     *
190
     * $domainId is domain UUID.
191
     *
192
     * See https://www.conoha.jp/docs/paas-dns-list-records-in-a-domain.html
193
     *
194
     * @param string $domainId
195
     * @return mixed
196
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
197
     */
198 1
    public function getDomainRecords($domainId)
199
    {
200 1
        $request = (new Request())
201 1
            ->setMethod('GET')
202 1
            ->setBaseURI($this->baseURI)
203 1
            ->setURI('/v1/domains/'. $domainId .'/records')
204 1
            ->setAccept('application/json')
205 1
            ->setToken($this->token->getToken());
206
207 1
        $response = $request->exec();
208 1
        return $response->getJson();
209
    }
210
211
212
    /**
213
     * Create new domain record.
214
     *
215
     * $domainId is domain UUID.
216
     * $name is domain record. This is FQDN (ex: www.example.com.)
217
     * $type is domain record type.
218
     * You can set [A/AAAA/MX/CNAME/TXT/SRV/NS/PTR].
219
     * $data is domain record value.
220
     *
221
     * $option can set priority, ttl, description, gslb_region, gslb_weight, and gslb_check.
222
     * 'priority' is require if you create MX or SRV record (This is int value).
223
     * 'description' is domain description.
224
     * 'gslb_region' is GSLB's region. You can set [JP/US/SG/AUTO].
225
     * 'gslb_weight' is GSLB's priority.
226
     * 'gslb_check' is GSLB's health check port. 0 is off, and other number is to check that port.
227
     *
228
     * See https://www.conoha.jp/docs/paas-dns-create-record.html
229
     *
230
     * @param string $domainId
231
     * @param string $name
232
     * @param string $type
233
     * @param string $data
234
     * @param array $options
235
     * @return mixed
236
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
237
     */
238 1
    public function postDomainRecord($domainId, $name, $type, $data, $options = array())
239
    {
240 1
        $optionArray = array_merge(array(
241 1
            'name' => $name,
242 1
            'type' => $type,
243
            'data' => $data
244 1
        ), $options);
245
246 1
        $request = (new Request())
247 1
            ->setMethod('POST')
248 1
            ->setBaseURI($this->baseURI)
249 1
            ->setURI('/v1/domains/'. $domainId .'/records')
250 1
            ->setAccept('application/json')
251 1
            ->setContentType('application/json')
252 1
            ->setToken($this->client->getToken())
253 1
            ->setJson($optionArray);
254
255 1
        $response = $request->exec();
256 1
        return $response->getJson();
257
    }
258
259
    /**
260
     * Delete domain record.
261
     *
262
     * $domainId is domain UUID
263
     * $recordId is record UUID
264
     *
265
     * See https://www.conoha.jp/docs/paas-dns-delete-a-record.html
266
     *
267
     * @param string $domainId
268
     * @param string $recordId
269
     * @return null
270
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
271
     */
272 1
    public function deleteDomainRecord($domainId, $recordId)
273
    {
274 1
        $request = (new Request())
275 1
            ->setMethod('DELETE')
276 1
            ->setBaseURI($this->baseURI)
277 1
            ->setURI('/v1/domains/'. $domainId .'/records/'. $recordId)
278 1
            ->setToken($this->token->getToken());
279
280 1
        $request->exec();
281 1
        return null;
282
    }
283
284
    /**
285
     * Get domain record information.
286
     *
287
     * $domainId is domain UUID
288
     * $recordId is record UUID
289
     *
290
     * See https://www.conoha.jp/docs/paas-dns-get-a-record.html
291
     *
292
     * @param $domainId
293
     * @param $recordId
294
     * @return mixed
295
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
296
     */
297 1
    public function getDomainRecord($domainId, $recordId)
298
    {
299 1
        $request = (new Request())
300 1
            ->setMethod('GET')
301 1
            ->setBaseURI($this->baseURI)
302 1
            ->setURI('/v1/domains/'. $domainId .'/records/'. $recordId)
303 1
            ->setAccept('application/json')
304 1
            ->setToken($this->token->getToken());
305
306 1
        $response = $request->exec();
307 1
        return $response->getJson();
308
    }
309
310
    /**
311
     * Change domain record settings.
312
     *
313
     * $domainId is domain UUID
314
     * $recordId is record UUID
315
     *
316
     * $options can set name, type, priority, data, ttl, description, gslb_region, gslb_weight, and gslb_check.
317
     *
318
     * See https://www.conoha.jp/docs/paas-dns-update-a-record.html
319
     *
320
     * @param $domainId
321
     * @param $recordId
322
     * @param array $options
323
     * @return mixed
324
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
325
     */
326 1
    public function putDomainRecord($domainId, $recordId, $options = array())
327
    {
328 1
        $request = (new Request())
329 1
            ->setMethod('PUT')
330 1
            ->setBaseURI($this->baseURI)
331 1
            ->setURI('/v1/domains/'. $domainId .'/records/'. $recordId)
332 1
            ->setAccept('application/json')
333 1
            ->setContentType('application/json')
334 1
            ->setToken($this->client->getToken())
335 1
            ->setJson($options);
336
337 1
        $response = $request->exec();
338 1
        return $response->getJson();
339
    }
340
341
    /**
342
     * Post zone file.
343
     *
344
     * $zoneText is zone file's text data.
345
     *
346
     * @param string $zoneText
347
     * @return mixed
348
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
349
     */
350 1
    public function postZoneFile($zoneText)
351
    {
352 1
        $request = (new Request())
353 1
            ->setMethod('POST')
354 1
            ->setBaseURI($this->baseURI)
355 1
            ->setURI('/v2/zones')
356 1
            ->setContentType('text/dns')
357 1
            ->setToken($this->client->getToken())
358 1
            ->setBody($zoneText);
359
360 1
        $response = $request->exec();
361 1
        return $response->getJson();
362
    }
363
364
    /**
365
     * Get zone file.
366
     *
367
     * $domainId is domain UUID.
368
     *
369
     * @param string $domainId
370
     * @return string
371
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
372
     */
373 1
    public function getZoneFile($domainId)
374
    {
375 1
        $request = (new Request())
376 1
            ->setMethod('GET')
377 1
            ->setBaseURI($this->baseURI)
378 1
            ->setURI('/v2/zones/'. $domainId)
379 1
            ->setAccept('text/dns')
380 1
            ->setToken($this->token->getToken());
381
382 1
        $response = $request->exec();
383 1
        return $response->getBody();
384
    }
385
386 2
    private function createDomainInfoArray($options)
387
    {
388 2
        if (!isset($options['gslb'])) {
389 2
            return $options;
390
        }
391
392 1
        if ($options['gslb']) {
393 1
            $options['gslb'] = 1;
394 1
            return $options;
395
        }
396
397
        $options['gslb'] = 0;
398
        return $options;
399
    }
400
401
}
402