Total Complexity | 100 |
Total Lines | 532 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like SecurityClient 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 SecurityClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class SecurityClient |
||
10 | { |
||
11 | private $topClient ; |
||
12 | private $randomNum ; |
||
13 | private $securityUtil; |
||
14 | private $cacheClient = null; |
||
15 | |||
16 | function __construct($client, $random) |
||
17 | { |
||
18 | |||
19 | define('APP_SECRET_TYPE','2'); |
||
20 | define('APP_USER_SECRET_TYPE','3'); |
||
21 | |||
22 | $this->topClient = $client; |
||
23 | $this->randomNum = $random; |
||
24 | $this->securityUtil = new SecurityUtil(); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * 设置缓存处理器 |
||
29 | */ |
||
30 | function setCacheClient($cache) |
||
|
|||
31 | { |
||
32 | $this->cacheClient = $cache; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * 密文检索,在秘钥升级场景下兼容查询 |
||
37 | * |
||
38 | * @see #search(String, String, String, Long) |
||
39 | * @return |
||
40 | */ |
||
41 | function searchPrevious($data,$type,$session = null) |
||
42 | { |
||
43 | return $this->searchInner($data,$type,$session,-1); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * 密文检索(每个用户单独分配秘钥) |
||
48 | * |
||
49 | * @see #search(String, String, String, Long) |
||
50 | * @return |
||
51 | */ |
||
52 | function search($data,$type,$session = null) |
||
53 | { |
||
54 | return $this->searchInner($data,$type,$session,null); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * 密文检索。 手机号码格式:$base64(H-MAC(phone后4位))$ simple格式:base64(H-MAC(滑窗)) |
||
59 | * |
||
60 | * @param data |
||
61 | * 明文数据 |
||
62 | * @param type |
||
63 | * 加密字段类型(例如:simple\phone) |
||
64 | * @param session |
||
65 | * 用户身份,用户级加密必填 |
||
66 | * @param version |
||
67 | * 秘钥历史版本 |
||
68 | * @return |
||
69 | */ |
||
70 | function searchInner($data, $type, $session,$version) |
||
71 | { |
||
72 | if(empty($data) || empty($type)){ |
||
73 | return $data; |
||
74 | } |
||
75 | |||
76 | $secretContext = null; |
||
77 | |||
78 | $secretContext = $this->callSecretApiWithCache($session,$version); |
||
79 | $this->incrCounter(3,$type,$secretContext,true); |
||
80 | |||
81 | if(empty($secretContext) || empty($secretContext->secret)) { |
||
82 | return $data; |
||
83 | } |
||
84 | |||
85 | return $this->securityUtil->search($data, $type,$secretContext); |
||
86 | } |
||
87 | |||
88 | |||
89 | /** |
||
90 | * 单条数据解密,使用appkey级别公钥 |
||
91 | * 非加密数据直接返回原文 |
||
92 | */ |
||
93 | function decryptPublic($data,$type) |
||
94 | { |
||
95 | return $this->decrypt($data,$type,null); |
||
96 | } |
||
97 | /** |
||
98 | * 单条数据解密 |
||
99 | * 非加密数据直接返回原文 |
||
100 | */ |
||
101 | function decrypt($data,$type,$session) |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * 多条数据解密,使用appkey级别公钥 |
||
123 | * 非加密数据直接返回原文 |
||
124 | */ |
||
125 | function decryptBatchPublic($array,$type) |
||
126 | { |
||
127 | if(empty($array) || empty($type)){ |
||
128 | return null; |
||
129 | } |
||
130 | |||
131 | $result = array(); |
||
132 | foreach ($array as $value) { |
||
133 | $secretData = $this->securityUtil->getSecretDataByType($value,$type); |
||
134 | $secretContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
||
135 | |||
136 | if(empty($secretData)){ |
||
137 | $result[$value] = $value; |
||
138 | }else{ |
||
139 | $result[$value] = $this->securityUtil->decrypt($value,$type,$secretContext); |
||
140 | $this->incrCounter(2,$type,$secretContext,true); |
||
141 | } |
||
142 | |||
143 | $this->flushCounter($secretContext); |
||
144 | } |
||
145 | |||
146 | return $result; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * 多条数据解密,必须是同一个type和用户,返回结果是 KV结果 |
||
151 | * 非加密数据直接返回原文 |
||
152 | */ |
||
153 | function decryptBatch($array,$type,$session) |
||
154 | { |
||
155 | if(empty($array) || empty($type)){ |
||
156 | return null; |
||
157 | } |
||
158 | |||
159 | $result = array(); |
||
160 | foreach ($array as $value) { |
||
161 | $secretData = $this->securityUtil->getSecretDataByType($value,$type); |
||
162 | if(empty($secretData)){ |
||
163 | $result[$value] = $value; |
||
164 | } else if($this->securityUtil->isPublicData($value,$type)){ |
||
165 | $appContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
||
166 | $result[$value] = $this->securityUtil->decrypt($value,$type,$appContext); |
||
167 | $this->incrCounter(2,$type,$appContext,false); |
||
168 | $this->flushCounter($appContext); |
||
169 | } else { |
||
170 | $secretContext = $this->callSecretApiWithCache($session,$secretData->secretVersion); |
||
171 | $result[$value] = $this->securityUtil->decrypt($value,$type,$secretContext); |
||
172 | $this->incrCounter(2,$type,$secretContext,false); |
||
173 | $this->flushCounter($secretContext); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | return $result; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * 使用上一版本秘钥解密,app级别公钥 |
||
182 | */ |
||
183 | function decryptPreviousPublic($data,$type) |
||
184 | { |
||
185 | $secretContext = $this->callSecretApiWithCache(null,-1); |
||
186 | return $this->securityUtil->decrypt($data,$type,$secretContext); |
||
187 | } |
||
188 | /** |
||
189 | * 使用上一版本秘钥解密,一般只用于更新秘钥 |
||
190 | */ |
||
191 | function decryptPrevious($data,$type,$session) |
||
192 | { |
||
193 | if($this->securityUtil->isPublicData($data,$type)){ |
||
194 | $secretContext = $this->callSecretApiWithCache(null,-1); |
||
195 | } else { |
||
196 | $secretContext = $this->callSecretApiWithCache($session,-1); |
||
197 | } |
||
198 | return $this->securityUtil->decrypt($data,$type,$secretContext); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * 加密单条数据,使用app级别公钥 |
||
203 | */ |
||
204 | function encryptPublic($data,$type,$version = null) |
||
205 | { |
||
206 | return $this->encrypt($data,$type,null,$version); |
||
207 | } |
||
208 | /** |
||
209 | * 加密单条数据 |
||
210 | */ |
||
211 | function encrypt($data,$type,$session = null,$version = null) |
||
212 | { |
||
213 | if(empty($data) || empty($type)){ |
||
214 | return null; |
||
215 | } |
||
216 | $secretContext = $this->callSecretApiWithCache($session,null); |
||
217 | $this->incrCounter(1,$type,$secretContext,true); |
||
218 | |||
219 | return $this->securityUtil->encrypt($data,$type,$version,$secretContext); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * 加密多条数据,使用app级别公钥 |
||
224 | */ |
||
225 | function encryptBatchPublic($array,$type,$version = null) |
||
226 | { |
||
227 | if(empty($array) || empty($type)){ |
||
228 | return null; |
||
229 | } |
||
230 | $secretContext = $this->callSecretApiWithCache(null,null); |
||
231 | $result = array(); |
||
232 | foreach ($array as $value) { |
||
233 | $result[$value] = $this->securityUtil->encrypt($value,$type,$version,$secretContext); |
||
234 | $this->incrCounter(1,$type,$secretContext,false); |
||
235 | } |
||
236 | $this->flushCounter($secretContext); |
||
237 | |||
238 | return $result; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * 加密多条数据,必须是同一个type和用户,返回结果是 KV结果 |
||
243 | */ |
||
244 | function encryptBatch($array,$type,$session,$version = null) |
||
245 | { |
||
246 | if(empty($array) || empty($type)){ |
||
247 | return null; |
||
248 | } |
||
249 | $secretContext = $this->callSecretApiWithCache($session,null); |
||
250 | $result = array(); |
||
251 | foreach ($array as $value) { |
||
252 | $result[$value] = $this->securityUtil->encrypt($value,$type,$version,$secretContext); |
||
253 | $this->incrCounter(1,$type,$secretContext,false); |
||
254 | } |
||
255 | $this->flushCounter($secretContext); |
||
256 | return $result; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * 使用上一版本秘钥加密,使用app级别公钥 |
||
261 | */ |
||
262 | function encryptPreviousPublic($data,$type) |
||
263 | { |
||
264 | $secretContext = $this->callSecretApiWithCache(null,-1); |
||
265 | $this->incrCounter(1,$type,$secretContext,true); |
||
266 | |||
267 | return $this->securityUtil->encrypt($data,$type,$secretContext->version,$secretContext); |
||
268 | } |
||
269 | /** |
||
270 | * 使用上一版本秘钥加密,一般只用于更新秘钥 |
||
271 | */ |
||
272 | function encryptPrevious($data,$type,$session) |
||
273 | { |
||
274 | $secretContext = $this->callSecretApiWithCache($session,-1); |
||
275 | $this->incrCounter(1,$type,$secretContext,true); |
||
276 | |||
277 | return $this->securityUtil->encrypt($data,$type,$secretContext); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * 根据session生成秘钥 |
||
282 | */ |
||
283 | function initSecret($session) |
||
284 | { |
||
285 | return $this->callSecretApiWithCache($session,null); |
||
286 | } |
||
287 | |||
288 | function buildCacheKey($session,$secretVersion) |
||
289 | { |
||
290 | if(empty($session)){ |
||
291 | return $this->topClient->getAppkey(); |
||
292 | } |
||
293 | if(empty($secretVersion)){ |
||
294 | return $session ; |
||
295 | } |
||
296 | return $session.'_'.$secretVersion ; |
||
297 | } |
||
298 | |||
299 | |||
300 | function generateCustomerSession($userId) |
||
301 | { |
||
302 | return '_'.$userId ; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * 判断是否是已加密的数据 |
||
307 | */ |
||
308 | function isEncryptData($data,$type) |
||
309 | { |
||
310 | if(empty($data) || empty($type)){ |
||
311 | return false; |
||
312 | } |
||
313 | return $this->securityUtil->isEncryptData($data,$type); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * 判断是否是已加密的数据,数据必须是同一个类型 |
||
318 | */ |
||
319 | function isEncryptDataArray($array,$type) |
||
320 | { |
||
321 | if(empty($array) || empty($type)){ |
||
322 | return false; |
||
323 | } |
||
324 | return $this->securityUtil->isEncryptDataArray($array,$type); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * 判断数组中的数据是否存在密文,存在任何一个返回true,否则false |
||
329 | */ |
||
330 | function isPartEncryptData($array,$type) |
||
331 | { |
||
332 | if(empty($array) || empty($type)){ |
||
333 | return false; |
||
334 | } |
||
335 | return $this->securityUtil->isPartEncryptData($array,$type); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * 获取秘钥,使用缓存 |
||
340 | */ |
||
341 | function callSecretApiWithCache($session,$secretVersion) |
||
342 | { |
||
343 | if($this->cacheClient) |
||
344 | { |
||
345 | $time = time(); |
||
346 | $cacheKey = $this->buildCacheKey($session,$secretVersion); |
||
347 | $secretContext = $this->cacheClient->getCache($cacheKey); |
||
348 | |||
349 | if($secretContext) |
||
350 | { |
||
351 | if($this->canUpload($secretContext)){ |
||
352 | if($this->report($secretContext)){ |
||
353 | $this->clearReport($secretContext); |
||
354 | } |
||
355 | } |
||
356 | } |
||
357 | |||
358 | if($secretContext && $secretContext->invalidTime > $time) |
||
359 | { |
||
360 | return $secretContext; |
||
361 | } |
||
362 | } |
||
363 | |||
364 | $secretContext = $this->callSecretApi($session,$secretVersion); |
||
365 | |||
366 | if($this->cacheClient) |
||
367 | { |
||
368 | $secretContext->cacheKey = $cacheKey; |
||
369 | $this->cacheClient->setCache($cacheKey,$secretContext); |
||
370 | } |
||
371 | |||
372 | return $secretContext; |
||
373 | } |
||
374 | |||
375 | function incrCounter($op,$type,$secretContext,$flush) |
||
376 | { |
||
377 | if($op == 1){ |
||
378 | switch ($type) { |
||
379 | case 'nick': |
||
380 | $secretContext->encryptNickNum ++ ; |
||
381 | break; |
||
382 | case 'simple': |
||
383 | $secretContext->encryptSimpleNum ++ ; |
||
384 | break; |
||
385 | case 'receiver_name': |
||
386 | $secretContext->encryptReceiverNameNum ++ ; |
||
387 | break; |
||
388 | case 'phone': |
||
389 | $secretContext->encryptPhoneNum ++ ; |
||
390 | break; |
||
391 | default: |
||
392 | break; |
||
393 | } |
||
394 | }else if($op == 2){ |
||
395 | switch ($type) { |
||
396 | case 'nick': |
||
397 | $secretContext->decryptNickNum ++ ; |
||
398 | break; |
||
399 | case 'simple': |
||
400 | $secretContext->decryptSimpleNum ++ ; |
||
401 | break; |
||
402 | case 'receiver_name': |
||
403 | $secretContext->decryptReceiverNameNum ++ ; |
||
404 | break; |
||
405 | case 'phone': |
||
406 | $secretContext->decryptPhoneNum ++ ; |
||
407 | break; |
||
408 | default: |
||
409 | break; |
||
410 | } |
||
411 | }else{ |
||
412 | switch ($type) { |
||
413 | case 'nick': |
||
414 | $secretContext->searchNickNum ++ ; |
||
415 | break; |
||
416 | case 'simple': |
||
417 | $secretContext->searchSimpleNum ++ ; |
||
418 | break; |
||
419 | case 'receiver_name': |
||
420 | $secretContext->searchReceiverNameNum ++ ; |
||
421 | break; |
||
422 | case 'phone': |
||
423 | $secretContext->searchPhoneNum ++ ; |
||
424 | break; |
||
425 | default: |
||
426 | break; |
||
427 | } |
||
428 | } |
||
429 | |||
430 | if($flush && $this->cacheClient){ |
||
431 | $this->cacheClient->setCache($secretContext->cacheKey,$secretContext); |
||
432 | } |
||
433 | } |
||
434 | |||
435 | function flushCounter($secretContext) |
||
436 | { |
||
437 | if($this->cacheClient){ |
||
438 | $this->cacheClient->setCache($secretContext->cacheKey,$secretContext); |
||
439 | } |
||
440 | } |
||
441 | |||
442 | function clearReport($secretContext) |
||
443 | { |
||
444 | $secretContext->encryptPhoneNum = 0; |
||
445 | $secretContext->encryptNickNum = 0; |
||
446 | $secretContext->encryptReceiverNameNum = 0; |
||
447 | $secretContext->encryptSimpleNum = 0; |
||
448 | $secretContext->encryptSearchNum = 0; |
||
449 | $secretContext->decryptPhoneNum = 0; |
||
450 | $secretContext->decryptNickNum = 0; |
||
451 | $secretContext->decryptReceiverNameNum = 0; |
||
452 | $secretContext->decryptSimpleNum = 0; |
||
453 | $secretContext->decryptSearchNum = 0; |
||
454 | $secretContext->searchPhoneNum = 0; |
||
455 | $secretContext->searchNickNum = 0; |
||
456 | $secretContext->searchReceiverNameNum = 0; |
||
457 | $secretContext->searchSimpleNum = 0; |
||
458 | $secretContext->searchSearchNum = 0; |
||
459 | $secretContext->lastUploadTime = time(); |
||
460 | } |
||
461 | |||
462 | function canUpload($secretContext) |
||
463 | { |
||
464 | $current = time(); |
||
465 | if($current - $secretContext->lastUploadTime > 300){ |
||
466 | return true; |
||
467 | } |
||
468 | return false; |
||
469 | } |
||
470 | |||
471 | /* |
||
472 | * 上报信息 |
||
473 | */ |
||
474 | function report($secretContext) |
||
475 | { |
||
476 | $request = new TopSdkFeedbackUploadRequest; |
||
477 | $request->setContent($secretContext->toLogString()); |
||
478 | |||
479 | if(empty($secretContext->session)){ |
||
480 | $request->setType(APP_SECRET_TYPE); |
||
481 | }else{ |
||
482 | $request->setType(APP_USER_SECRET_TYPE); |
||
483 | } |
||
484 | |||
485 | $response = $this->topClient->execute($request,$secretContext->session); |
||
486 | if($response->code == 0){ |
||
487 | return true; |
||
488 | } |
||
489 | return false; |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * 获取秘钥,不使用缓存 |
||
494 | */ |
||
495 | function callSecretApi($session,$secretVersion) |
||
541 | } |
||
542 | } |
||
543 | ?> |
||
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.