VerifySignaturePlugin   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 30 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Unipay\Open;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Exception\ContainerException;
10
use Yansongda\Artful\Exception\InvalidConfigException;
11
use Yansongda\Artful\Exception\ServiceNotFoundException;
12
use Yansongda\Artful\Logger;
13
use Yansongda\Artful\Rocket;
14
use Yansongda\Pay\Exception\InvalidSignException;
15
use Yansongda\Supports\Collection;
16
17
use function Yansongda\Artful\should_do_http_request;
18
use function Yansongda\Pay\get_provider_config;
19
use function Yansongda\Pay\verify_unipay_sign;
20
21
class VerifySignaturePlugin implements PluginInterface
22
{
23
    /**
24
     * @throws ContainerException
25
     * @throws InvalidConfigException
26
     * @throws InvalidSignException
27
     * @throws ServiceNotFoundException
28
     */
29
    public function assembly(Rocket $rocket, Closure $next): Rocket
30
    {
31
        /* @var Rocket $rocket */
32
        $rocket = $next($rocket);
33
34
        Logger::debug('[Unipay][VerifySignaturePlugin] 插件开始装载', ['rocket' => $rocket]);
35
36
        if (!should_do_http_request($rocket->getDirection())) {
37
            return $rocket;
38
        }
39
40
        $destination = $rocket->getDestination();
41
42
        if (!$destination instanceof Collection) {
43
            return $rocket;
44
        }
45
46
        $params = $rocket->getParams();
47
        $config = get_provider_config('unipay', $params);
48
49
        verify_unipay_sign(
50
            $config,
51
            $destination->except('signature')->sortKeys()->toString(),
52
            $destination->get('signature', ''),
53
            $destination->get('signPubKeyCert')
54
        );
55
56
        Logger::info('[Unipay][VerifySignaturePlugin] 插件装载完毕', ['rocket' => $rocket]);
57
58
        return $rocket;
59
    }
60
}
61