Passed
Push — master ( 1dfbea...9713c0 )
by Songda
02:53 queued 51s
created

AddRadarPlugin::assembly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 18
rs 9.9
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Unipay;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\ContainerException;
10
use Yansongda\Pay\Exception\InvalidParamsException;
11
use Yansongda\Pay\Exception\ServiceNotFoundException;
12
use Yansongda\Pay\Logger;
13
use Yansongda\Pay\Request;
14
use Yansongda\Pay\Rocket;
15
use Yansongda\Supports\Collection;
16
17
use function Yansongda\Pay\get_radar_method;
18
use function Yansongda\Pay\get_unipay_body;
19
use function Yansongda\Pay\get_unipay_config;
20
use function Yansongda\Pay\get_unipay_url;
21
22
class AddRadarPlugin implements PluginInterface
23
{
24
    /**
25
     * @throws ContainerException
26
     * @throws ServiceNotFoundException
27
     * @throws InvalidParamsException
28
     */
29
    public function assembly(Rocket $rocket, Closure $next): Rocket
30
    {
31
        Logger::debug('[Unipay][AddRadarPlugin] 插件开始装载', ['rocket' => $rocket]);
32
33
        $params = $rocket->getParams();
34
        $config = get_unipay_config($params);
35
        $payload = $rocket->getPayload();
36
37
        $rocket->setRadar(new Request(
38
            get_radar_method($payload) ?? 'POST',
39
            get_unipay_url($config, $payload),
40
            $this->getHeaders($payload),
41
            get_unipay_body($payload),
42
        ));
43
44
        Logger::info('[Unipay][AddRadarPlugin] 插件装载完毕', ['rocket' => $rocket]);
45
46
        return $next($rocket);
47
    }
48
49
    protected function getHeaders(?Collection $payload): array
50
    {
51
        $headers = [
52
            'User-Agent' => 'yansongda/pay-v3',
53
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
54
        ];
55
56
        if ($payload->has('_content-type')) {
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

56
        if ($payload->/** @scrutinizer ignore-call */ has('_content-type')) {

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...
57
            $headers['Content-Type'] = $payload->get('_content-type');
58
        }
59
60
        if ($payload->has('_accept')) {
61
            $headers['Accept'] = $payload->get('_accept');
62
        }
63
64
        return $headers;
65
    }
66
}
67