Passed
Push — master ( d1dcd3...a849c7 )
by Songda
01:59
created

AddRadarPlugin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 27
dl 0
loc 59
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaders() 0 29 5
A assembly() 0 18 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\ContainerException;
10
use Yansongda\Pay\Exception\Exception;
11
use Yansongda\Pay\Exception\InvalidParamsException;
12
use Yansongda\Pay\Exception\ServiceNotFoundException;
13
use Yansongda\Pay\Logger;
14
use Yansongda\Pay\Request;
15
use Yansongda\Pay\Rocket;
16
use Yansongda\Supports\Collection;
17
18
use function Yansongda\Pay\get_wechat_body;
19
use function Yansongda\Pay\get_wechat_config;
20
use function Yansongda\Pay\get_wechat_method;
21
use function Yansongda\Pay\get_wechat_url;
22
23
class AddRadarPlugin implements PluginInterface
24
{
25
    /**
26
     * @throws ContainerException
27
     * @throws InvalidParamsException
28
     * @throws ServiceNotFoundException
29
     */
30
    public function assembly(Rocket $rocket, Closure $next): Rocket
31
    {
32
        Logger::debug('[Wechat][AddRadarPlugin] 插件开始装载', ['rocket' => $rocket]);
33
34
        $params = $rocket->getParams();
35
        $payload = $rocket->getPayload();
36
        $config = get_wechat_config($params);
37
38
        $rocket->setRadar(new Request(
39
            get_wechat_method($payload),
40
            get_wechat_url($config, $payload),
41
            $this->getHeaders($payload),
42
            get_wechat_body($payload),
43
        ));
44
45
        Logger::info('[Wechat][AddRadarPlugin] 插件装载完毕', ['rocket' => $rocket]);
46
47
        return $next($rocket);
48
    }
49
50
    /**
51
     * @throws InvalidParamsException
52
     */
53
    protected function getHeaders(?Collection $payload): array
54
    {
55
        $authorization = $payload?->get('_authorization') ?? null;
56
57
        if (empty($authorization)) {
58
            throw new InvalidParamsException(Exception::PARAMS_WECHAT_AUTHORIZATION_MISSING, '参数异常: 微信 `_authorization` 参数缺失:你可能用错插件顺序,应该先使用 `AddPayloadSignaturePlugin`, 再使用 `AddRadarPlugin`');
59
        }
60
61
        $headers = [
62
            'Accept' => 'application/json, text/plain, application/x-gzip',
63
            'User-Agent' => 'yansongda/pay-v3',
64
            'Content-Type' => 'application/json; charset=utf-8',
65
            'Authorization' => $authorization,
66
        ];
67
68
        // 当 body 里有加密内容时,需要传递此参数用于微信区分
69
        if ($payload->has('_serial_no')) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on null. ( Ignorable by Annotation )

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

69
        if ($payload->/** @scrutinizer ignore-call */ has('_serial_no')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
            $headers['Wechatpay-Serial'] = $payload->get('_serial_no');
71
        }
72
73
        if ($payload->has('_content-type')) {
74
            $headers['Content-Type'] = $payload->get('_content-type');
75
        }
76
77
        if ($payload->has('_accept')) {
78
            $headers['Accept'] = $payload->get('_accept');
79
        }
80
81
        return $headers;
82
    }
83
}
84