Completed
Push — develop ( e482b1...20ddea )
by Chris
02:12
created

DNS::new()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 16
nc 3
nop 5
1
<?php
2
/*
3
 * @author Chris Hilsdon <[email protected]>
4
 *
5
 * @link https://api.cloudflare.com/#zone
6
 */
7
namespace Cloudflare;
8
9
class DNS extends Base
10
{
11
    protected $URL = "zones/%s/dns_records";
12
    protected $zone;
13
14
    public function __construct($cloudflare, $zone)
15
    {
16
        parent::__construct($cloudflare);
17
        $this->zone = $zone;
18
    }
19
20
    /*
21
     *
22
     * @link https://api.cloudflare.com/#zone-list-zones
23
     */
24
    public function fetch()
25
    {
26
        $this->makeRequest($this->getURL($this->URL . '?per_page=100'));
27
        $response = $this->getResponse();
28
29
        if($response->success != true) {
30
            return false;
31
        }
32
33
        return $response->result;
34
    }
35
36
    public function getInfo($id)
37
    {
38
        $url = "zones/%s/dns_records/" . $id;
39
        $this->makeRequest($this->getURL($url));
40
        $response = $this->getResponse();
41
42
        if($response->success != true) {
43
            return false;
44
        }
45
46
        return $response->result;
47
    }
48
49
    /**
50
     * @link https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record
51
     */
52
    public function new($type, $name, $content, $ttl = 1, $extra = [])
53
    {
54
        $data = [
55
            'type' => $type,
56
            'name' => $name,
57
            'content' => $content,
58
            'ttl' => $ttl
59
        ];
60
61
        $data = $data + $extra;
62
63
        $url = $this->getURL($this->URL) . '/';
64
        try {
65
            $this->makeRequest($url, 'POST', $data);
66
        } catch(\Exception $e) {
67
            //var_dump($e);
68
69
            return false;
70
        }
71
        $response = $this->getResponse();
72
73
        if ($response->success != true) {
74
            return false;
75
        }
76
77
        return $response->result;
78
    }
79
80
    public function update($id, $value)
81
    {
82
        $data = $value;
83
84
        if (!is_object($value)) {
85
            $info = $this->getInfo($id);
86
            $data = json_decode(json_encode($info), true);
87
            $data['content'] = $value;
88
        }
89
90
        $url = $this->getURL($this->URL) . '/' . $id;
91
        try {
92
            $this->makeRequest($url, 'PUT', $data);
93
        } catch(\Exception $e) {
94
            //var_dump($e);
95
        }
96
        $response = $this->getResponse();
97
98
        if($response->success != true) {
99
            return false;
100
        }
101
102
        return $response->result;
103
    }
104
105
    public function delete($id)
106
    {
107
        $url = $this->getURL($this->URL) . '/' . $id;
108
        try {
109
            $this->makeRequest($url, 'DELETE');
110
        } catch(\Exception $e) {
111
            //var_dump($e);
112
        }
113
114
        $response = $this->getResponse();
115
116
        if ($response->success === true) {
117
            return true;
118
        }
119
120
        return false;
121
    }
122
123
    private function getURL($url)
124
    {
125
        return sprintf($url, $this->zone);
126
    }
127
}
128