Passed
Pull Request — master (#662)
by Songda
03:38
created

SignPlugin::assembly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
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 Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Logger;
10
use Yansongda\Pay\Rocket;
11
use Yansongda\Supports\Collection;
12
13
class SignPlugin implements PluginInterface
14
{
15
    /**
16
     * @throws \Yansongda\Pay\Exception\ContainerException
17
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
18
     */
19
    public function assembly(Rocket $rocket, Closure $next): Rocket
20
    {
21
        Logger::info('[unipay][PreparePlugin] 插件开始装载', ['rocket' => $rocket]);
22
23
        $payload = $rocket->getPayload()->filter(fn ($v, $k) => 'signature' != $k);
24
25
        $rocket->mergePayload([
26
            'signature' => $this->getSign(get_unipay_config($rocket->getParams()), $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\Unipay\SignPlugin::getSign() 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

26
            'signature' => $this->getSign(get_unipay_config($rocket->getParams()), /** @scrutinizer ignore-type */ $payload),
Loading history...
27
        ]);
28
29
        Logger::info('[unipay][PreparePlugin] 插件装载完毕', ['rocket' => $rocket]);
30
31
        return $next($rocket);
32
    }
33
34
    protected function getSign(array $config, Collection $payload): string
35
    {
36
        $content = $payload->sortKeys()->toString();
37
38
        openssl_sign(sha1($content), $sign, $config['certs']['pkey'] ?? '');
39
40
        return base64_encode($sign);
41
    }
42
}
43