Total Complexity | 44 |
Total Lines | 495 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like UnionService 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 UnionService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class UnionService extends Service |
||
33 | { |
||
34 | /** |
||
35 | * 接口地址 |
||
36 | * @var string |
||
37 | */ |
||
38 | private $url = "https://router.jd.com/api"; |
||
39 | |||
40 | /** |
||
41 | * API接口名称 |
||
42 | * @var |
||
43 | */ |
||
44 | private $method = '', $response = ''; |
||
45 | |||
46 | /** |
||
47 | * 联盟分配给应用的appkey |
||
48 | * @var |
||
49 | */ |
||
50 | private $app_key = ''; |
||
51 | |||
52 | /** |
||
53 | * 联盟分配给应用的secretkey |
||
54 | * @var |
||
55 | */ |
||
56 | private $secret_key = ''; |
||
57 | |||
58 | /** |
||
59 | * 根据API属性标签,如果需要授权,则此参数必传;如果不需要授权,则此参数不需要传 |
||
60 | * @var |
||
61 | */ |
||
62 | private $access_token = ''; |
||
63 | |||
64 | /** |
||
65 | * 响应格式,暂时只支持json |
||
66 | * @var string |
||
67 | */ |
||
68 | private $format = "json"; |
||
69 | |||
70 | /** |
||
71 | * API协议版本,请根据API具体版本号传入此参数,一般为1.0 |
||
72 | * @var string |
||
73 | */ |
||
74 | private $v = "1.0"; |
||
75 | |||
76 | /** |
||
77 | * 签名的摘要算法,暂时只支持md5 |
||
78 | * @var string |
||
79 | */ |
||
80 | private $sign_method = "md5"; |
||
81 | |||
82 | /** |
||
83 | * 响应内容 |
||
84 | * @var |
||
85 | */ |
||
86 | private $output; |
||
87 | |||
88 | /** |
||
89 | * 需要发送的的参数 |
||
90 | * @var |
||
91 | */ |
||
92 | private $param, $params; |
||
93 | |||
94 | /** |
||
95 | * 联盟分配给应用的appkey |
||
96 | * @param string $appKey |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function appKey(string $appKey): self |
||
100 | { |
||
101 | $this->app_key = $appKey; |
||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * 联盟分配给应用的secretkey |
||
107 | * @param string $secretKey |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function secretKey(string $secretKey): self |
||
111 | { |
||
112 | $this->secret_key = $secretKey; |
||
113 | return $this; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * 根据API属性标签,如果需要授权,则此参数必传;如果不需要授权,则此参数不需要传 |
||
118 | * @param string $accessToken |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function accessToken(string $accessToken): self |
||
122 | { |
||
123 | $this->access_token = $accessToken; |
||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * 组参 |
||
129 | * @param array $param |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function param(array $param): self |
||
133 | { |
||
134 | $this->param = $param; |
||
135 | return $this; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * 网络请求 |
||
140 | * @throws DtaException |
||
141 | */ |
||
142 | private function http(): void |
||
143 | { |
||
144 | //生成签名 |
||
145 | $sign = $this->createSign(); |
||
146 | //组织参数 |
||
147 | $strParam = $this->createStrParam(); |
||
148 | $strParam .= 'sign=' . $sign; |
||
149 | //访问服务 |
||
150 | $result = file_get_contents("{$this->url}?{$strParam}"); |
||
151 | $result = json_decode($result, true); |
||
152 | $this->output = $result; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * 获取配置信息 |
||
157 | * @return $this |
||
158 | */ |
||
159 | private function getConfig(): self |
||
160 | { |
||
161 | $this->app_key = config('dtapp.jd.union.app_key'); |
||
162 | $this->secret_key = config('dtapp.jd.union.secret_key'); |
||
163 | return $this; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * 网站/APP获取推广链接接口 |
||
168 | * https://union.jd.com/openplatform/api/10421 |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function promotionCommonGet(): self |
||
172 | { |
||
173 | $this->method = 'jd.union.open.promotion.common.get'; |
||
174 | return $this; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * 社交媒体获取推广链接接口【申请】 |
||
179 | * https://union.jd.com/openplatform/api/10424 |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function promotionBySubUnionIdGet(): self |
||
183 | { |
||
184 | $this->method = 'jd.union.open.promotion.bysubunionid.get'; |
||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * 工具商获取推广链接接口【申请】 |
||
190 | * https://union.jd.com/openplatform/api/10425 |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function promotionByUnionIdGet(): self |
||
194 | { |
||
195 | $this->method = 'jd.union.open.promotion.byunionid.get'; |
||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * 订单行查询接口 |
||
201 | * https://union.jd.com/openplatform/api/12707 |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function orderRowQuery(): self |
||
205 | { |
||
206 | $this->method = 'jd.union.open.order.row.query'; |
||
207 | return $this; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * 奖励订单查询接口【申请】 |
||
212 | * https://union.jd.com/openplatform/api/11781 |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function orderBonusQuery(): self |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * 创建推广位【申请】 |
||
223 | * https://union.jd.com/openplatform/api/10429 |
||
224 | * @return $this |
||
225 | */ |
||
226 | public function positionCreate(): self |
||
227 | { |
||
228 | $this->method = 'jd.union.open.position.create'; |
||
229 | return $this; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * 查询推广位【申请】 |
||
234 | * https://union.jd.com/openplatform/api/10428 |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function positionQuery(): self |
||
238 | { |
||
239 | $this->method = 'jd.union.open.position.query'; |
||
240 | return $this; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * 获取PID【申请】 |
||
245 | * https://union.jd.com/openplatform/api/10430 |
||
246 | * @return $this |
||
247 | */ |
||
248 | public function userPidGet(): self |
||
249 | { |
||
250 | $this->method = 'jd.union.open.user.pid.get'; |
||
251 | return $this; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * 关键词商品查询接口【申请】 |
||
256 | * https://union.jd.com/openplatform/api/10421 |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function goodsQuery(): self |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * 京粉精选商品查询接口 |
||
267 | * https://union.jd.com/openplatform/api/10417 |
||
268 | * @return $this |
||
269 | */ |
||
270 | public function goodsJIngFenQuery(): self |
||
271 | { |
||
272 | if (!isset($this->param['pageIndex'])) { |
||
273 | $this->param['pageIndex'] = 1; |
||
274 | } |
||
275 | if (!isset($this->param['pageSize'])) { |
||
276 | $this->param['pageSize'] = 20; |
||
277 | } |
||
278 | $this->method = 'jd.union.open.goods.jingfen.query'; |
||
279 | return $this; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * 根据skuid查询商品信息接口 |
||
284 | * https://union.jd.com/openplatform/api/10422 |
||
285 | * @return $this |
||
286 | */ |
||
287 | public function goodsPromotionGoodsInfoQuery(): self |
||
288 | { |
||
289 | $this->method = 'jd.union.open.goods.promotiongoodsinfo.query'; |
||
290 | return $this; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * 礼金创建【申请】 |
||
295 | * https://union.jd.com/openplatform/api/12246 |
||
296 | * @return $this |
||
297 | */ |
||
298 | public function couponGiftGet(): self |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * 礼金停止【申请】 |
||
306 | * https://union.jd.com/openplatform/api/12240 |
||
307 | * @return $this |
||
308 | */ |
||
309 | public function couponGiftStop(): self |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * 礼金效果数据 |
||
317 | * https://union.jd.com/openplatform/api/12248 |
||
318 | * @return $this |
||
319 | */ |
||
320 | public function statisticsGifTCouponQuery(): self |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * 自定义接口 |
||
328 | * @param string $method |
||
329 | * @return $this |
||
330 | */ |
||
331 | public function setMethod($method = ''): self |
||
332 | { |
||
333 | $this->method = $method; |
||
334 | return $this; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * 返回数组数据 |
||
339 | * @return array|mixed |
||
340 | * @throws DtaException |
||
341 | */ |
||
342 | public function toArray() |
||
343 | { |
||
344 | //首先检测是否支持curl |
||
345 | if (!extension_loaded("curl")) { |
||
346 | throw new HttpException(404, '请开启curl模块!'); |
||
347 | } |
||
348 | if (empty($this->app_key)) { |
||
349 | $this->getConfig(); |
||
350 | } |
||
351 | if (empty($this->app_key)) { |
||
352 | throw new DtaException('请检查app_key参数'); |
||
353 | } |
||
354 | if (empty($this->method)) { |
||
355 | throw new DtaException('请检查method参数'); |
||
356 | } |
||
357 | $this->params['method'] = $this->method; |
||
358 | $this->params['app_key'] = $this->app_key; |
||
359 | $this->params['timestamp'] = date('Y-m-d H:i:s'); |
||
360 | $this->params['format'] = $this->format; |
||
361 | $this->params['v'] = $this->v; |
||
362 | $this->params['sign_method'] = $this->sign_method; |
||
363 | $this->params['param_json'] = json_encode($this->param, JSON_UNESCAPED_UNICODE); |
||
364 | $this->http(); |
||
365 | $response = Strings::replace('.', '_', $this->method) . "_response"; |
||
366 | if (isset($this->output[$response]['result'])) { |
||
367 | if (is_array($this->output[$response]['result'])) { |
||
368 | return $this->output[$response]['result']; |
||
369 | } |
||
370 | if (is_object($this->output[$response]['result'])) { |
||
371 | $this->output = json_encode($this->output[$response]['result'], JSON_UNESCAPED_UNICODE); |
||
372 | } |
||
373 | return json_decode($this->output[$response]['result'], true); |
||
374 | } |
||
375 | |||
376 | if (is_array($this->output)) { |
||
377 | return $this->output; |
||
378 | } |
||
379 | if (is_object($this->output)) { |
||
380 | $this->output = json_encode($this->output, JSON_UNESCAPED_UNICODE); |
||
381 | } |
||
382 | return json_decode($this->output, true); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * 签名 |
||
387 | * @return string |
||
388 | * @throws DtaException |
||
389 | */ |
||
390 | private function createSign(): string |
||
391 | { |
||
392 | if (empty($this->secret_key)) { |
||
393 | $this->getConfig(); |
||
394 | } |
||
395 | if (empty($this->secret_key)) { |
||
396 | throw new DtaException('请检查secret_key参数'); |
||
397 | } |
||
398 | $sign = $this->secret_key; |
||
399 | ksort($this->params); |
||
400 | foreach ($this->params as $key => $val) { |
||
401 | if ($key !== '' && $val !== '') { |
||
402 | $sign .= $key . $val; |
||
403 | } |
||
404 | } |
||
405 | $sign .= $this->secret_key; |
||
406 | $sign = strtoupper(md5($sign)); |
||
407 | return $sign; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * 组参 |
||
412 | * @return string |
||
413 | */ |
||
414 | private function createStrParam(): string |
||
415 | { |
||
416 | $strParam = ''; |
||
417 | foreach ($this->params as $key => $val) { |
||
418 | if ($key !== '' && $val !== '') { |
||
419 | $strParam .= $key . '=' . urlencode($val) . '&'; |
||
420 | } |
||
421 | } |
||
422 | return $strParam; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * 获取频道ID |
||
427 | * @return array[] |
||
428 | */ |
||
429 | public function getEliteIdList(): array |
||
527 | ], |
||
528 | ] |
||
529 | ], |
||
530 | ]; |
||
531 | } |
||
533 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths