Completed
Push — master ( b89557...66fcea )
by Vuong
02:09
created

PaymentGateway   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 5
loc 75
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseUrl() 0 4 2
A initSandboxEnvironment() 0 5 1
A requestInternal() 0 6 1
A getVerifyRequestData() 5 18 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\momo;
9
10
use yii\httpclient\Client as HttpClient;
11
12
use yiiviet\payment\BasePaymentGateway;
13
14
/**
15
 * 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 MOMO.
16
 * Hiện tại nó hổ trợ 100% các tính năng từ cổng thanh toán MOMO All In One Payment.
17
 *
18
 * @method ResponseData purchase(array $data, $clientId = null)
19
 * @method ResponseData queryDR(array $data, $clientId = null)
20
 * @method ResponseData refund(array $data, $clientId = null)
21
 * @method ResponseData queryRefund(array $data, $clientId = null)
22
 * @method bool|VerifiedData verifyRequestIPN($clientId = null, \yii\web\Request $request = null)
23
 * @method bool|VerifiedData verifyRequestPurchaseSuccess($clientId = null, \yii\web\Request $request = null)
24
 * @method PaymentClient getClient($id = null)
25
 * @method PaymentClient getDefaultClient()
26
 *
27
 * @property PaymentClient $client
28
 * @property PaymentClient $defaultClient
29
 *
30
 * @author Vuong Minh <[email protected]>
31
 * @since 1.0.3
32
 */
33
class PaymentGateway extends BasePaymentGateway
34
{
35
    /**
36
     * @inheritdoc
37
     */
38
    public $clientConfig = ['class' => PaymentClient::class];
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public $requestDataConfig = ['class' => RequestData::class];
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public $responseDataConfig = ['class' => ResponseData::class];
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public $verifiedDataConfig = ['class' => VerifiedData::class];
54
55
    /**
56
     * @inheritdoc
57
     */
58 8
    public function getBaseUrl(): string
59
    {
60 8
        return $this->sandbox ? 'https://test-payment.momo.vn/gw_payment/transactionProcessor' : 'https://payment.momo.vn/gw_payment/transactionProcessor';
61
    }
62
63
    /**
64
     * @inheritdoc
65
     * @throws \yii\base\InvalidConfigException
66
     */
67 11
    protected function initSandboxEnvironment()
68
    {
69 11
        $clientConfig = require(__DIR__ . '/sandbox-client.php');
70 11
        $this->setClient($clientConfig);
71 11
    }
72
73
    /**
74
     * @inheritdoc
75
     * @throws \yii\base\InvalidConfigException
76
     * @throws \yii\httpclient\Exception
77
     */
78 8
    protected function requestInternal(\vxm\gatewayclients\RequestData $requestData, HttpClient $httpClient): array
79
    {
80 8
        $data = $requestData->get();
81
82 4
        return $httpClient->post('', $data)->setFormat('json')->send()->getData();
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 2
    protected function getVerifyRequestData($command, \yii\web\Request $request): array
89
    {
90
        $params = [
91 2
            'partnerCode', 'accessKey', 'requestId', 'amount', 'orderId', 'orderInfo', 'orderType',
92
            'transId', 'message', 'localMessage', 'responseTime', 'errorCode', 'payType', 'extraData', 'signature'
93
        ];
94 2
        $commandRequestMethods = [self::VRC_PURCHASE_SUCCESS => 'get', self::VRC_IPN => 'post'];
95 2
        $requestMethod = $commandRequestMethods[$command];
96
97 2
        $data = [];
98 2 View Code Duplication
        foreach ($params as $param) {
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...
99 2
            if (($value = call_user_func([$request, $requestMethod], $param)) !== null) {
100 2
                $data[$param] = $value;
101
            }
102
        }
103
104 2
        return $data;
105
    }
106
107
}
108