Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
19 | class Accounts |
||
20 | { |
||
21 | /** |
||
22 | * |
||
23 | * @var WHMClient |
||
24 | */ |
||
25 | protected $client; |
||
26 | |||
27 | /** |
||
28 | * Accounts constructor. |
||
29 | * |
||
30 | * @param WHMClient $client |
||
31 | */ |
||
32 | public function __construct(WHMClient $client) |
||
36 | |||
37 | /** |
||
38 | * Search accounts from your WHM server. |
||
39 | * |
||
40 | * WHM API function: Accounts -> listaccts |
||
41 | * |
||
42 | * $accounts = new Accounts($c); |
||
43 | * $keyword = "search_keyword"; |
||
44 | * $searchType = "username"; //valid search types are "domain", "owner", "user", "ip", "package" |
||
45 | * $options = [ |
||
46 | * 'searchmethod' => "exact", //"exact" or "regex", |
||
47 | * "page" => 1, |
||
48 | * "limit" => 10, //per page, |
||
49 | * "want" => "username" //A comma-separated list of fields you want to fetch |
||
50 | * ]; |
||
51 | * |
||
52 | * try { |
||
53 | * $accounts->searchAccounts($keyword, $searchType, $options); |
||
54 | * } catch (\Http\Client\Exception $e) { |
||
55 | * echo $e->getMessage(); |
||
56 | * } catch (\PreviewTechs\cPanelWHM\Exceptions\ClientExceptions $e) { |
||
57 | * echo $e->getMessage(); |
||
58 | * } |
||
59 | * |
||
60 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+listaccts |
||
61 | * |
||
62 | * @param null $keyword |
||
63 | * @param null $searchType |
||
64 | * @param array $options |
||
65 | * |
||
66 | * @return array |
||
67 | * @throws ClientExceptions |
||
68 | * @throws Exception |
||
69 | */ |
||
70 | public function searchAccounts($keyword = null, $searchType = null, array $options = []) |
||
71 | { |
||
72 | $limit = 10; |
||
73 | $page = 1; |
||
74 | |||
75 | $params = [ |
||
76 | 'api.version' => 1, |
||
77 | 'api.chunk.enable' => 1, |
||
78 | 'api.chunk.size' => $limit, |
||
79 | 'api.chunk.start' => $page * $limit |
||
80 | ]; |
||
81 | |||
82 | if ( ! empty($options['limit'])) { |
||
83 | $params['api.chunk.size'] = intval($options['limit']); |
||
84 | } |
||
85 | |||
86 | if ( ! empty($options['page'])) { |
||
87 | $params['api.chunk.start'] = intval($options['page']) * $params['api.chunk.size']; |
||
88 | } |
||
89 | |||
90 | View Code Duplication | if ( ! empty($searchType) && ! in_array($searchType, ["domain", "owner", "user", "ip", "package"])) { |
|
|
|||
91 | throw new \InvalidArgumentException("`searchType` must be one of these - domain, owner, user, ip, package"); |
||
92 | } |
||
93 | |||
94 | if ( ! empty($options['searchmethod']) && ! in_array($options['searchmethod'], ["exact", "regex"])) { |
||
95 | throw new \InvalidArgumentException("options[searchmethod] must be either `regex` or `exact`"); |
||
96 | } |
||
97 | |||
98 | if ( ! empty($options['want'])) { |
||
99 | $params['want'] = $options['want']; |
||
100 | } |
||
101 | |||
102 | if ( ! empty($searchType)) { |
||
103 | $params['searchtype'] = $searchType; |
||
104 | } |
||
105 | |||
106 | if ( ! empty($keyword)) { |
||
107 | $params['search'] = $keyword; |
||
108 | empty($searchType) ? $params['searchtype'] = "user" : null; |
||
109 | } |
||
110 | |||
111 | $results = $this->client->sendRequest("/json-api/listaccts", "GET", $params); |
||
112 | if (empty($results['data']['acct'])) { |
||
113 | return []; |
||
114 | } |
||
115 | |||
116 | $accounts = []; |
||
117 | foreach ($results['data']['acct'] as $account) { |
||
118 | $accounts[] = Account::buildFromArray($account); |
||
119 | } |
||
120 | |||
121 | return [ |
||
122 | 'accounts' => $accounts, |
||
123 | 'count' => $params['api.chunk.size'], |
||
124 | 'page' => $page |
||
125 | ]; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Get an account details |
||
130 | * |
||
131 | * WHM API function: Accounts -> accountsummary |
||
132 | * |
||
133 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+accountsummary |
||
134 | * |
||
135 | * @param null $user |
||
136 | * @param null $domain |
||
137 | * |
||
138 | * @return null|Account |
||
139 | * @throws ClientExceptions |
||
140 | * @throws Exception |
||
141 | */ |
||
142 | public function getDetails($user = null, $domain = null) |
||
143 | { |
||
144 | if (empty($user) && empty($domain)) { |
||
145 | throw ClientExceptions::invalidArgument("You must provide either a username or a domain or both"); |
||
146 | } |
||
147 | |||
148 | if ( ! empty($user) && ! empty($domain)) { |
||
149 | throw ClientExceptions::invalidArgument( |
||
150 | "You must provide only one argument either user OR domain (not both)" |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | $params = []; |
||
155 | |||
156 | if ( ! empty($user)) { |
||
157 | $params['user'] = $user; |
||
158 | } |
||
159 | |||
160 | if ( ! empty($domain)) { |
||
161 | $params['domain'] = $domain; |
||
162 | } |
||
163 | |||
164 | $result = $this->client->sendRequest("/json-api/accountsummary", "GET", $params); |
||
165 | |||
166 | if (empty($result)) { |
||
167 | return null; |
||
168 | } |
||
169 | |||
170 | if ($result['metadata']['reason'] != "OK") { |
||
171 | throw ClientExceptions::recordNotFound( |
||
172 | ! empty($result['metadata']['reason']) ? $result['metadata']['reason'] : "Record not found" |
||
173 | ); |
||
174 | } |
||
175 | |||
176 | if ( ! empty($result['data']) && is_array($result['data']) && !empty($result['data']['acct'])) { |
||
177 | return Account::buildFromArray($result['data']['acct'][0]); |
||
178 | } |
||
179 | |||
180 | return null; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * This function lists available WHM API 1 functions. |
||
185 | * |
||
186 | * This function only lists the functions that are available to the current user. |
||
187 | * For example, if the authenticated user is a reseller without root -level privileges, |
||
188 | * the function will not list WHM API 1 functions that require root privileges. |
||
189 | * |
||
190 | * WHM API function: Accounts -> applist |
||
191 | * |
||
192 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+applist |
||
193 | * |
||
194 | * @return array |
||
195 | * @throws ClientExceptions |
||
196 | * @throws Exception |
||
197 | */ |
||
198 | public function availableFunctions() |
||
199 | { |
||
200 | $result = $this->client->sendRequest("/json-api/applist", 'GET', []); |
||
201 | |||
202 | if ( ! empty($result['app']) && sizeof($result['app']) > 0) { |
||
203 | return $result['app']; |
||
204 | } |
||
205 | |||
206 | return []; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Create a new account. This function creates a cPanel account. |
||
211 | * The function also sets up the new account's domain information. |
||
212 | * |
||
213 | * WHM API function: Accounts -> createacct |
||
214 | * |
||
215 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+createacct |
||
216 | * |
||
217 | * @param Account $account |
||
218 | * @param array $options |
||
219 | * |
||
220 | * @return array |
||
221 | * @throws ClientExceptions |
||
222 | * @throws Exception |
||
223 | */ |
||
224 | public function create(Account $account, array $options = []) |
||
225 | { |
||
226 | if (empty($account->getUser())) { |
||
227 | throw ClientExceptions::invalidArgument("You must provide an username to create new account"); |
||
228 | } |
||
229 | |||
230 | if (empty($account->getDomain())) { |
||
231 | throw ClientExceptions::invalidArgument("You must provide a domain to create new account"); |
||
232 | } |
||
233 | |||
234 | $params = []; |
||
235 | $params['username'] = $account->getUser(); |
||
236 | $params['domain'] = $account->getDomain(); |
||
237 | ! empty($account->getPlanName()) ? $params['plan'] = $account->getPlanName() : null; |
||
238 | ! empty($options['pkgname']) ? $params['pkgname'] = $options['pkgname'] : null; |
||
239 | if ( ! empty($options['savepkg'])) { |
||
240 | if ( ! in_array(intval($options['savepkg']), [0, 1])) { |
||
241 | throw new ClientExceptions("`savepkg` must be either 0 or 1"); |
||
242 | } |
||
243 | |||
244 | $params['savepkg'] = $options['savepkg']; |
||
245 | } |
||
246 | |||
247 | ! empty($options['featurelist']) ? $params['featurelist'] = $options['featurelist'] : null; |
||
248 | if ( ! empty($account->getDiskLimit())) { |
||
249 | if ($account->getDiskLimit() === -1) { |
||
250 | $params['quota'] = 0; |
||
251 | } else { |
||
252 | $params['quota'] = intval($account->getDiskLimit()); |
||
253 | } |
||
254 | } |
||
255 | |||
256 | ! empty($account->getPassword()) ? $params['password'] = $account->getPassword() : null; |
||
257 | ! empty($account->getIpAddress()) ? $params['ip'] = $account->getIpAddress() : null; |
||
258 | ! empty($account->isCgiEnable()) ? $params['cgi'] = (int)$account->isCgiEnable() : null; |
||
259 | ! empty($account->isSpamAssassinEnable()) ? |
||
260 | $params['spamassassin'] = (int)$account->isSpamAssassinEnable() |
||
261 | : null; |
||
262 | ! empty($account->isFrontPageEnable()) ? $params['frontpage'] = (int)$account->isFrontPageEnable() : null; |
||
263 | ! empty($account->getShell()) ? $params['hasshell'] = 1 : null; |
||
264 | ! empty($account->getEmail()) ? $params['contactemail'] = $account->getEmail() : null; |
||
265 | ! empty($account->getTheme()) ? $params['cpmod'] = 1 : null; |
||
266 | |||
267 | View Code Duplication | if ($account->getMaxFTP() === -1) { |
|
268 | $params['maxftp'] = "unlimited"; |
||
269 | } elseif ($account->getMaxFTP() > 0) { |
||
270 | $params['maxftp'] = intval($account->getMaxFTP()); |
||
271 | } |
||
272 | |||
273 | View Code Duplication | if ($account->getMaxSQL() === -1) { |
|
274 | $params['maxsql'] = "unlimited"; |
||
275 | } elseif ($account->getMaxSQL() > 0) { |
||
276 | $params['maxsql'] = intval($account->getMaxSQL()); |
||
277 | } |
||
278 | |||
279 | View Code Duplication | if ($account->getMaxPOP() === -1) { |
|
280 | $params['maxpop'] = "unlimited"; |
||
281 | } elseif ($account->getMaxPOP() > 0) { |
||
282 | $params['maxpop'] = intval($account->getMaxPOP()); |
||
283 | } |
||
284 | |||
285 | View Code Duplication | if ($account->getMaxMailingList() === -1) { |
|
286 | $params['maxlst'] = "unlimited"; |
||
287 | } elseif ($account->getMaxMailingList() > 0) { |
||
288 | $params['maxlst'] = intval($account->getMaxMailingList()); |
||
289 | } |
||
290 | |||
291 | View Code Duplication | if ($account->getMaxSubDomain() === -1) { |
|
292 | $params['maxsub'] = "unlimited"; |
||
293 | } elseif ($account->getMaxSubDomain() > 0) { |
||
294 | $params['maxsub'] = intval($account->getMaxSubDomain()); |
||
295 | } |
||
296 | |||
297 | View Code Duplication | if ($account->getMaxParkedDomains() === -1) { |
|
298 | $params['maxpark'] = "unlimited"; |
||
299 | } elseif ($account->getMaxParkedDomains() > 0) { |
||
300 | $params['maxpark'] = intval($account->getMaxParkedDomains()); |
||
301 | } |
||
302 | |||
303 | View Code Duplication | if ($account->getMaxAddonDomains() === -1) { |
|
304 | $params['maxaddon'] = "unlimited"; |
||
305 | } elseif ($account->getMaxAddonDomains() > 0) { |
||
306 | $params['maxaddon'] = intval($account->getMaxAddonDomains()); |
||
307 | } |
||
308 | |||
309 | View Code Duplication | if ($account->getBandwidthLimit() === -1) { |
|
310 | $params['bwlimit'] = "unlimited"; |
||
311 | } elseif ($account->getBandwidthLimit() > 0) { |
||
312 | $params['bwlimit'] = intval($account->getBandwidthLimit()); |
||
313 | } |
||
314 | |||
315 | ! empty($options['customip']) ? $params['customip'] = $options['customip'] : null; |
||
316 | |||
317 | ! empty($account->getLanguagePreference()) ? $params['language'] = $account->getLanguagePreference() : null; |
||
318 | |||
319 | ! empty($options['useregns']) ? $params['useregns'] = $options['useregns'] : null; |
||
320 | ! empty($options['reseller']) ? $params['reseller'] = (int)$options['reseller'] : null; |
||
321 | ! empty($options['forcedns']) ? $params['forcedns'] = (int)$options['forcedns'] : null; |
||
322 | |||
323 | ! empty($account->getMailboxFormat()) ? $params['mailbox_format'] = $account->getMailboxFormat() : null; |
||
324 | |||
325 | if ( ! empty($options['mxcheck'])) { |
||
326 | if ( ! in_array($options['mxcheck'], ['local', 'secondary', 'remote', 'auto'])) { |
||
327 | throw new ClientExceptions("options[mxcheck] parameters must be one of local, secondary, remote, auto"); |
||
328 | } |
||
329 | |||
330 | $params['mxcheck'] = $options['mxcheck']; |
||
331 | } |
||
332 | |||
333 | View Code Duplication | if ($account->getMaxEmailPerHour() === -1) { |
|
334 | $params['max_email_per_hour'] = "unlimited"; |
||
335 | } elseif ($account->getMaxEmailPerHour() > 0) { |
||
336 | $params['max_email_per_hour'] = intval($account->getMaxEmailPerHour()); |
||
337 | } |
||
338 | |||
339 | if ($account->getMaxEmailAccountQuota() === -1) { |
||
340 | $params['max_emailacct_quota'] = "unlimited"; |
||
341 | } elseif ($account->getMaxEmailAccountQuota() > 0) { |
||
342 | $params['max_email_per_hour'] = intval($account->getMaxEmailAccountQuota()); |
||
343 | } |
||
344 | |||
345 | View Code Duplication | if ($account->getMaxDeferFailMailPercentage() === -1) { |
|
346 | $params['max_defer_fail_percentage'] = "unlimited"; |
||
347 | } elseif ($account->getMaxDeferFailMailPercentage() > 0) { |
||
348 | $params['max_defer_fail_percentage'] = intval($account->getMaxDeferFailMailPercentage()); |
||
349 | } |
||
350 | |||
351 | ! empty($account->getUid()) ? $params['uid'] = $account->getUid() : null; |
||
352 | ! empty($account->getPartition()) ? $params['homedir'] = $account->getPartition() : null; |
||
353 | ! empty($options['dkim']) ? $params['dkim'] = intval($options['dkim']) : null; |
||
354 | ! empty($options['spf']) ? $params['spf'] = intval($options['spf']) : null; |
||
355 | ! empty($account->getOwner()) ? $params['owner'] = $account->getOwner() : null; |
||
356 | |||
357 | $result = $this->client->sendRequest("/json-api/createacct", "GET", $params); |
||
358 | |||
359 | if(!empty($result['metadata']) && $result['metadata']['result'] === 0){ |
||
360 | throw new ClientExceptions($result['metadata']['reason']); |
||
361 | } |
||
362 | |||
363 | if ( ! empty($result) && ! empty($result['result'][0]) && $result['result'][0]['status'] === 0) { |
||
364 | throw new ClientExceptions($result['result'][0]['statusmsg']); |
||
365 | } |
||
366 | |||
367 | if ( ! empty($result) && ! empty($result['result'][0]) && $result['result'][0]['status'] === 1) { |
||
368 | return ['result' => $result['result'][0]['options'], 'raw_output' => $result['result'][0]['rawout']]; |
||
369 | } |
||
370 | |||
371 | if(!empty($result['metadata']) && $result['metadata']['result'] === 1){ |
||
372 | return ['result' => $result['data'], 'raw_output' => $result['metadata']['output']['raw']]; |
||
373 | } |
||
374 | |||
375 | return []; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * This function retrieves domain data. |
||
380 | * |
||
381 | * WHM API function: Accounts -> domainuserdata |
||
382 | * |
||
383 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+domainuserdata |
||
384 | * |
||
385 | * @param $domain |
||
386 | * |
||
387 | * @return null|DomainUser |
||
388 | * @throws ClientExceptions |
||
389 | * @throws Exception |
||
390 | */ |
||
391 | public function domainDetails($domain) |
||
430 | |||
431 | /** |
||
432 | * This function modifies a user's disk quota. |
||
433 | * WHM API function: Accounts -> editquota |
||
434 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+editquota |
||
435 | * |
||
436 | * @param $username |
||
437 | * @param $newQuota |
||
438 | * |
||
439 | * @return bool |
||
440 | * @throws ClientExceptions |
||
441 | * @throws Exception |
||
442 | */ |
||
443 | public function changeDiskSpaceQuota($username, $newQuota) |
||
451 | |||
452 | /** |
||
453 | * This function forces a user to change the account password after the next login attempt. |
||
454 | * |
||
455 | * WHM API function: Accounts -> forcepasswordchange |
||
456 | * |
||
457 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+forcepasswordchange |
||
458 | * |
||
459 | * @param array $usernames |
||
460 | * @param int $stopOnFail |
||
461 | * |
||
462 | * @return null |
||
463 | * @throws ClientExceptions |
||
464 | * @throws Exception |
||
465 | */ |
||
466 | public function forcePasswordChange(array $usernames, $stopOnFail = 1) |
||
467 | { |
||
468 | $params = [ |
||
469 | "stop_on_failure" => $stopOnFail |
||
470 | ]; |
||
471 | |||
472 | $usersJson = []; |
||
473 | foreach ($usernames as $username) { |
||
474 | $usersJson[$username] = 1; |
||
475 | } |
||
476 | |||
477 | $params['users_json'] = json_encode($usersJson); |
||
478 | |||
479 | $result = $this->client->sendRequest("/json-api/forcepasswordchange", "GET", $params); |
||
480 | if ($result['metadata']['result'] === 0) { |
||
481 | throw new ClientExceptions($result['metadata']['reason']); |
||
482 | } |
||
483 | |||
484 | if ( ! empty($result['data'])) { |
||
485 | return $result['updated']; |
||
486 | } |
||
487 | |||
488 | return null; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * This function returns information about each domain on the server. |
||
493 | * |
||
494 | * WHM API function: Accounts -> get_domain_info |
||
495 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+get_domain_info |
||
496 | * |
||
497 | * @return Domain[]|null |
||
498 | * @throws ClientExceptions |
||
499 | * @throws Exception |
||
500 | */ |
||
501 | public function getDomains() |
||
537 | |||
538 | /** |
||
539 | * Digest Authentication is enabled or disabled for any specific user. |
||
540 | * This function checks whether Digest Authentication is enabled for |
||
541 | * a cPanel user. |
||
542 | * Windows® Vista, Windows® 7, and Windows® 8 require Digest Authentication |
||
543 | * support in order to access Web Disk over an unencrypted connection. |
||
544 | * |
||
545 | * WHM API function: Accounts -> has_digest_auth |
||
546 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+has_digest_auth |
||
547 | * |
||
548 | * @param $user |
||
549 | * |
||
550 | * @return bool|null |
||
551 | * @throws ClientExceptions |
||
552 | * @throws Exception |
||
553 | */ |
||
554 | View Code Duplication | public function hasDigestAuth($user) |
|
564 | |||
565 | /** |
||
566 | * This function enables or disables Digest Authentication for an account. |
||
567 | * Windows Vista®, Windows® 7, and Windows® 8 requires that |
||
568 | * you enable Digest Authentication support in order to access your Web Disk over a clear text, |
||
569 | * unencrypted connection. |
||
570 | * |
||
571 | * WHM API function: Accounts -> set_digest_auth |
||
572 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+set_digest_auth |
||
573 | * |
||
574 | * @param $user |
||
575 | * @param $password |
||
576 | * @param bool $enableDigestAuth |
||
577 | * |
||
578 | * @return bool |
||
579 | * @throws ClientExceptions |
||
580 | * @throws Exception |
||
581 | */ |
||
582 | public function setDigestAuth($user, $password, $enableDigestAuth = true) |
||
583 | { |
||
584 | $params = ['user' => $user, 'password' => $password, 'digestauth' => (int) $enableDigestAuth]; |
||
585 | $result = $this->client->sendRequest("/json-api/set_digest_auth", "GET", $params); |
||
586 | |||
587 | if(!empty($result['metadata']) && $result['metadata']['result'] === 0){ |
||
588 | throw new ClientExceptions($result['metadata']['reason']); |
||
589 | } |
||
590 | |||
591 | if(!empty($result['metadata']) && $result['metadata']['result'] === 1){ |
||
592 | return true; |
||
593 | } |
||
594 | |||
595 | return false; |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * This function checks whether a cPanel user's home directory contains a valid .my.cnf file. |
||
600 | * WHM API function: Accounts -> has_mycnf_for_cpuser |
||
601 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+has_mycnf_for_cpuser |
||
602 | * |
||
603 | * @param $user |
||
604 | * |
||
605 | * @return bool|null |
||
606 | * @throws ClientExceptions |
||
607 | * @throws Exception |
||
608 | */ |
||
609 | View Code Duplication | public function hasMyCnfInHomeDirectory($user) |
|
619 | |||
620 | /** |
||
621 | * Modify a cPanel account's bandwidth quota. |
||
622 | * WHM API function: Accounts -> limitbw |
||
623 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+limitbw |
||
624 | * |
||
625 | * @param $user |
||
626 | * @param $bwlimit |
||
627 | * |
||
628 | * @return null |
||
629 | * @throws ClientExceptions |
||
630 | * @throws Exception |
||
631 | */ |
||
632 | View Code Duplication | public function limitBandwidth($user, $bwlimit) |
|
633 | { |
||
634 | $params = ['user' => $user, 'bwlimit' => intval($bwlimit)]; |
||
635 | $result = $this->client->sendRequest("/json-api/limitbw", "GET", $params); |
||
636 | if ( ! empty($result['metadata']) && $result['metadata']['result'] === 1) { |
||
637 | return $result['data']['bwlimits'][0]; |
||
638 | } |
||
639 | |||
640 | return null; |
||
641 | } |
||
642 | |||
643 | |||
644 | /** |
||
645 | * Get lists of the cPanel user accounts and the root user on the server. |
||
646 | * WHM API function: Accounts -> list_users |
||
647 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+list_users |
||
648 | * |
||
649 | * @return array |
||
650 | * @throws ClientExceptions |
||
651 | * @throws Exception |
||
652 | */ |
||
653 | View Code Duplication | public function getUsers() |
|
662 | |||
663 | /** |
||
664 | * Get a list of locked accounts. |
||
665 | * This function lists locked accounts on the server. Only WHM users with root-level privileges can unsuspend locked accounts. |
||
666 | * |
||
667 | * WHM API function: Accounts -> listlockedaccounts |
||
668 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+listlockedaccounts |
||
669 | * |
||
670 | * @return array |
||
671 | * @throws ClientExceptions |
||
672 | * @throws Exception |
||
673 | */ |
||
674 | View Code Duplication | public function getLockedAccounts() |
|
683 | |||
684 | /** |
||
685 | * Get a list of suspended accounts |
||
686 | * |
||
687 | * WHM API function: listsuspended |
||
688 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+listsuspended |
||
689 | * |
||
690 | * @return SuspendedAccount[] |
||
691 | * @throws ClientExceptions |
||
692 | * @throws Exception |
||
693 | */ |
||
694 | public function getSuspendedAccounts() |
||
716 | |||
717 | /** |
||
718 | * This function modifies a cPanel account. |
||
719 | * You must define the user parameter to identify which account to update. |
||
720 | * All other input parameters are optional, and assign new values to the account. |
||
721 | * If you specify the current value in one of these parameters, no change will occur. |
||
722 | * |
||
723 | * WHM API function: Accounts -> modifyacct |
||
724 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+modifyacct |
||
725 | * |
||
726 | * @param Account $account |
||
727 | * |
||
728 | * @return bool |
||
729 | * @throws ClientExceptions |
||
730 | * @throws Exception |
||
731 | */ |
||
732 | public function modifyAccount(Account $account) |
||
844 | |||
845 | /** |
||
846 | * WHM API function: Accounts -> passwd |
||
847 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+passwd |
||
848 | * |
||
849 | * @param $user |
||
850 | * @param $newPassword |
||
851 | * @param null $digestAuth |
||
852 | * @param bool $dbPassUpdate |
||
853 | * |
||
854 | * @return null|array |
||
855 | * @throws ClientExceptions |
||
856 | * @throws Exception |
||
857 | */ |
||
858 | public function changePassword($user, $newPassword, $digestAuth = null, $dbPassUpdate = false) |
||
885 | |||
886 | /** |
||
887 | * This function deletes a cPanel or WHM account. |
||
888 | * |
||
889 | * WHM API function: Accounts -> removeacct |
||
890 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+removeacct |
||
891 | * @param $username |
||
892 | * @param bool $keepDNS |
||
893 | * |
||
894 | * @return bool |
||
895 | * @throws ClientExceptions |
||
896 | * @throws Exception |
||
897 | */ |
||
898 | View Code Duplication | public function remove($username, $keepDNS = false) |
|
913 | |||
914 | /** |
||
915 | * This function retrieves account bandwidth information. |
||
916 | * WHM API function: Accounts -> showbw |
||
917 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+showbw |
||
918 | * |
||
919 | * @param null $month |
||
920 | * @param null $year |
||
921 | * @param null $resellerUsername |
||
922 | * @param null $searchKeyword |
||
923 | * @param null $searchType |
||
924 | * |
||
925 | * @return array |
||
926 | * @throws ClientExceptions |
||
927 | * @throws Exception |
||
928 | */ |
||
929 | public function getBandwidthInfo($month = null, $year = null, $resellerUsername = null, $searchKeyword = null, $searchType = null) |
||
975 | |||
976 | /** |
||
977 | * This function suspends an account. Suspension denies the user access to the account. Unlike account deletion, you can reverse account suspension. |
||
978 | * WHM API function: Accounts->suspendacct |
||
979 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+suspendacct |
||
980 | * |
||
981 | * @param $user |
||
982 | * @param $reason |
||
983 | * @param bool $disallowUnsuspension |
||
984 | * |
||
985 | * @return bool |
||
986 | * @throws ClientExceptions |
||
987 | * @throws Exception |
||
988 | */ |
||
989 | public function suspend($user, $reason, $disallowUnsuspension = true) |
||
990 | { |
||
991 | $params = ['user' => $user, 'reason' => $reason, 'disallowun' => (int) $disallowUnsuspension]; |
||
992 | $result = $this->client->sendRequest("/json-api/suspendacct", "GET", $params); |
||
993 | |||
994 | if(!empty($result['metadata']) && $result['metadata']['result'] === 0){ |
||
995 | throw new ClientExceptions($result['metadata']['reason']); |
||
996 | } |
||
997 | |||
998 | if(!empty($result['metadata']) && $result['metadata']['result'] === 1){ |
||
999 | return true; |
||
1000 | } |
||
1001 | |||
1002 | return false; |
||
1003 | } |
||
1004 | |||
1005 | /** |
||
1006 | * This function unsuspends an account. |
||
1007 | * WHM API function: Accounts -> unsuspendacct |
||
1008 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+unsuspendacct |
||
1009 | * |
||
1010 | * @param $user |
||
1011 | * |
||
1012 | * @return bool |
||
1013 | * @throws ClientExceptions |
||
1014 | * @throws Exception |
||
1015 | */ |
||
1016 | View Code Duplication | public function unsuspend($user) |
|
1031 | |||
1032 | /** |
||
1033 | * This function checks for username conflicts during account creation. |
||
1034 | * WHM API function: verify_new_username |
||
1035 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+verify_new_username |
||
1036 | * |
||
1037 | * @param $username |
||
1038 | * |
||
1039 | * @return bool |
||
1040 | * @throws ClientExceptions |
||
1041 | * @throws Exception |
||
1042 | */ |
||
1043 | View Code Duplication | public function validateNewUsername($username) |
|
1056 | |||
1057 | /** |
||
1058 | * This generates a URL for one-click login to cPanel. |
||
1059 | * |
||
1060 | * You can replace $service with a supported service, defaults to cPanel |
||
1061 | * $app when set to something invalid will send the user to the cPanel home page, you |
||
1062 | * can set it to anything valid to override the default of 'home' |
||
1063 | * |
||
1064 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+create_user_session |
||
1065 | * |
||
1066 | * @param $username |
||
1067 | * @param string $service |
||
1068 | * @param string $app |
||
1069 | * |
||
1070 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
1071 | * @throws ClientExceptions |
||
1072 | * @throws Exception |
||
1073 | */ |
||
1074 | public function createUserSession($username, $service = 'cpaneld', $app = 'home') |
||
1086 | |||
1087 | /** |
||
1088 | * This changes the plan set on the account |
||
1089 | * |
||
1090 | * This is purely for changing the plan of the account |
||
1091 | * |
||
1092 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+modifyacct |
||
1093 | * |
||
1094 | * @param $username |
||
1095 | * @param $plan |
||
1096 | * |
||
1097 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
1098 | * @throws ClientExceptions |
||
1099 | * @throws Exception |
||
1100 | */ |
||
1101 | View Code Duplication | public function changePlan($username, $plan) |
|
1113 | } |
||
1114 |
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.