Completed
Push — develop ( 51584f...15f6f1 )
by Chris
02:03
created

DNS::update()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 14
nc 8
nop 2
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));
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)
53
    {
54
        $data = [
55
            'type' => $type,
56
            'name' => $name,
57
            'content' => $content,
58
            'ttl' => $ttl
59
        ];
60
61
        $url = $this->getURL($this->URL) . '/';
62
        try {
63
            $this->makeRequest($url, 'POST', $data);
64
        } catch(\Exception $e) {
65
            //var_dump($e);
66
        }
67
        $response = $this->getResponse();
68
69
        if($response->success != true) {
70
            return false;
71
        }
72
73
        return $response->result;
74
    }
75
76
    public function update($id, $value)
77
    {
78
        $data = $value;
79
80
        if (!is_object($value)) {
81
            $info = $this->getInfo($id);
82
            $data = json_decode(json_encode($info), true);
83
            $data['content'] = $value;
84
        }
85
86
        $url = $this->getURL($this->URL) . '/' . $id;
87
        try {
88
            $this->makeRequest($url, 'PUT', $data);
89
        } catch(\Exception $e) {
90
            //var_dump($e);
91
        }
92
        $response = $this->getResponse();
93
94
        if($response->success != true) {
95
            return false;
96
        }
97
98
        return $response->result;
99
    }
100
101
    public function delete($id)
102
    {
103
        $url = $this->getURL($this->URL) . '/' . $id;
104
        try {
105
            $this->makeRequest($url, 'DELETE');
106
        } catch(\Exception $e) {
107
            //var_dump($e);
108
        }
109
110
        $response = $this->getResponse();
111
112
        if($response->success === true) {
113
            return true;
114
        }
115
116
        return false;
117
    }
118
119
    private function getURL($url)
120
    {
121
        return sprintf($url, $this->zone);
122
    }
123
}
124