Test Setup Failed
Push — master ( 63098b...38badb )
by Carlos
03:06
created

Encryptor::getAESKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * Encryptor.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    mingyoung <[email protected]>
21
 * @copyright 2016
22
 *
23
 * @see      https://github.com/overtrue
24
 * @see      http://overtrue.me
25
 */
26
27
namespace EasyWeChat\MiniProgram\Encryption;
28
29
use EasyWeChat\Core\Exceptions\InvalidConfigException;
30
use EasyWeChat\Encryption\EncryptionException;
31
use EasyWeChat\Encryption\Encryptor as BaseEncryptor;
32
use EasyWeChat\Support\Collection;
33
use Exception as BaseException;
34
35
class Encryptor extends BaseEncryptor
36
{
37
    /**
38
     * A non-NULL Initialization Vector.
39
     *
40
     * @var string
41
     */
42
    protected $iv;
43
44
    /**
45
     * Encryptor constructor.
46
     *
47
     * @param string $sessionKey
48
     * @param string $iv
49
     */
50
    public function __construct($sessionKey, $iv)
51
    {
52
        $this->iv = base64_decode($iv, true);
53
54
        parent::__construct(null, null, $sessionKey);
55
    }
56
57
    /**
58
     * Decrypt data.
59
     *
60
     * @param $encrypted
61
     *
62
     * @return \EasyWeChat\Support\Collection
63
     */
64
    public function decryptData($encrypted)
65
    {
66
        return new Collection(
67
            $this->decrypt($encrypted)
68
        );
69
    }
70
71
    /**
72
     * Decrypt data.
73
     *
74
     * @param string $encrypted
75
     *
76
     * @return array
77
     *
78
     * @throws EncryptionException
79
     */
80
    private function decrypt($encrypted)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
81
    {
82
        try {
83
            $key = $this->getAESKey();
84
            $ciphertext = base64_decode($encrypted, true);
85
            $decrypted = openssl_decrypt($ciphertext, 'aes-128-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $this->iv);
86
        } catch (BaseException $e) {
87
            throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_DECRYPT_AES);
88
        }
89
90
        $result = json_decode($this->decode($decrypted), true);
91
92
        if (is_null($result)) {
93
            throw new EncryptionException('ILLEGAL_BUFFER', EncryptionException::ILLEGAL_BUFFER);
94
        }
95
96
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (object|integer|double|string|array|boolean) is incompatible with the return type of the parent method EasyWeChat\Encryption\Encryptor::decrypt of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
97
    }
98
99
    /**
100
     * Return AESKey.
101
     *
102
     * @return string
103
     *
104
     * @throws InvalidConfigException
105
     */
106
    protected function getAESKey()
107
    {
108
        if (empty($this->AESKey)) {
109
            throw new InvalidConfigException("Configuration mission, 'aes_key' is required.");
110
        }
111
112
        return base64_decode($this->AESKey, true);
113
    }
114
}
115