Passed
Pull Request — master (#662)
by Songda
02:21
created

LaunchPlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 12
c 1
b 0
f 0
dl 0
loc 30
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 22 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
12
use function Yansongda\Pay\should_do_http_request;
13
use function Yansongda\Pay\verify_unipay_sign;
14
15
use Yansongda\Supports\Collection;
16
17
class LaunchPlugin implements PluginInterface
18
{
19
    /**
20
     * @throws \Yansongda\Pay\Exception\ContainerException
21
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
22
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
23
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
24
     */
25
    public function assembly(Rocket $rocket, Closure $next): Rocket
26
    {
27
        /* @var Rocket $rocket */
28
        $rocket = $next($rocket);
29
30
        Logger::info('[unipay][LaunchPlugin] 插件开始装载', ['rocket' => $rocket]);
31
32
        if (should_do_http_request($rocket->getDirection())) {
33
            $response = Collection::wrap($rocket->getDestination());
34
            $signature = $response->get('signature');
35
            $response->forget('signature');
36
37
            verify_unipay_sign(
38
                $rocket->getParams(), $response->sortKeys()->toString(), $signature
39
            );
40
41
            $rocket->setDestination($response);
42
        }
43
44
        Logger::info('[unipay][LaunchPlugin] 插件装载完毕', ['rocket' => $rocket]);
45
46
        return $rocket;
47
    }
48
}
49