Total Complexity | 94 |
Total Lines | 399 |
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 |
||
16 | class Accounts |
||
17 | { |
||
18 | /** |
||
19 | * |
||
20 | * @var WHMClient |
||
21 | */ |
||
22 | protected $client; |
||
23 | |||
24 | /** |
||
25 | * Accounts constructor. |
||
26 | * |
||
27 | * @param WHMClient $client |
||
28 | */ |
||
29 | public function __construct(WHMClient $client) |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Search accounts from your WHM server. |
||
36 | * |
||
37 | * WHM API function: Accounts -> listaccts |
||
38 | * |
||
39 | * $accounts = new Accounts($c); |
||
40 | * $keyword = "search_keyword"; |
||
41 | * $searchType = "username"; //valid search types are "domain", "owner", "user", "ip", "package" |
||
42 | * $options = [ |
||
43 | * 'searchmethod' => "exact", //"exact" or "regex", |
||
44 | * "page" => 1, |
||
45 | * "limit" => 10, //per page, |
||
46 | * "want" => "username" //A comma-separated list of fields you want to fetch |
||
47 | * ]; |
||
48 | * |
||
49 | * try { |
||
50 | * $accounts->searchAccounts($keyword, $searchType, $options); |
||
51 | * } catch (\Http\Client\Exception $e) { |
||
52 | * echo $e->getMessage(); |
||
53 | * } catch (\PreviewTechs\cPanelWHM\Exceptions\ClientExceptions $e) { |
||
54 | * echo $e->getMessage(); |
||
55 | * } |
||
56 | * |
||
57 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+listaccts |
||
58 | * |
||
59 | * @param null $keyword |
||
|
|||
60 | * @param null $searchType |
||
61 | * @param array $options |
||
62 | * |
||
63 | * @return array |
||
64 | * @throws ClientExceptions |
||
65 | * @throws Exception |
||
66 | */ |
||
67 | public function searchAccounts($keyword = null, $searchType = null, array $options = []) |
||
68 | { |
||
69 | $limit = 10; |
||
70 | $page = 1; |
||
71 | |||
72 | $params = [ |
||
73 | 'api.version' => 1, |
||
74 | 'api.chunk.enable' => 1, |
||
75 | 'api.chunk.size' => $limit, |
||
76 | 'api.chunk.start' => $page * $limit |
||
77 | ]; |
||
78 | |||
79 | if (!empty($options['limit'])) { |
||
80 | $params['api.chunk.size'] = intval($options['limit']); |
||
81 | } |
||
82 | |||
83 | if (!empty($options['page'])) { |
||
84 | $params['api.chunk.start'] = intval($options['page']) * $params['api.chunk.size']; |
||
85 | } |
||
86 | |||
87 | if (!empty($searchType) && !in_array($searchType, ["domain", "owner", "user", "ip", "package"])) { |
||
88 | throw new \InvalidArgumentException("`searchType` must be one of these - domain, owner, user, ip, package"); |
||
89 | } |
||
90 | |||
91 | if (!empty($options['searchmethod']) && !in_array($options['searchmethod'], ["exact", "regex"])) { |
||
92 | throw new \InvalidArgumentException("options[searchmethod] must be either `regex` or `exact`"); |
||
93 | } |
||
94 | |||
95 | if (!empty($options['want'])) { |
||
96 | $params['want'] = $options['want']; |
||
97 | } |
||
98 | |||
99 | if (!empty($searchType)) { |
||
100 | $params['searchtype'] = $searchType; |
||
101 | } |
||
102 | |||
103 | if (!empty($keyword)) { |
||
104 | $params['search'] = $keyword; |
||
105 | empty($searchType) ? $params['searchtype'] = "user" : null; |
||
106 | } |
||
107 | |||
108 | $results = $this->client->sendRequest("/json-api/listaccts", "GET", $params); |
||
109 | if (empty($results['data']['acct'])) { |
||
110 | return []; |
||
111 | } |
||
112 | |||
113 | $accounts = []; |
||
114 | foreach ($results['data']['acct'] as $account) { |
||
115 | $accounts[] = Account::buildFromArray($account); |
||
116 | } |
||
117 | |||
118 | return [ |
||
119 | 'accounts' => $accounts, |
||
120 | 'count' => $params['api.chunk.size'], |
||
121 | 'page' => $page |
||
122 | ]; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get an account details |
||
127 | * |
||
128 | * WHM API function: Accounts -> accountsummary |
||
129 | * |
||
130 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+accountsummary |
||
131 | * |
||
132 | * @param null $user |
||
133 | * @param null $domain |
||
134 | * |
||
135 | * @return null|Account |
||
136 | * @throws ClientExceptions |
||
137 | * @throws Exception |
||
138 | */ |
||
139 | public function getDetails($user = null, $domain = null) |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * This function lists available WHM API 1 functions. |
||
181 | * |
||
182 | * This function only lists the functions that are available to the current user. |
||
183 | * For example, if the authenticated user is a reseller without root -level privileges, |
||
184 | * the function will not list WHM API 1 functions that require root privileges. |
||
185 | * |
||
186 | * WHM API function: Accounts -> applist |
||
187 | * |
||
188 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+applist |
||
189 | * |
||
190 | * @return array |
||
191 | * @throws ClientExceptions |
||
192 | * @throws Exception |
||
193 | */ |
||
194 | public function availableFunctions() |
||
195 | { |
||
196 | $result = $this->client->sendRequest("/json-api/applist", 'GET', []); |
||
197 | |||
198 | if (!empty($result['app']) && sizeof($result['app']) > 0) { |
||
199 | return $result['app']; |
||
200 | } |
||
201 | |||
202 | return []; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Create a new account. This function creates a cPanel account. |
||
207 | * The function also sets up the new account's domain information. |
||
208 | * |
||
209 | * WHM API function: Accounts -> createacct |
||
210 | * |
||
211 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+createacct |
||
212 | * |
||
213 | * @param Account $account |
||
214 | * @param array $options |
||
215 | * @return array |
||
216 | * @throws ClientExceptions |
||
217 | * @throws Exception |
||
218 | */ |
||
219 | public function create(Account $account, array $options = []) |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * This function retrieves domain data. |
||
368 | * |
||
369 | * WHM API function: Accounts -> domainuserdata |
||
370 | * |
||
371 | * @link https://documentation.cpanel.net/display/DD/WHM+API+1+Functions+-+domainuserdata |
||
372 | * @param $domain |
||
373 | * @return null|DomainUser |
||
374 | * @throws ClientExceptions |
||
375 | * @throws Exception |
||
376 | */ |
||
377 | public function domainDetails($domain) |
||
415 | } |
||
416 | } |
||
417 |