WebGateway   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 6.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 7
loc 104
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 7 7 2
A buildPayHtml() 0 16 3
A getMethod() 0 4 1
A getProductCode() 0 4 1
A pay() 0 19 4

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
namespace Yansongda\Pay\Gateways\Alipay;
4
5
use Symfony\Component\HttpFoundation\RedirectResponse;
6
use Symfony\Component\HttpFoundation\Response;
7
use Yansongda\Pay\Events;
8
use Yansongda\Pay\Exceptions\InvalidArgumentException;
9
use Yansongda\Pay\Exceptions\InvalidConfigException;
10
use Yansongda\Pay\Gateways\Alipay;
11
12
class WebGateway extends Gateway
13
{
14
    /**
15
     * Pay an order.
16
     *
17
     * @author yansongda <[email protected]>
18
     *
19
     * @param string $endpoint
20
     * @param array  $payload
21
     *
22
     * @throws InvalidConfigException
23
     * @throws InvalidArgumentException
24
     *
25
     * @return Response
26
     */
27
    public function pay($endpoint, array $payload): Response
28
    {
29
        $biz_array = json_decode($payload['biz_content'], true);
30
        $biz_array['product_code'] = $this->getProductCode();
31
32
        $method = $biz_array['http_method'] ?? 'POST';
33
34
        unset($biz_array['http_method']);
35
        if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) {
36
            $biz_array['extend_params'] = is_array($biz_array['extend_params']) ? array_merge(['sys_service_provider_id' => Support::getInstance()->pid], $biz_array['extend_params']) : ['sys_service_provider_id' => Support::getInstance()->pid];
37
        }
38
        $payload['method'] = $this->getMethod();
39
        $payload['biz_content'] = json_encode($biz_array);
40
        $payload['sign'] = Support::generateSign($payload);
41
42
        Events::dispatch(new Events\PayStarted('Alipay', 'Web/Wap', $endpoint, $payload));
43
44
        return $this->buildPayHtml($endpoint, $payload, $method);
45
    }
46
47
    /**
48
     * Find.
49
     *
50
     * @author yansongda <[email protected]>
51
     *
52
     * @param $order
53
     *
54
     * @return array
55
     */
56 View Code Duplication
    public function find($order): array
0 ignored issues
show
Duplication introduced by yansongda
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...
57
    {
58
        return [
59
            'method' => 'alipay.trade.query',
60
            'biz_content' => json_encode(is_array($order) ? $order : ['out_trade_no' => $order]),
61
        ];
62
    }
63
64
    /**
65
     * Build Html response.
66
     *
67
     * @author yansongda <[email protected]>
68
     *
69
     * @param string $endpoint
70
     * @param array  $payload
71
     * @param string $method
72
     *
73
     * @return Response
74
     */
75
    protected function buildPayHtml($endpoint, $payload, $method = 'POST'): Response
76
    {
77
        if ('GET' === strtoupper($method)) {
78
            return RedirectResponse::create($endpoint.'&'.http_build_query($payload));
79
        }
80
81
        $sHtml = "<form id='alipay_submit' name='alipay_submit' action='".$endpoint."' method='".$method."'>";
82
        foreach ($payload as $key => $val) {
83
            $val = str_replace("'", '&apos;', $val);
84
            $sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
85
        }
86
        $sHtml .= "<input type='submit' value='ok' style='display:none;'></form>";
87
        $sHtml .= "<script>document.forms['alipay_submit'].submit();</script>";
88
89
        return Response::create($sHtml);
90
    }
91
92
    /**
93
     * Get method config.
94
     *
95
     * @author yansongda <[email protected]>
96
     *
97
     * @return string
98
     */
99
    protected function getMethod(): string
100
    {
101
        return 'alipay.trade.page.pay';
102
    }
103
104
    /**
105
     * Get productCode config.
106
     *
107
     * @author yansongda <[email protected]>
108
     *
109
     * @return string
110
     */
111
    protected function getProductCode(): string
112
    {
113
        return 'FAST_INSTANT_TRADE_PAY';
114
    }
115
}
116