Total Complexity | 47 |
Total Lines | 429 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like ApiService 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 ApiService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class ApiService extends Service |
||
34 | { |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $url = ''; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $page = 1; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $limit = 15; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $order = 'id desc'; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private $where = []; |
||
59 | |||
60 | /** |
||
61 | * @var |
||
62 | */ |
||
63 | private $contents, $backtrack, $key, $panel; |
||
64 | |||
65 | /** |
||
66 | * @param string $key |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function key(string $key): self |
||
70 | { |
||
71 | $this->key = $key; |
||
72 | return $this; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param string $panel |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function panel(string $panel) |
||
80 | { |
||
81 | $this->panel = $panel; |
||
82 | return $this; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * 获取配置信息 |
||
87 | * @return $this |
||
88 | */ |
||
89 | private function getConfig(): self |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * 获取监控信息 |
||
98 | * @param string $type 类型 GetCpuIo = CPU信息/内存 GetDiskIo = 磁盘IO GetNetWorkIo = 网络IO |
||
99 | * @param int $start_time 开始时间 |
||
100 | * @param int $end_time 结束时间 |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public function getCpuIoInfo($type = 'GetCpuIo', $start_time = 0, $end_time = 0) |
||
104 | { |
||
105 | if (empty($start_time)) { |
||
106 | $start_time = strtotime(date('Y-m-d')); |
||
107 | } |
||
108 | if (empty($end_time)) { |
||
109 | $end_time = time(); |
||
110 | } |
||
111 | $this->url = "/ajax?action={$type}&start={$start_time}&end={$end_time}"; |
||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * 获取网站列表 |
||
117 | * @return mixed |
||
118 | */ |
||
119 | public function getSites() |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * 获取数据库列表 |
||
128 | * @return mixed |
||
129 | */ |
||
130 | public function getDatabases() |
||
131 | { |
||
132 | $this->url = 'data?action=getData'; |
||
133 | $this->where['tojs'] = 'database.get_list'; |
||
134 | $this->where['table'] = 'databases'; |
||
135 | $this->where['limit'] = $this->limit; |
||
136 | $this->where['p'] = $this->page; |
||
137 | $this->where['order'] = $this->order; |
||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * 获取防火墙 |
||
143 | * @return mixed |
||
144 | */ |
||
145 | public function getFirewalls() |
||
146 | { |
||
147 | $this->url = 'data?action=getData'; |
||
148 | $this->where['tojs'] = 'firewall.get_list'; |
||
149 | $this->where['table'] = 'firewall'; |
||
150 | $this->where['limit'] = $this->limit; |
||
151 | $this->where['p'] = $this->page; |
||
152 | $this->where['order'] = $this->order; |
||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * 获取面板日志 |
||
158 | * @return mixed |
||
159 | */ |
||
160 | public function getLogs() |
||
161 | { |
||
162 | $this->url = 'data?action=getData'; |
||
163 | $this->where['tojs'] = 'firewall.get_log_list'; |
||
164 | $this->where['table'] = 'logs'; |
||
165 | $this->where['limit'] = $this->limit; |
||
166 | $this->where['p'] = $this->page; |
||
167 | $this->where['order'] = $this->order; |
||
168 | return $this; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * 获取消息通道 |
||
173 | * @return mixed |
||
174 | */ |
||
175 | public function getNews() |
||
176 | { |
||
177 | $this->url = 'config?action=get_settings'; |
||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * 获取网站列表 |
||
183 | * @return mixed |
||
184 | */ |
||
185 | public function getCronTabs() |
||
186 | { |
||
187 | $this->url = 'data?action=getData'; |
||
188 | $this->where['tojs'] = 'site.get_list'; |
||
189 | $this->where['table'] = 'sites'; |
||
190 | $this->where['limit'] = $this->limit; |
||
191 | $this->where['p'] = $this->page; |
||
192 | $this->where['order'] = $this->order; |
||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * 获取网站分类 |
||
198 | * @return mixed |
||
199 | */ |
||
200 | public function getTypes() |
||
201 | { |
||
202 | $this->url = 'site?action=get_site_types'; |
||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * 获取软件列表 |
||
208 | * @return mixed |
||
209 | */ |
||
210 | public function getSoFts() |
||
211 | { |
||
212 | $this->url = 'plugin?action=get_soft_list'; |
||
213 | $this->where['p'] = $this->page; |
||
214 | $this->where['tojs'] = 'soft.get_list'; |
||
215 | return $this; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * 获取硬盘信息 |
||
220 | * @return mixed |
||
221 | */ |
||
222 | public function getDiskInfo() |
||
223 | { |
||
224 | $this->url = 'system?action=GetDiskInfo'; |
||
225 | return $this; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * 获取信息系统 |
||
230 | * @return mixed |
||
231 | */ |
||
232 | public function getSystemTotal() |
||
233 | { |
||
234 | $this->url = 'system?action=GetSystemTotal'; |
||
235 | return $this; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * 获取用户信息 |
||
240 | * @return mixed |
||
241 | */ |
||
242 | public function getUserInfo() |
||
243 | { |
||
244 | $this->url = 'ssl?action=GetUserInfo'; |
||
245 | return $this; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * 获取网络信息 |
||
250 | * @return mixed |
||
251 | */ |
||
252 | public function getNetWork() |
||
253 | { |
||
254 | $this->url = 'system?action=GetNetWork'; |
||
255 | return $this; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * 获取插件信息 |
||
260 | * @return mixed |
||
261 | */ |
||
262 | public function getPlugin() |
||
263 | { |
||
264 | $this->url = 'plugin?action=get_index_list'; |
||
265 | return $this; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * 获取软件信息 |
||
270 | * @return mixed |
||
271 | */ |
||
272 | public function getSoft() |
||
273 | { |
||
274 | $this->url = 'plugin?action=get_soft_list'; |
||
275 | return $this; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * 获取更新信息 |
||
280 | * @return mixed |
||
281 | */ |
||
282 | public function getUpdatePanel() |
||
283 | { |
||
284 | $this->url = 'ajax?action=UpdatePanel'; |
||
285 | return $this; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * 当前页码 |
||
290 | * @param int $is |
||
291 | * @return $this |
||
292 | */ |
||
293 | public function page(int $is = 1): self |
||
294 | { |
||
295 | $this->page = $is; |
||
296 | return $this; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * 返回数量 |
||
301 | * @param int $is |
||
302 | * @return $this |
||
303 | */ |
||
304 | public function limit(int $is = 15): self |
||
305 | { |
||
306 | $this->limit = $is; |
||
307 | return $this; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * 排序 |
||
312 | * @param string $ss |
||
313 | * @return $this |
||
314 | */ |
||
315 | public function order(string $ss = 'id desc'): self |
||
316 | { |
||
317 | $this->order = $ss; |
||
318 | return $this; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * 查询条件 |
||
323 | * @param array $array |
||
324 | * @return ApiService |
||
325 | */ |
||
326 | public function where($array = []): ApiService |
||
327 | { |
||
328 | $this->where = $array; |
||
329 | return $this; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * 获取数据和总数 |
||
334 | * @return $this |
||
335 | */ |
||
336 | private function getDataWithOrderOpt(): self |
||
337 | { |
||
338 | $this->backtrack['data'] = $this->contents['data']; |
||
339 | $this->backtrack['orderOpt'] = $this->contents['orderOpt']; |
||
340 | return $this; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * 获取数据和总数 |
||
345 | * @return $this |
||
346 | */ |
||
347 | private function getDataWithCount(): self |
||
348 | { |
||
349 | if (empty($this->contents['data'])) { |
||
350 | $this->contents['data'] = []; |
||
351 | } |
||
352 | if (!is_array($this->contents['data'])) { |
||
353 | $this->contents['data'] = []; |
||
354 | } |
||
355 | $this->backtrack['data'] = $this->contents; |
||
356 | if (empty($this->contents['page'])) { |
||
357 | $this->contents['page'] = 0; |
||
358 | } |
||
359 | $this->backtrack['count'] = $this->getCountData($this->contents['page']); |
||
360 | return $this; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * 获取数据 |
||
365 | * @return $this |
||
366 | */ |
||
367 | private function getData() |
||
|
|||
368 | { |
||
369 | $this->backtrack['data'] = $this->contents; |
||
370 | return $this; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * 发起网络请求 |
||
375 | * @return $this |
||
376 | * @throws DtaException |
||
377 | */ |
||
378 | private function getHttp(): self |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * 返回Array |
||
387 | * @return array|mixed |
||
388 | * @throws DtaException |
||
389 | */ |
||
390 | public function toArray() |
||
391 | { |
||
392 | $this->getHttp(); |
||
393 | if ($this->where['type'] === 'sites') { |
||
394 | $this->getDataWithOrderOpt(); |
||
395 | } else { |
||
396 | $this->getDataWithCount(); |
||
397 | } |
||
398 | if (empty($this->backtrack)) { |
||
399 | return []; |
||
400 | } |
||
401 | if (is_array($this->backtrack)) { |
||
402 | return $this->backtrack; |
||
403 | } |
||
404 | return json_decode($this->backtrack, true); |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * 发起POST请求 |
||
409 | * @param string $url 网址 |
||
410 | * @param array $data 数据 |
||
411 | * @param bool $is_json 是否返回Json格式 |
||
412 | * @return bool|mixed|string |
||
413 | * @throws DtaException |
||
414 | */ |
||
415 | protected function HttpPostCookie(string $url, array $data = [], bool $is_json = true) |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * 获取总数 |
||
450 | * @param string $str |
||
451 | * @return false|int|string |
||
452 | */ |
||
453 | protected function getCountData(string $str) |
||
462 | } |
||
463 | } |
||
464 |
This check looks for private methods that have been defined, but are not used inside the class.