Completed
Push — develop ( eb315e...09dc45 )
by Chris
03:29
created

DNS   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 111
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fetch() 0 11 2
A getInfo() 0 12 2
A new() 0 23 3
A update() 0 20 3
A delete() 0 17 3
A getURL() 0 4 1
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
        $info = $this->getInfo($id);
79
        $data = json_decode(json_encode($info), true);
80
        $data['content'] = $value;
81
82
        $url = $this->getURL($this->URL) . '/' . $id;
83
        try {
84
            $this->makeRequest($url, 'PUT', $data);
85
        } catch(\Exception $e) {
86
            //var_dump($e);
87
        }
88
        $response = $this->getResponse();
89
90
        if($response->success != true) {
91
            return false;
92
        }
93
94
        return $response->result;
95
    }
96
97
    public function delete($id)
98
    {
99
        $url = $this->getURL($this->URL) . '/' . $id;
100
        try {
101
            $this->makeRequest($url, 'DELETE');
102
        } catch(\Exception $e) {
103
            //var_dump($e);
104
        }
105
106
        $response = $this->getResponse();
107
108
        if($response->success === true) {
109
            return true;
110
        }
111
112
        return false;
113
    }
114
115
    private function getURL($url)
116
    {
117
        return sprintf($url, $this->zone);
118
    }
119
}
120