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