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

AddRadarPlugin::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
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\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_epay_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('[Epay][AddRadarPlugin] 插件开始装载', ['rocket' => $rocket]);
28
29
        $params = $rocket->getParams();
30
        $config = get_provider_config('epay', $params);
31
        $payload = $rocket->getPayload();
32
33
        $rocket->setRadar(new Request(
34
            strtoupper($params['_method'] ?? 'POST'),
35
            get_epay_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\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

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