Client::registerDomain()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 21
nc 4
nop 8
dl 0
loc 39
rs 9.584
c 1
b 0
f 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Subreg - Client Class
4
 *
5
 * @author     Vítězslav Dvořák <[email protected]>
6
 * @copyright  (C) 2018-2019 Spoje.Net
7
 */
8
9
namespace Subreg;
10
11
/**
12
 * Basic Soap Client class
13
 *
14
 * @author Vítězslav Dvořák <[email protected]>
15
 */
16
class Client extends \Ease\Molecule
17
{
18
    /**
19
     * ClientLibrary version
20
     * @var string 
21
     */
22
    static public $libVersion = '0.4';
23
24
    /**
25
     * Object Configuration
26
     * @var array
27
     */
28
    public $config = [];
29
30
    /**
31
     * Soap Helper
32
     * @var \SoapClient
33
     */
34
    public $soaper = null;
35
36
    /**
37
     * Authentification
38
     * @var string 
39
     */
40
    public $token = null;
41
42
    /**
43
     * Last call status code
44
     * @var string ok|error
45
     */
46
    public $lastStatus = null;
47
48
    /**
49
     * Last call error Data
50
     * @var array 
51
     */
52
    public $lastError = [];
53
54
    /**
55
     * Last call obtained Data
56
     * @var array 
57
     */
58
    public $lastResult = [];
59
60
    /**
61
     * 
62
     * @param type $config
0 ignored issues
show
Bug introduced by
The type Subreg\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
     */
64
    public function __construct($config)
65
    {
66
        parent::__construct();
67
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type Subreg\type is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
        $this->soaper = new \SoapClient(null,
69
            [
70
            "location" => $config['location'],
71
            "uri" => $config['uri']
72
            ]
73
        );
74
        $this->login();
75
    }
76
77
    /**
78
     * Add Info about used user, server and libraries
79
     *
80
     * @param string $additions Additional note text
81
     * 
82
     * @return boolean was logged ?
83
     */
84
    public function logBanner($additions = null)
85
    {
86
        return $this->addStatusMessage('API '.str_replace('://',
87
                    '://'.$this->config['login'].'@', $this->config['uri']).' php-subreg v'.self::$libVersion.' EasePHP Framework v'.\Ease\Atom::$frameworkVersion.' '.$additions,
88
                'debug');
89
    }
90
91
    /**
92
     * API Call
93
     * 
94
     * @param string $command command to execute
95
     * @param array  $params  command parameters
96
     * 
97
     * @return array
98
     */
99
    public function call(string $command, array $params = [])
100
    {
101
        $this->lastError  = null;
102
        $this->lastStatus = null;
103
        $this->lastResult = null;
104
        if ($this->token && !array_key_exists('ssid', $params)) {
105
            $params['ssid'] = $this->token;
106
        }
107
        $responseRaw = $this->soaper->__call($command, ['data' => $params]);
108
109
110
        if (isset($responseRaw['status'])) {
111
            $this->lastStatus = $responseRaw['status'];
112
            switch ($responseRaw['status']) {
113
                case 'ok':
114
                    if (array_key_exists('data', $responseRaw)) {
115
                        $this->lastResult = $responseRaw['data'];
116
                    } else {
117
                        $this->lastResult = $this->lastStatus;
118
                    }
119
                    break;
120
                case 'error':
121
                    $this->logError($responseRaw['error']);
122
                    $this->lastResult = ['error' => $responseRaw['error']];
123
                    break;
124
            }
125
        }
126
127
        return $this->lastResult;
128
    }
129
130
    /**
131
     * log Error
132
     * 
133
     * @param array $errorData
134
     */
135
    public function logError($errorData)
136
    {
137
        $this->lastError = $errorData;
138
        $this->addStatusMessage($errorData['errorcode']['major'].' '.$errorData['errorcode']['minor'].': '.$errorData['errormsg'],
139
            'error');
140
    }
141
142
    /**
143
     * Perform Login to Server
144
     * 
145
     * @return boolean success
146
     */
147
    public function login()
148
    {
149
        $result        = false;
150
        $params        = [
151
            "login" => $this->config['login'],
152
            "password" => $this->config['password']
153
        ];
154
        $loginResponse = $this->call("Login", $params);
155
        if (array_key_exists('ssid', $loginResponse)) {
156
            $this->token = $loginResponse['ssid'];
157
            $result      = true;
158
        }
159
        return $result;
160
    }
161
162
    /**
163
     *  Check if domain is available or not
164
     * 
165
     * @link https://subreg.cz/manual/?cmd=Check_Domain Command: Check_Domain
166
     * 
167
     * @param string $domain
168
     * 
169
     * @return array
170
     */
171
    public function checkDomain($domain)
172
    {
173
        return $this->call('Check_Domain', ['domain' => $domain]);
174
    }
175
176
    /**
177
     * Create a new domain
178
     * 
179
     * @link https://subreg.cz/manual/?cmd=Create_Domain Order: Create_Domain
180
     * 
181
     * @param string $domain
182
     * @param string $registrantID
183
     * @param string $contactsAdminID
184
     * @param string $contactsTechID
185
     * @param string $authID
186
     * @param array  $nsHosts          Hostnames of nameservers: ['ns.domain.cz','ns2.domain.cz']
187
     * @param string $nsset            Nameserver Set (only for FRED registries (.CZ,.EE,...))
188
     * @param int    $period            
189
     * 
190
     * @return array
191
     */
192
    public function registerDomain($domain, $registrantID, $contactsAdminID,
193
                                   $contactsTechID, $authID, $nsHosts = [],
194
                                   $nsset = null, $period = 1)
195
    {
196
197
        foreach ($nsHosts as $host) {
198
            $ns[]["hostname"] = $host;
199
        }
200
201
        $order = array(
202
            "domain" => $domain,
203
            "type" => "Create_Domain",
204
            "params" => array(
205
                "registrant" => array(
206
                    "id" => $registrantID,
207
                ),
208
                "contacts" => array(
209
                    "admin" => array(
210
                        "id" => $contactsAdminID,
211
                    ),
212
                    "tech" => array(
213
                        "id" => $contactsTechID,
214
                    ),
215
                ),
216
                "ns" => array(
217
                    "hosts" => $ns,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ns seems to be defined by a foreach iteration on line 197. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
218
                ),
219
                "params" => array(
220
                    "authid" => $authID,
221
                ),
222
                "period" => $period
223
            )
224
        );
225
226
        if (!empty($nsset)) {
227
            $order['params']['ns']['nsset'] = $nsset;
228
        }
229
230
        return $this->call('Make_Order', ['order' => $order]);
231
    }
232
233
    /**
234
     *  Get all domains from your account
235
     * 
236
     * @link https://subreg.cz/manual/?cmd=Domains_List Command: Domains_List
237
     * 
238
     * @return array
239
     */
240
    public function domainsList()
241
    {
242
        return $this->call('Domains_List');
243
    }
244
245
    /**
246
     * Get pricelist from your account
247
     * 
248
     * @link https://subreg.cz/manual/?cmd=Pricelist Command: Pricelist
249
     * 
250
     * @return array pricelist details
251
     */
252
    public function pricelist()
253
    {
254
        return $this->call('Pricelist');
255
    }
256
257
    /**
258
     * Renew a existing domain from your account
259
     * 
260
     * @link https://subreg.cz/manual/?cmd=Renew_Domain Command: Renew_Domain
261
     * 
262
     * @param string $domain name
263
     * @param int $years
264
     * 
265
     * @return string|array OK or Result array
266
     */
267
    public function renewDomain(string $domain, int $years = 1)
268
    {
269
        return $this->call('Make_Order',
270
                ['order' => ['domain' => $domain, 'params' => ['period' => $years],
271
                    'type' => 'Renew_Domain']]);
272
    }
273
274
    /**
275
     * Set autorenew policy for your domain. 
276
     * 
277
     * @link https://subreg.cz/manual/?cmd=Set_Autorenew Command: Set_Autorenew
278
     * 
279
     * @param string $domain
280
     * @param string $renew  only EXPIRE, AUTORENEW or RENEWONCE
281
     * 
282
     * @return string|array OK or Result array
283
     */
284
    public function setAutorenew(string $domain, string $renew)
285
    {
286
        return $this->call('Set_Autorenew',
287
                ['domain' => $domain, 'autorenew' => $renew]);
288
    }
289
}
290