| Conditions | 8 |
| Paths | 24 |
| Total Lines | 54 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | public function handle() |
||
| 20 | { |
||
| 21 | // oss 前面header、公钥 header |
||
| 22 | $authorizationBase64 = ''; |
||
| 23 | $pubKeyUrlBase64 = ''; |
||
| 24 | |||
| 25 | if (isset($_SERVER['HTTP_AUTHORIZATION'])) { |
||
| 26 | $authorizationBase64 = $_SERVER['HTTP_AUTHORIZATION']; |
||
| 27 | } |
||
| 28 | |||
| 29 | if (isset($_SERVER['HTTP_X_OSS_PUB_KEY_URL'])) { |
||
| 30 | $pubKeyUrlBase64 = $_SERVER['HTTP_X_OSS_PUB_KEY_URL']; |
||
| 31 | } |
||
| 32 | |||
| 33 | // 验证失败 |
||
| 34 | if ('' == $authorizationBase64 || '' == $pubKeyUrlBase64) { |
||
| 35 | return [false, ['CallbackFailed' => 'authorization or pubKeyUrl is null']]; |
||
| 36 | } |
||
| 37 | |||
| 38 | // 获取OSS的签名 |
||
| 39 | $authorization = base64_decode($authorizationBase64); |
||
| 40 | // 获取公钥 |
||
| 41 | $pubKeyUrl = base64_decode($pubKeyUrlBase64); |
||
| 42 | // 请求验证 |
||
| 43 | $ch = curl_init(); |
||
| 44 | curl_setopt($ch, CURLOPT_URL, $pubKeyUrl); |
||
|
|
|||
| 45 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||
| 46 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); |
||
| 47 | $pubKey = curl_exec($ch); |
||
| 48 | |||
| 49 | if ('' == $pubKey) { |
||
| 50 | return [false, ['CallbackFailed' => 'curl is fail']]; |
||
| 51 | } |
||
| 52 | |||
| 53 | // 获取回调 body |
||
| 54 | $body = file_get_contents('php://input'); |
||
| 55 | // 拼接待签名字符串 |
||
| 56 | $path = $_SERVER['REQUEST_URI']; |
||
| 57 | $pos = strpos($path, '?'); |
||
| 58 | if (false === $pos) { |
||
| 59 | $authStr = urldecode($path) . "\n" . $body; |
||
| 60 | } else { |
||
| 61 | $authStr = urldecode(substr($path, 0, $pos)) . substr($path, $pos, strlen($path) - $pos) . "\n" . $body; |
||
| 62 | } |
||
| 63 | // 验证签名 |
||
| 64 | $ok = openssl_verify($authStr, $authorization, $pubKey, OPENSSL_ALGO_MD5); |
||
| 65 | |||
| 66 | if (1 !== $ok) { |
||
| 67 | return [false, ['CallbackFailed' => 'verify is fail, Illegal data']]; |
||
| 68 | } |
||
| 69 | |||
| 70 | parse_str($body, $data); |
||
| 71 | |||
| 72 | return [true, $data]; |
||
| 73 | } |
||
| 76 |