Passed
Pull Request — master (#1010)
by ma
02:08
created

PayPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 55
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 28 2
A normal() 0 5 1
A service() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Pay\Pos;
6
7
use Closure;
8
use Throwable;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\ServiceNotFoundException;
12
use Yansongda\Artful\Logger;
13
use Yansongda\Artful\Rocket;
14
15
use function Yansongda\Pay\get_provider_config;
16
use function Yansongda\Pay\get_wechat_type_key;
17
18
/**
19
 * @see https://pay.weixin.qq.com/docs/merchant/apis/code-payment-v3/direct/code-pay.html
20
 */
21
class PayPlugin implements PluginInterface
22
{
23
    /**
24
     * @throws ContainerException
25
     * @throws ServiceNotFoundException
26
     * @throws Throwable                随机字符串生成失败
27
     */
28
    public function assembly(Rocket $rocket, Closure $next): Rocket
29
    {
30
        Logger::debug('[Wechat][V3][Pay][Pos][PayPlugin] 插件开始装载', ['rocket' => $rocket]);
31
32
        $payload = $rocket->getPayload();
33
        $params = $rocket->getParams();
34
        $config = get_provider_config('wechat', $params);
35
36
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
37
            $rocket->mergePayload(array_merge(
38
                [
39
                    '_method' => 'POST',
40
                    '_url' => 'v3/pay/partner/transactions/codepay',
41
                ],
42
                $this->service($payload, $params, $config)
0 ignored issues
show
Bug introduced by
It seems like $payload can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Wec...os\PayPlugin::service() does only seem to accept Yansongda\Pay\Plugin\Wechat\V3\Pay\Pos\Collection, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
                $this->service(/** @scrutinizer ignore-type */ $payload, $params, $config)
Loading history...
43
            ));
44
        }else{
45
            $rocket->mergePayload(array_merge(
46
                [
47
                    '_method' => 'POST',
48
                    '_url' => 'v3/pay/transactions/codepay',
49
                ],
50
                $this->normal($params, $config)
51
            ));
52
        }
53
        Logger::info('[Wechat][V3][Pay][Pos][PayPlugin] 插件装载完毕', ['rocket' => $rocket]);
54
55
        return $next($rocket);
56
    }
57
58
    protected function normal(array $params, array $config): array
59
    {
60
        return [
61
            'appid' => $config[get_wechat_type_key($params)] ?? '',
62
            'mchid' => $config['mch_id'] ?? '',
63
        ];
64
    }
65
66
    protected function service(Collection $payload, array $params, array $config): array
0 ignored issues
show
Bug introduced by
The type Yansongda\Pay\Plugin\Wechat\V3\Pay\Pos\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
67
    {
68
        $configKey = get_wechat_type_key($params);
69
70
        $data = [
71
            'sp_appid' => $config[$configKey] ?? '',
72
            'sp_mchid' => $config['mch_id'] ?? '',
73
            'sub_mchid' => $payload->get('sub_mchid', $config['sub_mch_id'] ?? ''),
74
        ];
75
        return $data;
76
    }
77
78
}
79