Passed
Push — dev ( 3796bc...7e9308 )
by Greg
03:17
created

GoDaddy::getFullInfo()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 6.9743
c 0
b 0
f 0
cc 7
eloc 25
nc 3
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * /classes/DomainMOD/GoDaddy.php
4
 *
5
 * This file is part of DomainMOD, an open source domain and internet asset manager.
6
 * Copyright (c) 2010-2018 Greg Chetcuti <[email protected]>
7
 *
8
 * Project: http://domainmod.org   Author: http://chetcuti.com
9
 *
10
 * DomainMOD is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
11
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * DomainMOD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along with DomainMOD. If not, see
18
 * http://www.gnu.org/licenses/.
19
 *
20
 */
21
//@formatter:off
22
namespace DomainMOD;
23
24
class GoDaddy
25
{
26
    public $format;
27
    public $log;
28
29
    public function __construct()
30
    {
31
        $this->format = new Format();
32
        $this->log = new Log('class.godaddy');
33
    }
34
35
    public function getApiUrl($domain, $command)
36
    {
37
        $base_url = 'https://api.godaddy.com/v1/';
38
        if ($command == 'domainlist') {
39
            return $base_url . 'domains?statusGroups=VISIBLE&limit=1000';
40
        } elseif ($command == 'info') {
41
            return $base_url . 'domains/' . $domain;
42
        } else {
43
            return 'Unable to build API URL';
44
        }
45
    }
46
47
    public function apiCall($api_key, $api_secret, $full_url)
48
    {
49
        $handle = curl_init($full_url);
50
        curl_setopt($handle, CURLOPT_HTTPHEADER, array(
51
            'Authorization: sso-key ' . $api_key . ':' . $api_secret,
52
            'Accept: application/json'));
53
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
54
        curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
55
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
56
        $result = curl_exec($handle);
57
        curl_close($handle);
58
        return $result;
59
    }
60
61
    public function getDomainList($api_key, $api_secret)
62
    {
63
        $domain_list = array();
64
        $domain_count = 0;
65
66
        $api_url = $this->getApiUrl('', 'domainlist');
67
        $api_results = $this->apiCall($api_key, $api_secret, $api_url);
68
        $array_results = $this->convertToArray($api_results);
69
70
        // confirm that the api call was successful
71
        if (isset($array_results[0]['domain'])) {
72
73
            foreach ($array_results as $domain) {
74
75
                $domain_list[] = $domain['domain'];
76
                $domain_count++;
77
78
            }
79
80
        } else {
81
82
            $log_message = 'Unable to get domain list';
83
            $log_extra = array('API Key' => $this->format->obfusc($api_key), 'API Secret' => $this->format->obfusc($api_secret));
84
            $this->log->error($log_message, $log_extra);
85
86
        }
87
88
        return array($domain_count, $domain_list);
89
    }
90
91
    public function getFullInfo($api_key, $api_secret, $domain)
92
    {
93
        $expiration_date = '';
94
        $dns_servers = array();
95
        $privacy_status = '';
96
        $autorenewal_status = '';
97
98
        $api_url = $this->getApiUrl($domain, 'info');
99
        $api_results = $this->apiCall($api_key, $api_secret, $api_url);
100
        $array_results = $this->convertToArray($api_results);
101
102
        // confirm that the api call was successful
103
        /*
104
         * The $array_results['expires'] and $array_results['contactRegistrant']['nameFirst'] checks were put in place
105
         * because GoDaddy's domain list API command returns domains that aren't necessarily registered with them (such
106
         * as domains that are registered elsewhere but the DNS is hosted at GoDaddy). These additional checks ensure
107
         * that the domain details will only be processed for domains that are actually registered at GoDaddy.
108
         */
109
        if (isset($array_results['domain']) && isset($array_results['expires']) && isset($array_results['contactRegistrant']['nameFirst'])) {
110
111
            // get expiration date
112
            $expiration_date = substr($array_results['expires'], 0, 10);
113
114
            // get dns servers
115
            $dns_result = $array_results['nameServers'];
116
            $dns_servers = $this->processDns($dns_result);
117
118
            // get privacy status
119
            $privacy_result = (string) $array_results['privacy'];
120
            $privacy_status = $this->processPrivacy($privacy_result);
121
122
            // get auto renewal status
123
            $autorenewal_result = (string) $array_results['renewAuto'];
124
            $autorenewal_status = $this->processAutorenew($autorenewal_result);
125
126
        } elseif (isset($array_results['domain']) && !isset($array_results['expires']) && !isset($array_results['contactRegistrant']['nameFirst'])) {
127
128
            $domain_status = 'invalid';
129
            $log_message = 'Invalid domain (not registered at GoDaddy)';
130
            $log_extra = array('Domain' => $domain, 'API Key' => $this->format->obfusc($api_key), 'API Secret' => $this->format->obfusc($api_secret));
131
            $this->log->warning($log_message, $log_extra);
132
133
        } else {
134
135
            $log_message = 'Unable to get domain details';
136
            $log_extra = array('Domain' => $domain, 'API Key' => $this->format->obfusc($api_key), 'API Secret' => $this->format->obfusc($api_secret));
137
            $this->log->error($log_message, $log_extra);
138
139
        }
140
141
        return array($domain_status, $expiration_date, $dns_servers, $privacy_status, $autorenewal_status);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $domain_status does not seem to be defined for all execution paths leading up to this point.
Loading history...
142
    }
143
144
    public function convertToArray($api_result)
145
    {
146
        return json_decode($api_result, true);
147
    }
148
149
    public function processDns($dns_result)
150
    {
151
        $dns_servers = array();
152
        if (!empty($dns_result)) {
153
            $dns_servers = array_filter($dns_result);
154
        } else {
155
            $dns_servers[0] = 'no.dns-servers.1';
156
            $dns_servers[1] = 'no.dns-servers.2';
157
        }
158
        return $dns_servers;
159
    }
160
161
    public function processPrivacy($privacy_result)
162
    {
163
        if ($privacy_result == '') {
164
            $privacy_status = '0';
165
        } else {
166
            $privacy_status = '1';
167
        }
168
        return $privacy_status;
169
    }
170
171
    public function processAutorenew($autorenewal_result)
172
    {
173
        if ($autorenewal_result == '') {
174
            $autorenewal_status = '0';
175
        } else {
176
            $autorenewal_status = '1';
177
        }
178
        return $autorenewal_status;
179
    }
180
181
} //@formatter:on
182