Completed
Push — master ( eccc2c...9c0ef3 )
by Vuong
01:43
created

HmacDataSignature::init()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 5
cts 7
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 3.2098
1
<?php
2
/**
3
 * @link https://github.com/yiiviet/yii2-payment
4
 * @copyright Copyright (c) 2017 Yii Viet
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace yiiviet\payment;
9
10
use yii\base\InvalidConfigException;
11
12
/**
13
 * Lớp HmacDataSignature dùng cho việc tạo và kiểm tra chữ ký theo chuẩn HMAC.
14
 *
15
 * @author Vuong Minh <[email protected]>
16
 * @since 1.0
17
 */
18 View Code Duplication
class HmacDataSignature extends DataSignature
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
19
{
20
21
    /**
22
     * Tên loại mã hóa. Ví dụ: md5, sha1, sha256...
23
     *
24
     * @var string
25
     */
26
    public $hmacAlgo;
27
28
    /**
29
     * Khóa mã hóa. Độ phức tạp càng cao thì dữ liệu càng được an toàn.
30
     *
31
     * @var string
32
     */
33
    public $key;
34
35
    /**
36
     * @inheritdoc
37
     * @throws InvalidConfigException
38
     */
39 5
    public function init()
40
    {
41 5
        if ($this->key === null) {
42
            throw new InvalidConfigException('Property `key` must be set for generate signature!');
43 5
        } elseif ($this->hmacAlgo === null) {
44
            throw new InvalidConfigException('Property `hmacAlgo` must be set for generate signature!');
45
        }
46
47 5
        parent::init(); // TODO: Change the autogenerated stub
48 5
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 5
    public function generate(): string
54
    {
55 5
        return hash_hmac($this->hmacAlgo, $this->getData(), $this->key);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     * @throws InvalidConfigException
61
     */
62 1
    public function validate(string $expect): bool
63
    {
64 1
        $actual = $this->generate();
65
66 1
        return strcasecmp($expect, $actual) === 0;
67
    }
68
69
}
70