Completed
Push — master ( 49221f...14cce3 )
by keika
02:12
created

Service::getDomainInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

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