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