Completed
Pull Request — master (#4)
by keika
02:10
created

Service::createDomainInfoArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

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