Passed
Pull Request — master (#894)
by Songda
01:58
created

ResponsePlugin   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 21 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Alipay;
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
use function Yansongda\Pay\should_do_http_request;
14
15
class ResponsePlugin implements PluginInterface
16
{
17
    public function assembly(Rocket $rocket, Closure $next): Rocket
18
    {
19
        /* @var Rocket $rocket */
20
        $rocket = $next($rocket);
21
22
        Logger::debug('[Alipay][ResponsePlugin] 插件开始装载', ['rocket' => $rocket]);
23
24
        $destination = $rocket->getDestination();
25
        $payload = $rocket->getPayload();
26
        $resultKey = str_replace('.', '_', $payload->get('method')).'_response';
27
28
        if (should_do_http_request($rocket->getDirection()) && $destination instanceof Collection) {
29
            $rocket->setDestination(new Collection(array_merge(
30
                ['_sign' => $destination->get('sign', '')],
31
                $destination->get($resultKey, $destination->all())
32
            )));
33
        }
34
35
        Logger::info('[Alipay][ResponsePlugin] 插件装载完毕', ['rocket' => $rocket]);
36
37
        return $rocket;
38
    }
39
}
40