Passed
Pull Request — master (#1002)
by
unknown
02:06
created

AddRadarPlugin::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Epay;
6
7
use Closure;
8
use GuzzleHttp\Psr7\Request;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Logger;
11
use Yansongda\Artful\Rocket;
12
use Yansongda\Supports\Collection;
13
14
use function Yansongda\Pay\get_epay_url;
15
use function Yansongda\Pay\get_provider_config;
16
17
class AddRadarPlugin implements PluginInterface
18
{
19
    public function assembly(Rocket $rocket, Closure $next): Rocket
20
    {
21
        Logger::info('[epay][AddRadarPlugin] 插件开始装载', ['rocket' => $rocket]);
22
23
        $params = $rocket->getParams();
24
        $config = get_provider_config('epay', $params);
25
        $payload = $rocket->getPayload();
26
27
        $rocket->setRadar(new Request(
28
            strtoupper($params['_method'] ?? 'POST'),
29
            get_epay_url($config, $payload),
30
            $this->getHeaders(),
31
            $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\Epa...dRadarPlugin::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

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