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