CloudflareConnection   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 51
c 4
b 1
f 0
dl 0
loc 184
rs 10
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getZones() 0 3 1
A current() 0 3 1
A createZone() 0 10 1
A deleteDnsRecord() 0 3 1
A deleteZone() 0 3 1
A getDnsRecordsForZone() 0 6 1
A createDnsRecord() 0 3 1
A validateLogin() 0 5 2
A updateDnsRecord() 0 3 1
A currentUser() 0 3 1
B apiCall() 0 43 6
1
<?php
2
3
class CloudflareConnection
4
{
5
    private $username;
6
    private $api_key;
7
    private $host;
8
    private $debug;
9
10
    /**
11
     * CloudflareConnection constructor.
12
     *
13
     * @param string $username
14
     * @param string $apiKey
15
     */
16
    public function __construct(string $username, string $apiKey)
17
    {
18
        $this->username = $username;
19
        $this->api_key = $apiKey;
20
        $this->host = 'https://api.cloudflare.com/client/v4/';
21
22
        $this->debug = false;
23
    }
24
25
    /**
26
     * @return bool
27
     */
28
    public function validateLogin(): bool
29
    {
30
        $user = $this->currentUser();
31
32
        return $user && $user['success'] === true;
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    public function currentUser(): ?array
39
    {
40
        return $this->apiCall('user');
41
    }
42
43
    /**
44
     * @return array|null
45
     */
46
    public function current(): ?array
47
    {
48
        return $this->apiCall('user');
49
    }
50
51
    /**
52
     * @param string $id
53
     * @param array  $parameters
54
     *
55
     * @return array|null
56
     */
57
    public function getDnsRecordsForZone(string $id, array $parameters): ?array
58
    {
59
        $parameters['per_page'] = 100;
60
        $query_string = http_build_query($parameters);
61
62
        return $this->apiCall('zones/'.$id.'/dns_records?'.$query_string);
63
    }
64
65
    /**
66
     * @param array $parameters
67
     *
68
     * @return array|null
69
     */
70
    public function getZones(array $parameters): ?array
71
    {
72
        return $this->apiCall('zones?'.http_build_query($parameters));
73
    }
74
75
    /**
76
     * @param string $domain
77
     *
78
     * @return array|null
79
     */
80
    public function createZone(string $domain, string $accountId): ?array
81
    {
82
        $data = [
83
            'name'       => $domain,
84
            'account'    => ['id' => $accountId],
85
            'jump_start' => true,
86
            'type'       => 'full',
87
        ];
88
89
        return $this->apiCall('zones', 'POST', $data);
90
    }
91
92
    /**
93
     * @param string $zoneId
94
     *
95
     * @return array|null
96
     */
97
    public function deleteZone(string $zoneId): ?array
98
    {
99
        return $this->apiCall('zones/'.$zoneId, 'DELETE');
100
    }
101
102
    /**
103
     * @param string $zoneId
104
     * @param string $id
105
     * @param array  $data
106
     *
107
     * @return array|null
108
     */
109
    public function createDnsRecord(string $zoneId, array $data): ?array
110
    {
111
        return $this->apiCall('zones/'.$zoneId.'/dns_records', 'POST', $data);
112
    }
113
114
    /**
115
     * @param string $zoneId
116
     * @param string $id
117
     * @param array  $data
118
     *
119
     * @return array|null
120
     */
121
    public function updateDnsRecord(string $zoneId, string $id, array $data): ?array
122
    {
123
        return $this->apiCall('zones/'.$zoneId.'/dns_records/'.$id, 'PUT', $data);
124
    }
125
126
    /**
127
     * @param string $zoneId
128
     * @param string $id
129
     *
130
     * @return array|null
131
     */
132
    public function deleteDnsRecord(string $zoneId, string $id): ?array
133
    {
134
        return $this->apiCall('zones/'.$zoneId.'/dns_records/'.$id, 'DELETE');
135
    }
136
137
    /**
138
     * @param string $path
139
     * @param string $method
140
     * @param array  $data
141
     *
142
     * @return array|null
143
     */
144
    private function apiCall(string $path, string $method = 'GET', array $data = []): ?array
145
    {
146
        $ch = curl_init();
147
148
        if (false === $ch) {
149
            return null;
150
        }
151
152
        $request_headers = [
153
            'X-Auth-Email: '.$this->username,
154
            'X-Auth-Key: '.$this->api_key,
155
            'Content-Type: application/json',
156
        ];
157
158
        if (in_array($method, ['PUT', 'POST'])) {
159
            $json_data = json_encode($data);
160
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
161
162
            $request_headers[] = 'Content-Length: '.strlen($json_data);
163
        }
164
165
        if ($method !== 'GET') {
166
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
167
        }
168
169
        curl_setopt($ch, CURLOPT_URL, $this->host.$path);
170
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
171
        curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
172
173
        if (curl_errno($ch)) {
174
            return null;
175
        }
176
177
        $response = curl_exec($ch);
178
179
        if ($this->debug) {
180
            $info = curl_getinfo($ch);
181
            echo $info['url'];
182
        }
183
184
        curl_close($ch);
185
186
        return json_decode($response, true);
187
    }
188
}
189