Passed
Pull Request — master (#662)
by Songda
01:49
created

RadarSignPlugin::assembly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
rs 10
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 GuzzleHttp\Psr7\Utils;
9
use Yansongda\Pay\Contract\PluginInterface;
10
11
use function Yansongda\Pay\get_unipay_config;
12
13
use Yansongda\Pay\Logger;
14
use Yansongda\Pay\Rocket;
15
use Yansongda\Pay\Traits\GetUnipayCerts;
16
use Yansongda\Supports\Collection;
17
18
class RadarSignPlugin implements PluginInterface
19
{
20
    use GetUnipayCerts;
21
22
    /**
23
     * @throws \Yansongda\Pay\Exception\ContainerException
24
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
25
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
26
     */
27
    public function assembly(Rocket $rocket, Closure $next): Rocket
28
    {
29
        Logger::info('[unipay][PreparePlugin] 插件开始装载', ['rocket' => $rocket]);
30
31
        $this->sign($rocket);
32
33
        $this->reRadar($rocket);
34
35
        Logger::info('[unipay][PreparePlugin] 插件装载完毕', ['rocket' => $rocket]);
36
37
        return $next($rocket);
38
    }
39
40
    /**
41
     * @throws \Yansongda\Pay\Exception\ContainerException
42
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
43
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
44
     */
45
    protected function sign(Rocket $rocket): void
46
    {
47
        $payload = $rocket->getPayload()->filter(fn ($v, $k) => 'signature' != $k);
48
        $config = $this->getConfig($rocket->getParams());
49
50
        $rocket->mergePayload([
51
            'signature' => $this->getSignature($config['certs']['pkey'] ?? '', $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\Uni...nPlugin::getSignature() 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

51
            'signature' => $this->getSignature($config['certs']['pkey'] ?? '', /** @scrutinizer ignore-type */ $payload),
Loading history...
52
        ]);
53
    }
54
55
    protected function reRadar(Rocket $rocket): void
56
    {
57
        $body = $this->getBody($rocket->getPayload());
0 ignored issues
show
Bug introduced by
It seems like $rocket->getPayload() can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Uni...arSignPlugin::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

57
        $body = $this->getBody(/** @scrutinizer ignore-type */ $rocket->getPayload());
Loading history...
58
        $radar = $rocket->getRadar();
59
60
        if (!empty($body) && !empty($radar)) {
61
            $radar = $radar->withBody(Utils::streamFor($body));
62
63
            $rocket->setRadar($radar);
64
        }
65
    }
66
67
    /**
68
     * @throws \Yansongda\Pay\Exception\ContainerException
69
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
70
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
71
     */
72
    protected function getConfig(array $params): array
73
    {
74
        $config = get_unipay_config($params);
75
76
        if (empty($config['certs']['pkey'])) {
77
            $this->getCertId($params['_config'] ?? 'default', $config);
78
79
            $config = get_unipay_config($params);
80
        }
81
82
        return $config;
83
    }
84
85
    protected function getSignature(string $pkey, Collection $payload): string
86
    {
87
        $content = $payload->sortKeys()->toString();
88
89
        openssl_sign(hash('sha256', $content), $sign, $pkey, 'sha256');
90
91
        return base64_encode($sign);
92
    }
93
94
    protected function getBody(Collection $payload): string
95
    {
96
        return $payload->query();
97
    }
98
}
99