PaymentGateway   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 15.04 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 17
loc 113
ccs 20
cts 26
cp 0.7692
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseUrl() 0 4 2
A requestCommands() 0 4 1
A defaultVersion() 0 4 1
A initSandboxEnvironment() 0 6 1
A requestInternal() 5 17 2
A getVerifyRequestData() 12 12 3

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
namespace yiiviet\payment\vnpayment;
9
10
use yiiviet\payment\BasePaymentGateway;
11
12
/**
13
 * Lớp PaymentGateway thực thi các phương thức trừu tượng dùng hổ trợ kết nối đến OnePay.
14
 * Hiện tại nó hổ trợ 100% các tính năng từ cổng thanh toán VnPayment v2.
15
 *
16
 * @method ResponseData purchase(array $data, $clientId = null)
17
 * @method ResponseData queryDR(array $data, $clientId = null)
18
 * @method ResponseData refund(array $data, $clientId = null)
19
 * @method VerifiedData verifyRequestIPN(\yii\web\Request $request = null, $clientId = null)
20
 * @method VerifiedData verifyRequestPurchaseSuccess(\yii\web\Request $request = null, $clientId = null)
21
 * @method PaymentClient getClient($id = null)
22
 * @method PaymentClient getDefaultClient()
23
 *
24
 * @property PaymentClient $client
25
 * @property PaymentClient $defaultClient
26
 *
27
 * @author Vuong Minh <[email protected]>
28
 * @since 1.0
29
 */
30
class PaymentGateway extends BasePaymentGateway
31
{
32
33
    /**
34
     * Đường dẫn API để yêu cầu tạo giao dịch thanh toán.
35
     */
36
    const PURCHASE_URL = '/paymentv2/vpcpay.html';
37
38
    /**
39
     * Đường dẫn API để truy vấn thông tin giao dịch.
40
     */
41
    const QUERY_DR_URL = '/merchant_webapi/merchant.html';
42
43
    /**
44
     * Đường dẫn API để yêu cầu hoàn trả tiền.
45
     */
46
    const REFUND_URL = '/merchant_webapi/merchant.html';
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public $clientConfig = ['class' => PaymentClient::class];
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public $requestDataConfig = ['class' => RequestData::class];
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public $responseDataConfig = ['class' => ResponseData::class];
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public $verifiedDataConfig = ['class' => VerifiedData::class];
67
68
    /**
69
     * @inheritdoc
70
     */
71 3
    public function getBaseUrl(): string
72
    {
73 3
        return $this->sandbox ? 'http://sandbox.vnpayment.vn' : 'http://vnpayment.vn';
74
    }
75
76
    /**
77
     * @inheritdoc
78
     * @since 1.0.3
79
     */
80 3
    public function requestCommands(): array
81
    {
82 3
        return [self::RC_PURCHASE, self::RC_QUERY_DR, self::RC_REFUND];
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 3
    protected function defaultVersion(): string
89
    {
90 3
        return '2.0.0';
91
    }
92
93
    /**
94
     * @inheritdoc
95
     * @throws \yii\base\InvalidConfigException
96
     */
97 7
    protected function initSandboxEnvironment()
98
    {
99 7
        $clientConfig = require(__DIR__ . '/sandbox-client.php');
100
101 7
        $this->setClient($clientConfig);
102 7
    }
103
104
    /**
105
     * @inheritdoc
106
     * @throws \yii\base\InvalidConfigException|\yii\httpclient\Exception
107
     */
108 3
    protected function requestInternal(\vxm\gatewayclients\RequestData $requestData, \yii\httpclient\Client $httpClient): array
109
    {
110 3
        $command = $requestData->getCommand();
111
        $commandUrls = [
112 3
            self::RC_PURCHASE => self::PURCHASE_URL,
113 3
            self::RC_REFUND => self::REFUND_URL,
114 3
            self::RC_QUERY_DR => self::QUERY_DR_URL
115
        ];
116 3
        $data = $requestData->get();
117 3
        $data[0] = $commandUrls[$command];
118
119 3 View Code Duplication
        if ($command === self::RC_PURCHASE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
120 1
            return ['redirect_url' => $httpClient->createRequest()->setUrl($data)->getFullUrl()];
121
        } else {
122 2
            return $httpClient->get($data)->send()->getData();
123
        }
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129 View Code Duplication
    protected function getVerifyRequestData($command, \yii\web\Request $request): array
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...
130
    {
131
        $data = [];
132
133
        foreach ($request->get() as $param => $value) {
134
            if (strpos($param, 'vnp_') === 0) {
135
                $data[$param] = $value;
136
            }
137
        }
138
139
        return $data;
140
    }
141
142
}
143