HashDataSignature::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
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
    /**
23
     * @var string tên loại mã hóa. Ví dụ: md5, sha1, sha256...
24
     */
25
    public $hashAlgo;
26
27
    /**
28
     * @var bool phân biệt ký tự hoa thường khi xác minh chữ ký.
29
     * @since 1.0.3
30
     */
31
    public $caseSensitive = true;
32
33
    /**
34
     * @inheritdoc
35
     * @throws InvalidConfigException
36
     */
37 10
    public function init()
38
    {
39 10
        if ($this->hashAlgo === null) {
40
            throw new InvalidConfigException('Property `hashAlgo` must be set!');
41
        }
42
43 10
        parent::init();
44 10
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 10
    public function generate(): string
50
    {
51 10
        return hash($this->hashAlgo, $this->getData());
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 5 View Code Duplication
    public function validate(string $expect): bool
0 ignored issues
show
Duplication introduced by
This method 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...
58
    {
59 5
        $actual = $this->generate();
60
61 5
        if ($this->caseSensitive) {
62 5
            return strcmp($expect, $actual) === 0;
63
        } else {
64
            return strcasecmp($expect, $actual) === 0;
65
        }
66
    }
67
68
}
69