Completed
Push — master ( ee71a2...2d5b89 )
by Greg
05:23
created

AboveCom::getFullInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 52
Code Lines 31

Duplication

Lines 52
Ratio 100 %

Importance

Changes 0
Metric Value
dl 52
loc 52
rs 9.4929
c 0
b 0
f 0
cc 2
eloc 31
nc 2
nop 2

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/AboveCom.php
4
 *
5
 * This file is part of DomainMOD, an open source domain and internet asset manager.
6
 * Copyright (c) 2010-2017 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 AboveCom
25
{
26
    public $format;
27
    public $log;
28
    public $system;
29
30
    public function __construct()
31
    {
32
        $this->format = new Format();
33
        $this->log = new Log('abovecom.class');
34
        $this->system = new System();
35
    }
36
37 View Code Duplication
    public function getApiUrl($api_key, $command)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $base_url = 'https://www.above.com/registrar/api/query.html?key=' . $api_key;
40
        if ($command == 'domainlist') {
41
            return $base_url . '&query=my_domains&limit=1000';
42
        } else {
43
            return 'Unable to build API URL';
44
        }
45
    }
46
47 View Code Duplication
    public function apiCall($full_url)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        $handle = curl_init($full_url);
50
        curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
51
        $result = curl_exec($handle);
52
        curl_close($handle);
53
        return $result;
54
    }
55
56
    public function getDomainList($api_key, $account_id)
57
    {
58
        $domain_list = array();
59
        $domain_count = 0;
60
61
        $api_url = $this->getApiUrl($api_key, 'domainlist');
62
        $api_results = $this->apiCall($api_url);
63
        $array_results = $this->convertToArray($api_results);
64
65
        // confirm that the api call was successful
66
        if ($array_results[0]['@attributes']['code'] == '100') {
67
68
            $tmpq = $this->system->db()->prepare("
69
                INSERT INTO domain_queue_temp
70
                (account_id, domain, expiry_date, ns1, ns2, ns3, ns4, ns5, ns6, ns7, ns8, ns9, ns10, autorenew, privacy)
71
                VALUES
72
                (:account_id, :domain, :expiry_date, :ns1, :ns2, :ns3, :ns4, :ns5, :ns6, :ns7, :ns8, :ns9, :ns10,
73
                 :autorenew, :privacy)");
74
75
            foreach ($array_results[0]['domains']['r'] as $value) {
76
77
                $domain_list[] = $value['@attributes']['domain'];
78
                $expiry_date = $this->processExpiry($value['expiry']);
79
                if ($value['dns1'] != '') $ns1 = $value['dns1'];
80
                if ($value['dns2'] != '') $ns2 = $value['dns2'];
81
                if ($value['dns3'] != '') $ns3 = $value['dns3'];
82
                if ($value['dns4'] != '') $ns4 = $value['dns4'];
83
                if ($value['dns5'] != '') $ns5 = $value['dns5'];
84
                if ($value['dns6'] != '') $ns6 = $value['dns6'];
85
                if ($value['dns7'] != '') $ns7 = $value['dns7'];
86
                if ($value['dns8'] != '') $ns8 = $value['dns8'];
87
                if ($value['dns9'] != '') $ns9 = $value['dns9'];
88
                if ($value['dns10'] != '') $ns10 = $value['dns10'];
89
                $autorenew = $this->processAutorenew($value['autorenew']);
90
                $privacy = $this->processPrivacy($value['privacy']);
91
92
                $tmpq->execute(['account_id' => $account_id,
93
                                'domain' => $domain_list[$domain_count],
94
                                'expiry_date' => $expiry_date,
95
                                'ns1' => $ns1,
96
                                'ns2' => $ns2,
97
                                'ns3' => $ns3,
98
                                'ns4' => $ns4,
99
                                'ns5' => $ns5,
100
                                'ns6' => $ns6,
101
                                'ns7' => $ns7,
102
                                'ns8' => $ns8,
103
                                'ns9' => $ns9,
104
                                'ns10' => $ns10,
105
                                'autorenew' => $autorenew,
106
                                'privacy' => $privacy]);
107
108
                $domain_count++;
109
110
            }
111
112
        } else {
113
114
            $log_message = 'Unable to get domain list';
115
            $log_extra = array('API Key' => $this->format->obfusc($api_key), 'Account ID' => $account_id);
116
            $this->log->error($log_message, $log_extra);
0 ignored issues
show
Documentation introduced by
$log_extra is of type array<string,?,{"API Key...ing","Account ID":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
118
        }
119
120
        return array($domain_count, $domain_list);
121
    }
122
123 View Code Duplication
    public function getFullInfo($account_id, $domain)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $expiration_date = '';
126
        $dns_servers = array();
127
        $privacy_status = '';
128
        $autorenewal_status = '';
129
130
        $tmpq = $this->system->db()->prepare("
131
            SELECT id, expiry_date, ns1, ns2, ns3, ns4, ns5, ns6, ns7, ns8, ns9, ns10, autorenew, privacy
132
            FROM domain_queue_temp
133
            WHERE account_id = :account_id
134
              AND domain = :domain
135
            ORDER BY id ASC");
136
        $tmpq->execute(['account_id' => $account_id,
137
                        'domain' => $domain]);
138
        $result = $tmpq->fetch();
139
140
        if (!$result) {
141
142
            $log_message = 'Unable to get domain details';
143
            $log_extra = array('Domain' => $domain, 'Account ID' => $account_id);
144
            $this->log->error($log_message, $log_extra);
0 ignored issues
show
Documentation introduced by
$log_extra is of type array<string,?,{"Domain":"?","Account ID":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
145
146
        } else {
147
148
            $expiration_date = $result->expiry_date;
149
150
            $dns_result[0] = $result->ns1;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dns_result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dns_result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
151
            $dns_result[1] = $result->ns2;
152
            $dns_result[2] = $result->ns3;
153
            $dns_result[3] = $result->ns4;
154
            $dns_result[4] = $result->ns5;
155
            $dns_result[5] = $result->ns6;
156
            $dns_result[6] = $result->ns7;
157
            $dns_result[7] = $result->ns8;
158
            $dns_result[8] = $result->ns9;
159
            $dns_result[9] = $result->ns10;
160
            $dns_servers = $this->processDns($dns_result);
161
162
            $privacy_status = $result->privacy;
163
164
            $autorenewal_status = $result->autorenew;
165
166
            $tmpq = $this->system->db()->prepare("
167
                DELETE FROM domain_queue_temp
168
                WHERE id = :id");
169
            $tmpq->execute(['id' => $result->id]);
170
171
        }
172
173
        return array($expiration_date, $dns_servers, $privacy_status, $autorenewal_status);
174
    }
175
176
    public function convertToArray($api_result)
177
    {
178
        $xml = simplexml_load_string($api_result);
179
        $json = json_encode((array($xml)), true);
180
        return json_decode($json, true);
181
    }
182
183
    public function processExpiry($expiry_result)
184
    {
185
        $unix_expiration_date = strtotime($expiry_result);
186
        return gmdate("Y-m-d", $unix_expiration_date);
187
    }
188
189 View Code Duplication
    public function processDns($dns_result)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
    {
191
        $dns_servers = array();
192
        if (!empty($dns_result)) {
193
            $dns_servers = array_filter($dns_result);
194
        } else {
195
            $dns_servers[0] = 'no.dns-servers.1';
196
            $dns_servers[1] = 'no.dns-servers.2';
197
        }
198
        return $dns_servers;
199
    }
200
201
    public function processPrivacy($privacy_result)
202
    {
203
        if ($privacy_result == 'on') {
204
            $privacy_status = '1';
205
        } else {
206
            $privacy_status = '0';
207
        }
208
        return $privacy_status;
209
    }
210
211
    public function processAutorenew($autorenewal_result)
212
    {
213
        if ($autorenewal_result == 'on') {
214
            $autorenewal_status = '1';
215
        } else { // result messages - 'do not renew', 'no renew option'
216
            $autorenewal_status = '0';
217
        }
218
        return $autorenewal_status;
219
    }
220
221
} //@formatter:on
222