Decryption::decryptionNoticeXML()   C
last analyzed

Complexity

Conditions 11
Paths 5

Size

Total Lines 43
Code Lines 23

Duplication

Lines 5
Ratio 11.63 %

Importance

Changes 0
Metric Value
cc 11
eloc 23
nc 5
nop 0
dl 5
loc 43
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
namespace OpenOauth;
3
4
use OpenOauth\Core\Core;
5
use OpenOauth\Core\Tools;
6
use OpenOauth\Core\WechatCode\WXBizMsgCrypt;
7
8
class Decryption extends Core
9
{
10
    public function decryptionNoticeXML()
0 ignored issues
show
Coding Style introduced by
decryptionNoticeXML uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
11
    {
12
        //接收微信的xml 消息
13
        $xml = file_get_contents("php://input");
14
15
        $msg_signature = $_GET['msg_signature'];
16
        $timestamp     = $_GET['timestamp'];
17
        $nonce         = $_GET['nonce'];
18
19
        if (!is_string($xml) || $xml == '') {
20
            $this->setError('xml参数错误');
21
22
            return false;
23
        }
24
25 View Code Duplication
        if (!is_string($msg_signature) || $xml == '' || !is_numeric($timestamp) || $timestamp <= 0 || !is_string($nonce) || $nonce == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
            $this->setError('签名参数错误');
27
28
            return false;
29
        }
30
31
        $verify_signature = Tools::verifySignature($this->configs->component_app_token);
32
33
        if (!$verify_signature) {
34
            $this->setError('签名验证失败');
35
36
            return false;
37
        }
38
39
        //解密XML
40
        $msgCrypt = new WXBizMsgCrypt($this->configs->component_app_token, $this->configs->component_app_key, $this->configs->component_app_id);
41
        $xmlInfo  = '';
42
43
        $errCode = $msgCrypt->decryptMsg($msg_signature, $timestamp, $nonce, $xml, $xmlInfo);
44
45
        if ($errCode == 0) {
46
            $xml_array = json_decode(json_encode(simplexml_load_string($xmlInfo, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
47
48
            return $xml_array;
49
        } else {
50
            return false;
51
        }
52
    }
53
}