Completed
Push — master ( 065035...4118ee )
by Songda
01:49
created

WebGateway   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 6.93 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 7
loc 101
rs 10
c 0
b 0
f 0

5 Methods

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

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