Completed
Push — master ( 4f7c7c...fdaf8f )
by Vuong
02:10
created

SignatureValidatorTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 34
ccs 8
cts 11
cp 0.7272
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A signatureValidator() 0 13 2
A getDataSignAttributes() 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
namespace yiiviet\payment\momo;
9
10
use yii\base\InvalidCallException;
11
12
/**
13
 * Trait SignatureValidatorTrait cung cấp phương thức xác minh chữ ký dữ liệu từ MOMO.
14
 *
15
 * @author Vuong Minh <[email protected]>
16
 * @since 1.0.3
17
 */
18
trait SignatureValidatorTrait
19
{
20
    /**
21
     * Phương thức kiểm tra chữ ký dữ liệu có hợp lệ hay không từ MOMO gửi sang.
22
     *
23
     * @param string $attribute có giá trị là chữ ký cần kiểm tra.
24
     * @param array $params thiết lập từ rule
25
     * @param \yii\validators\InlineValidator $validator
26
     * @throws \yii\base\InvalidConfigException|\yii\base\NotSupportedException|InvalidCallException
27
     */
28 4
    public function signatureValidator($attribute, $params, \yii\validators\InlineValidator $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
    {
30 4
        $dataSignAttributes = $this->getDataSignAttributes();
31 4
        $data = array_merge(array_fill_keys($dataSignAttributes, ''), $this->get(false));
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32 4
        $dataSign = array_intersect_key($data, array_flip($dataSignAttributes));
33 4
        $dataSign = urldecode(http_build_query($dataSign));
34
35 4
        $client = $this->getClient();
0 ignored issues
show
Bug introduced by
It seems like getClient() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
37 4
        if (!$client->validateSignature($dataSign, $this->$attribute ?? '')) {
38
            $validator->addError($this, $attribute, $validator->message);
39
        }
40 4
    }
41
42
    /**
43
     * Phương thức hồ trợ cung cấp các attributes dùng để xác minh tính hợp lệ của chữ ký dữ liệu phản hồi từ MOMO.
44
     *
45
     * @return array attributes dùng để xác minh tính hợp lệ.
46
     */
47
    protected function getDataSignAttributes(): array
48
    {
49
        throw new InvalidCallException(__METHOD__ . ' must be override by class used it!');
50
    }
51
}
52