Total Complexity | 104 |
Total Lines | 503 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Accounts often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Accounts, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Accounts |
||
18 | { |
||
19 | /** |
||
20 | * |
||
21 | * @var WHMClient |
||
22 | */ |
||
23 | protected $client; |
||
24 | |||
25 | /** |
||
26 | * Accounts constructor. |
||
27 | * |
||
28 | * @param WHMClient $client |
||
29 | */ |
||
30 | public function __construct(WHMClient $client) |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Search accounts from your WHM server. |
||
37 | * |
||
38 | * WHM API function: Accounts -> listaccts |
||
39 | * |
||
40 | * $accounts = new Accounts($c); |
||
41 | * $keyword = "search_keyword"; |
||
42 | * $searchType = "username"; //valid search types are "domain", "owner", "user", "ip", "package" |
||
43 | * $options = [ |
||
44 | * 'searchmethod' => "exact", //"exact" or "regex", |
||
45 | * "page" => 1, |
||
46 | * "limit" => 10, //per page, |
||
47 | * "want" => "username" //A comma-separated list of fields you want to fetch |
||
48 | * ]; |
||
49 | * |
||
50 | * try { |
||
51 | * $accounts->searchAccounts($keyword, $searchType, $options); |
||
52 | * } catch (\Http\Client\Exception $e) { |
||
53 | * echo $e->getMessage(); |
||
54 | * } catch (\PreviewTechs\cPanelWHM\Exceptions\ClientExceptions $e) { |
||
55 | * echo $e->getMessage(); |
||
56 | * } |
||
57 | * |
||
58 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+listaccts |
||
59 | * |
||
60 | * @param null $keyword |
||
|
|||
61 | * @param null $searchType |
||
62 | * @param array $options |
||
63 | * |
||
64 | * @return array |
||
65 | * @throws ClientExceptions |
||
66 | * @throws Exception |
||
67 | */ |
||
68 | public function searchAccounts($keyword = null, $searchType = null, array $options = []) |
||
69 | { |
||
70 | $limit = 10; |
||
71 | $page = 1; |
||
72 | |||
73 | $params = [ |
||
74 | 'api.version' => 1, |
||
75 | 'api.chunk.enable' => 1, |
||
76 | 'api.chunk.size' => $limit, |
||
77 | 'api.chunk.start' => $page * $limit |
||
78 | ]; |
||
79 | |||
80 | if (!empty($options['limit'])) { |
||
81 | $params['api.chunk.size'] = intval($options['limit']); |
||
82 | } |
||
83 | |||
84 | if (!empty($options['page'])) { |
||
85 | $params['api.chunk.start'] = intval($options['page']) * $params['api.chunk.size']; |
||
86 | } |
||
87 | |||
88 | if (!empty($searchType) && !in_array($searchType, ["domain", "owner", "user", "ip", "package"])) { |
||
89 | throw new \InvalidArgumentException("`searchType` must be one of these - domain, owner, user, ip, package"); |
||
90 | } |
||
91 | |||
92 | if (!empty($options['searchmethod']) && !in_array($options['searchmethod'], ["exact", "regex"])) { |
||
93 | throw new \InvalidArgumentException("options[searchmethod] must be either `regex` or `exact`"); |
||
94 | } |
||
95 | |||
96 | if (!empty($options['want'])) { |
||
97 | $params['want'] = $options['want']; |
||
98 | } |
||
99 | |||
100 | if (!empty($searchType)) { |
||
101 | $params['searchtype'] = $searchType; |
||
102 | } |
||
103 | |||
104 | if (!empty($keyword)) { |
||
105 | $params['search'] = $keyword; |
||
106 | empty($searchType) ? $params['searchtype'] = "user" : null; |
||
107 | } |
||
108 | |||
109 | $results = $this->client->sendRequest("/json-api/listaccts", "GET", $params); |
||
110 | if (empty($results['data']['acct'])) { |
||
111 | return []; |
||
112 | } |
||
113 | |||
114 | $accounts = []; |
||
115 | foreach ($results['data']['acct'] as $account) { |
||
116 | $accounts[] = Account::buildFromArray($account); |
||
117 | } |
||
118 | |||
119 | return [ |
||
120 | 'accounts' => $accounts, |
||
121 | 'count' => $params['api.chunk.size'], |
||
122 | 'page' => $page |
||
123 | ]; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Get an account details |
||
128 | * |
||
129 | * WHM API function: Accounts -> accountsummary |
||
130 | * |
||
131 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+accountsummary |
||
132 | * |
||
133 | * @param null $user |
||
134 | * @param null $domain |
||
135 | * |
||
136 | * @return null|Account |
||
137 | * @throws ClientExceptions |
||
138 | * @throws Exception |
||
139 | */ |
||
140 | public function getDetails($user = null, $domain = null) |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * This function lists available WHM API 1 functions. |
||
182 | * |
||
183 | * This function only lists the functions that are available to the current user. |
||
184 | * For example, if the authenticated user is a reseller without root -level privileges, |
||
185 | * the function will not list WHM API 1 functions that require root privileges. |
||
186 | * |
||
187 | * WHM API function: Accounts -> applist |
||
188 | * |
||
189 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+applist |
||
190 | * |
||
191 | * @return array |
||
192 | * @throws ClientExceptions |
||
193 | * @throws Exception |
||
194 | */ |
||
195 | public function availableFunctions() |
||
196 | { |
||
197 | $result = $this->client->sendRequest("/json-api/applist", 'GET', []); |
||
198 | |||
199 | if (!empty($result['app']) && sizeof($result['app']) > 0) { |
||
200 | return $result['app']; |
||
201 | } |
||
202 | |||
203 | return []; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Create a new account. This function creates a cPanel account. |
||
208 | * The function also sets up the new account's domain information. |
||
209 | * |
||
210 | * WHM API function: Accounts -> createacct |
||
211 | * |
||
212 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+createacct |
||
213 | * |
||
214 | * @param Account $account |
||
215 | * @param array $options |
||
216 | * @return array |
||
217 | * @throws ClientExceptions |
||
218 | * @throws Exception |
||
219 | */ |
||
220 | public function create(Account $account, array $options = []) |
||
221 | { |
||
222 | if (empty($account->getUser())) { |
||
223 | throw ClientExceptions::invalidArgument("You must provide an username to create new account"); |
||
224 | } |
||
225 | |||
226 | if (empty($account->getDomain())) { |
||
227 | throw ClientExceptions::invalidArgument("You must provide a domain to create new account"); |
||
228 | } |
||
229 | |||
230 | $params = []; |
||
231 | $params['username'] = $account->getUser(); |
||
232 | $params['domain'] = $account->getDomain(); |
||
233 | !empty($account->getPlanName()) ? $params['plan'] = $account->getPlanName() : null; |
||
234 | !empty($options['pkgname']) ? $params['pkgname'] = $options['pkgname'] : null; |
||
235 | if (!empty($options['savepkg'])) { |
||
236 | if (!in_array(intval($options['savepkg']), [0, 1])) { |
||
237 | throw new ClientExceptions("`savepkg` must be either 0 or 1"); |
||
238 | } |
||
239 | |||
240 | $params['savepkg'] = $options['savepkg']; |
||
241 | } |
||
242 | |||
243 | !empty($options['featurelist']) ? $params['featurelist'] = $options['featurelist'] : null; |
||
244 | if (!empty($account->getDiskLimit())) { |
||
245 | if ($account->getDiskLimit() === -1) { |
||
246 | $params['quota'] = 0; |
||
247 | } else { |
||
248 | $params['quota'] = intval($account->getDiskLimit()); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | !empty($account->getPassword()) ? $params['password'] = $account->getPassword() : null; |
||
253 | !empty($account->getIpAddress()) ? $params['ip'] = $account->getIpAddress() : null; |
||
254 | !empty($account->isCgiEnable()) ? $params['cgi'] = (int)$account->isCgiEnable() : null; |
||
255 | !empty($account->isSpamAssassinEnable()) ? |
||
256 | $params['spamassassin'] = (int)$account->isSpamAssassinEnable() |
||
257 | : null; |
||
258 | !empty($account->isFrontPageEnable()) ? $params['frontpage'] = (int)$account->isFrontPageEnable() : null; |
||
259 | !empty($account->getShell()) ? $params['hasshell'] = 1 : null; |
||
260 | !empty($account->getEmail()) ? $params['contactemail'] = 1 : null; |
||
261 | !empty($account->getEmail()) ? $params['contactemail'] = 1 : null; |
||
262 | !empty($account->getTheme()) ? $params['cpmod'] = 1 : null; |
||
263 | |||
264 | if ($account->getMaxFTP() === -1) { |
||
265 | $params['maxftp'] = "unlimited"; |
||
266 | } elseif ($account->getMaxFTP() > 0) { |
||
267 | $params['maxftp'] = intval($account->getMaxFTP()); |
||
268 | } |
||
269 | |||
270 | if ($account->getMaxSQL() === -1) { |
||
271 | $params['maxsql'] = "unlimited"; |
||
272 | } elseif ($account->getMaxSQL() > 0) { |
||
273 | $params['maxsql'] = intval($account->getMaxSQL()); |
||
274 | } |
||
275 | |||
276 | if ($account->getMaxPOP() === -1) { |
||
277 | $params['maxpop'] = "unlimited"; |
||
278 | } elseif ($account->getMaxPOP() > 0) { |
||
279 | $params['maxpop'] = intval($account->getMaxPOP()); |
||
280 | } |
||
281 | |||
282 | if ($account->getMaxMailingList() === -1) { |
||
283 | $params['maxlst'] = "unlimited"; |
||
284 | } elseif ($account->getMaxMailingList() > 0) { |
||
285 | $params['maxlst'] = intval($account->getMaxMailingList()); |
||
286 | } |
||
287 | |||
288 | if ($account->getMaxSubDomain() === -1) { |
||
289 | $params['maxsub'] = "unlimited"; |
||
290 | } elseif ($account->getMaxSubDomain() > 0) { |
||
291 | $params['maxsub'] = intval($account->getMaxSubDomain()); |
||
292 | } |
||
293 | |||
294 | if ($account->getMaxParkedDomains() === -1) { |
||
295 | $params['maxpark'] = "unlimited"; |
||
296 | } elseif ($account->getMaxParkedDomains() > 0) { |
||
297 | $params['maxpark'] = intval($account->getMaxParkedDomains()); |
||
298 | } |
||
299 | |||
300 | if ($account->getMaxAddonDomains() === -1) { |
||
301 | $params['maxaddon'] = "unlimited"; |
||
302 | } elseif ($account->getMaxAddonDomains() > 0) { |
||
303 | $params['maxaddon'] = intval($account->getMaxAddonDomains()); |
||
304 | } |
||
305 | |||
306 | if ($account->getBandwidthLimit() === -1) { |
||
307 | $params['bwlimit'] = "unlimited"; |
||
308 | } elseif ($account->getBandwidthLimit() > 0) { |
||
309 | $params['bwlimit'] = intval($account->getBandwidthLimit()); |
||
310 | } |
||
311 | |||
312 | !empty($options['customip']) ? $params['customip'] = $options['customip'] : null; |
||
313 | |||
314 | !empty($account->getLanguagePreference()) ? $params['language'] = $account->getLanguagePreference() : null; |
||
315 | |||
316 | !empty($options['useregns']) ? $params['useregns'] = $options['useregns'] : null; |
||
317 | !empty($options['reseller']) ? $params['reseller'] = (int)$options['reseller'] : null; |
||
318 | !empty($options['forcedns']) ? $params['forcedns'] = (int)$options['forcedns'] : null; |
||
319 | |||
320 | !empty($account->getMailboxFormat()) ? $params['mailbox_format'] = $account->getMailboxFormat() : null; |
||
321 | |||
322 | if (!empty($options['mxcheck'])) { |
||
323 | if (!in_array($options['mxcheck'], ['local', 'secondary', 'remote', 'auto'])) { |
||
324 | throw new ClientExceptions("options[mxcheck] parameters must be one of local, secondary, remote, auto"); |
||
325 | } |
||
326 | |||
327 | $params['mxcheck'] = $options['mxcheck']; |
||
328 | } |
||
329 | |||
330 | if ($account->getMaxEmailPerHour() === -1) { |
||
331 | $params['max_email_per_hour'] = "unlimited"; |
||
332 | } elseif ($account->getMaxEmailPerHour() > 0) { |
||
333 | $params['max_email_per_hour'] = intval($account->getMaxEmailPerHour()); |
||
334 | } |
||
335 | |||
336 | if ($account->getMaxEmailAccountQuota() === -1) { |
||
337 | $params['max_emailacct_quota'] = "unlimited"; |
||
338 | } elseif ($account->getMaxEmailAccountQuota() > 0) { |
||
339 | $params['max_email_per_hour'] = intval($account->getMaxEmailAccountQuota()); |
||
340 | } |
||
341 | |||
342 | if ($account->getMaxDeferFailMailPercentage() === -1) { |
||
343 | $params['max_defer_fail_percentage'] = "unlimited"; |
||
344 | } elseif ($account->getMaxDeferFailMailPercentage() > 0) { |
||
345 | $params['max_defer_fail_percentage'] = intval($account->getMaxDeferFailMailPercentage()); |
||
346 | } |
||
347 | |||
348 | !empty($account->getUid()) ? $params['uid'] = $account->getUid() : null; |
||
349 | !empty($account->getPartition()) ? $params['homedir'] = $account->getPartition() : null; |
||
350 | !empty($options['dkim']) ? $params['dkim'] = intval($options['dkim']) : null; |
||
351 | !empty($options['spf']) ? $params['spf'] = intval($options['spf']) : null; |
||
352 | !empty($account->getOwner()) ? $params['owner'] = $account->getOwner() : null; |
||
353 | |||
354 | $result = $this->client->sendRequest("/json-api/createacct", "GET", $params); |
||
355 | |||
356 | if (!empty($result) && !empty($result['result'][0]) && $result['result'][0]['status'] === 0) { |
||
357 | throw new ClientExceptions($result['result'][0]['statusmsg']); |
||
358 | } |
||
359 | |||
360 | if (!empty($result) && !empty($result['result'][0]) && $result['result'][0]['status'] === 1) { |
||
361 | return ['result' => $result['result'][0]['options'], 'raw_output' => $result['result'][0]['rawout']]; |
||
362 | } |
||
363 | |||
364 | return []; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * This function retrieves domain data. |
||
369 | * |
||
370 | * WHM API function: Accounts -> domainuserdata |
||
371 | * |
||
372 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+domainuserdata |
||
373 | * @param $domain |
||
374 | * @return null|DomainUser |
||
375 | * @throws ClientExceptions |
||
376 | * @throws Exception |
||
377 | */ |
||
378 | public function domainDetails($domain) |
||
379 | { |
||
380 | $params = ['domain' => $domain]; |
||
381 | |||
382 | $result = $this->client->sendRequest("/json-api/domainuserdata", "GET", $params); |
||
383 | if(empty($result)){ |
||
384 | return null; |
||
385 | } |
||
386 | |||
387 | if($result['result'][0]['status'] === 0){ |
||
388 | throw new ClientExceptions($result['result'][0]['statusmsg']); |
||
389 | } |
||
390 | |||
391 | $userData = $result['userdata']; |
||
392 | $domainUser = new DomainUser(); |
||
393 | $domainUser->setHasCGI((bool) $userData['hascgi']); |
||
394 | $domainUser->setServerName($userData['servername']); |
||
395 | $domainUser->setOwner($userData['owner']); |
||
396 | $domainUser->setScriptAlias($userData['scriptalias']); |
||
397 | $domainUser->setHomeDirectory($userData['homedir']); |
||
398 | $domainUser->setCustomLog($userData['customlog']); |
||
399 | $domainUser->setUser($userData['user']); |
||
400 | $domainUser->setGroup($userData['group']); |
||
401 | $domainUser->setIpAddress($userData['ip']); |
||
402 | $domainUser->setPort($userData['port']); |
||
403 | $domainUser->setPhpOpenBaseDirectoryProtect((bool) $userData['phpopenbasedirprotect']); |
||
404 | |||
405 | if($userData['usecanonicalname'] === "Off"){ |
||
406 | $domainUser->setUseCanonicalName(false); |
||
407 | }elseif($userData['usecanonicalname'] === "On"){ |
||
408 | $domainUser->setUseCanonicalName(true); |
||
409 | } |
||
410 | |||
411 | $domainUser->setServerAdmin($userData['serveradmin']); |
||
412 | $domainUser->setServerAlias($userData['serveralias']); |
||
413 | $domainUser->setDocumentRoot($userData['documentroot']); |
||
414 | |||
415 | return $domainUser; |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * This function modifies a user's disk quota. |
||
420 | * WHM API function: Accounts -> editquota |
||
421 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+editquota |
||
422 | * |
||
423 | * @param $username |
||
424 | * @param $newQuota |
||
425 | * @return bool |
||
426 | * @throws ClientExceptions |
||
427 | * @throws Exception |
||
428 | */ |
||
429 | public function changeDiskSpaceQuota($username, $newQuota) |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * This function forces a user to change the account password after the next login attempt. |
||
439 | * |
||
440 | * WHM API function: Accounts -> forcepasswordchange |
||
441 | * |
||
442 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+forcepasswordchange |
||
443 | * |
||
444 | * @param array $usernames |
||
445 | * @param int $stopOnFail |
||
446 | * @return null |
||
447 | * @throws ClientExceptions |
||
448 | * @throws Exception |
||
449 | */ |
||
450 | public function forcePasswordChange(array $usernames, $stopOnFail = 1) |
||
451 | { |
||
452 | $params = [ |
||
453 | "stop_on_failure" => $stopOnFail |
||
454 | ]; |
||
455 | |||
456 | $usersJson = []; |
||
457 | foreach ($usernames as $username){ |
||
458 | $usersJson[$username] = 1; |
||
459 | } |
||
460 | |||
461 | $params['users_json'] = json_encode($usersJson); |
||
462 | |||
463 | $result = $this->client->sendRequest("/json-api/forcepasswordchange", "GET", $params); |
||
464 | if($result['metadata']['result'] === 0){ |
||
465 | throw new ClientExceptions($result['metadata']['reason']); |
||
466 | } |
||
467 | |||
468 | if(!empty($result['data'])){ |
||
469 | return $result['updated']; |
||
470 | } |
||
471 | |||
472 | return null; |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * This function returns information about each domain on the server. |
||
477 | * |
||
478 | * WHM API function: Accounts -> get_domain_info |
||
479 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+get_domain_info |
||
480 | * |
||
481 | * @return Domain[]|null |
||
482 | * @throws ClientExceptions |
||
483 | * @throws Exception |
||
484 | */ |
||
485 | public function getDomains() |
||
520 | } |
||
521 | } |
||
522 |