AddRadarPlugin::getBody()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Jsb;
6
7
use Closure;
8
use GuzzleHttp\Psr7\Request;
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
use Yansongda\Supports\Collection;
15
16
use function Yansongda\Pay\get_jsb_url;
17
use function Yansongda\Pay\get_provider_config;
18
19
class AddRadarPlugin implements PluginInterface
20
{
21
    /**
22
     * @throws ServiceNotFoundException
23
     * @throws ContainerException
24
     */
25
    public function assembly(Rocket $rocket, Closure $next): Rocket
26
    {
27
        Logger::info('[Jsb][AddRadarPlugin] 插件开始装载', ['rocket' => $rocket]);
28
29
        $params = $rocket->getParams();
30
        $config = get_provider_config('jsb', $params);
31
        $payload = $rocket->getPayload();
32
33
        $rocket->setRadar(new Request(
34
            strtoupper($params['_method'] ?? 'POST'),
35
            get_jsb_url($config, $payload),
36
            $this->getHeaders(),
37
            $this->getBody($payload),
0 ignored issues
show
Bug introduced by
It seems like $payload can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Jsb\AddRadarPlugin::getBody() does only seem to accept Yansongda\Supports\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

37
            $this->getBody(/** @scrutinizer ignore-type */ $payload),
Loading history...
38
        ));
39
40
        Logger::info('[Jsb][AddRadarPlugin] 插件装载完毕', ['rocket' => $rocket]);
41
42
        return $next($rocket);
43
    }
44
45
    protected function getHeaders(): array
46
    {
47
        return [
48
            'Content-Type' => 'text/html',
49
            'User-Agent' => 'yansongda/pay-v3',
50
        ];
51
    }
52
53
    protected function getBody(Collection $payload): string
54
    {
55
        $sign = $payload->get('sign');
56
        $signType = $payload->get('signType');
57
58
        $payload->forget('sign');
59
        $payload->forget('signType');
60
61
        $payload = $payload->sortKeys();
62
63
        $payload->set('sign', $sign);
64
        $payload->set('signType', $signType);
65
66
        return $payload->toString();
67
    }
68
}
69