DataSignature::generate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
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\vnpayment;
9
10
use yii\base\InvalidConfigException;
11
use yiiviet\payment\DataSignature as BaseDataSignature;
12
13
/**
14
 * Lớp DataSignature hổ trợ tạo và kiểm tra chữ ký dữ liệu khi tương tác với VnPayment.
15
 *
16
 * @property string $secureHash
17
 *
18
 * @author Vuong Minh <[email protected]>
19
 * @since 1.0
20
 * @deprecated since 1.0.2 we use `yiiviet\payment\HashDataSignature` instead.
21
 */
22
class DataSignature extends BaseDataSignature
23
{
24
25
    /**
26
     * @var string
27
     */
28
    public $hashSecret;
29
30
    /**
31
     * @var string
32
     */
33
    public $hashAlgo;
34
35
    /**
36
     * @inheritdoc
37
     * @throws InvalidConfigException
38
     */
39
    public function generate(): string
40
    {
41
        if ($this->hashSecret === null) {
42
            throw new InvalidConfigException('Property `hashSecret` must be set!');
43
        } elseif ($this->hashAlgo === null) {
44
            throw new InvalidConfigException('Property `hashAlgo` must be set!');
45
        }
46
47
        return hash($this->hashAlgo, $this->hashSecret . $this->getData());
48
    }
49
50
    /**
51
     * @throws InvalidConfigException
52
     * @inheritdoc
53
     */
54
    public function validate(string $expect): bool
55
    {
56
        $actual = $this->generate();
57
58
        return strcasecmp($expect, $actual) === 0;
59
    }
60
61
}
62