Completed
Push — master ( 4b202c...dc308f )
by Vuong
02:05
created

HashDataSignature   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 35
ccs 6
cts 9
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A generate() 0 4 1
A validate() 0 4 1
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
9
namespace yiiviet\payment;
10
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * Lớp HashDataSignature hổ trợ tạo chữ ký dữ liệu thông qua các kiểu mã hóa hash 1 chiều như md5, sha1, sha256...
15
 *
16
 * @author Vuong Minh <[email protected]>
17
 * @since 1.0.2
18
 */
19
class HashDataSignature extends DataSignature
20
{
21
22
    public $hashAlgo;
23
24
    /**
25
     * @inheritdoc
26
     * @throws InvalidConfigException
27
     */
28 3
    public function init()
29
    {
30 3
        if ($this->hashAlgo === null) {
31
            throw new InvalidConfigException('Property `hashAlgo` must be set!');
32
        }
33
34 3
        parent::init();
35 3
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40 3
    public function generate(): string
41
    {
42 3
        return hash($this->hashAlgo, $this->getData());
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function validate(string $expect): bool
49
    {
50
        return strcmp($expect, $this->generate()) === 0;
51
    }
52
53
}
54