VerifiedData   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 21.15 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 11
loc 52
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 11 11 1
B validateSignature() 0 24 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\vtcpay;
10
11
use yii\helpers\ArrayHelper;
12
13
use yiiviet\payment\VerifiedData as BaseVerifiedData;
14
15
/**
16
 * Lớp VerifiedData tổng hợp dữ liệu đã được xác minh từ VTCPay.
17
 *
18
 * @method PaymentClient getClient() đối tượng client đã dùng để thực thi request.
19
 *
20
 * @property PaymentClient $client đối tượng client đã dùng để thực thi request.
21
 * @property double $amount số tiền đơn hàng.
22
 * @property string $message thông tin bổ sung.
23
 * @property string $payment_type hình thức thanh toán.
24
 * @property mixed $reference_number mã đơn hàng.
25
 * @property int $status trạng thái.
26
 * @property mixed $trans_ref_no mã giao dịch tại VTCPay.
27
 * @property string $website_id merchant id của client.
28
 *
29
 * @author Vuong Minh <[email protected]>
30
 * @since 1.0.2
31
 */
32
class VerifiedData extends BaseVerifiedData
33
{
34
35
    /**
36
     * @inheritdoc
37
     */
38 5 View Code Duplication
    public function rules()
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...
39
    {
40
        return [
41 5
            [['signature'], 'required', 'on' => [
42
                PaymentGateway::VRC_PURCHASE_SUCCESS, PaymentGateway::VRC_IPN
43
            ]],
44
            [['signature'], 'validateSignature', 'message' => '{attribute} is not valid!', 'on' => [
45
                PaymentGateway::VRC_PURCHASE_SUCCESS, PaymentGateway::VRC_IPN
46
            ]]
47
        ];
48
    }
49
50
    /**
51
     * Phương thức kiểm tra chữ ký dữ liệu có hợp lệ hay không từ VTCPay gửi sang.
52
     *
53
     * @param string $attribute Attribute có giá trị là chữ ký cần kiểm tra.
54
     * @param array $params Mảng tham trị thiết lập từ rule
55
     * @param \yii\validators\InlineValidator $validator
56
     * @throws \yii\base\InvalidConfigException|\yii\base\NotSupportedException
57
     */
58 5
    public function validateSignature($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...
59
    {
60 5
        $client = $this->getClient();
61 5
        $data = $this->get(false);
62 5
        $expectSignature = ArrayHelper::remove($data, $attribute, false);
63
64 5
        if ($this->command === PaymentGateway::VRC_IPN) {
65 4
            $dataSign = $data['data'] ?? '';
66
        } else {
67 1
            ksort($data);
68 1
            $dataSign = implode('|', $data);
69
        }
70
71 5
        if (!$expectSignature || !$client->validateSignature($dataSign, $expectSignature)) {
72 3
            $validator->addError($this, $attribute, $validator->message);
73 2
        } elseif ($this->command === PaymentGateway::VRC_IPN) {
74 2
            $attributes = ['amount', 'message', 'payment_type', 'reference_number', 'status', 'trans_ref_no', 'website_id'];
75 2
            $values = explode('|', $dataSign);
76
77 2
            foreach (array_combine($attributes, $values) as $attr => $value) {
78 2
                $this->defineAttribute($attr, $value === '' ? null : $value);
79
            }
80
        }
81 5
    }
82
83
}
84