MiniGateway   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A pay() 17 17 5

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 Yansongda\Pay\Events;
6
use Yansongda\Pay\Exceptions\GatewayException;
7
use Yansongda\Pay\Exceptions\InvalidArgumentException;
8
use Yansongda\Pay\Exceptions\InvalidConfigException;
9
use Yansongda\Pay\Exceptions\InvalidSignException;
10
use Yansongda\Pay\Gateways\Alipay;
11
use Yansongda\Supports\Collection;
12
13 View Code Duplication
class MiniGateway extends Gateway
0 ignored issues
show
Duplication introduced by iwzh
This class 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...
14
{
15
    /**
16
     * Pay an order.
17
     *
18
     * @author xiaozan <[email protected]>
19
     *
20
     * @param string $endpoint
21
     *
22
     * @throws GatewayException
23
     * @throws InvalidArgumentException
24
     * @throws InvalidConfigException
25
     * @throws InvalidSignException
26
     *
27
     * @see https://docs.alipay.com/mini/introduce/pay
28
     */
29
    public function pay($endpoint, array $payload): Collection
30
    {
31
        $biz_array = json_decode($payload['biz_content'], true);
32
        if (empty($biz_array['buyer_id'])) {
33
            throw new InvalidArgumentException('buyer_id required');
34
        }
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['biz_content'] = json_encode($biz_array);
39
        $payload['method'] = 'alipay.trade.create';
40
        $payload['sign'] = Support::generateSign($payload);
41
42
        Events::dispatch(new Events\PayStarted('Alipay', 'Mini', $endpoint, $payload));
43
44
        return Support::requestApi($payload);
45
    }
46
}
47