ResponsePlugin   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 28 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Alipay\V2;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Exception\InvalidResponseException;
10
use Yansongda\Artful\Logger;
11
use Yansongda\Artful\Rocket;
12
use Yansongda\Pay\Exception\Exception;
13
use Yansongda\Supports\Collection;
14
15
use function Yansongda\Artful\should_do_http_request;
16
17
class ResponsePlugin implements PluginInterface
18
{
19
    /**
20
     * @throws InvalidResponseException
21
     */
22
    public function assembly(Rocket $rocket, Closure $next): Rocket
23
    {
24
        /* @var Rocket $rocket */
25
        $rocket = $next($rocket);
26
27
        Logger::debug('[Alipay][ResponsePlugin] 插件开始装载', ['rocket' => $rocket]);
28
29
        $destination = $rocket->getDestination();
30
        $payload = $rocket->getPayload();
31
        $resultKey = str_replace('.', '_', $payload->get('method')).'_response';
32
33
        if (should_do_http_request($rocket->getDirection()) && $destination instanceof Collection) {
34
            $sign = $destination->get('sign', '');
35
            $response = $destination->get($resultKey, $destination->all());
36
37
            if (empty($sign) && '10000' !== ($response['code'] ?? 'null')) {
38
                throw new InvalidResponseException(Exception::RESPONSE_BUSINESS_CODE_WRONG, '支付宝网关响应异常: '.($response['sub_msg'] ?? $response['msg'] ?? '未知错误,请查看支付宝原始响应'), $rocket->getDestination());
39
            }
40
41
            $rocket->setDestination(new Collection(array_merge(
42
                ['_sign' => $sign],
43
                $response
44
            )));
45
        }
46
47
        Logger::info('[Alipay][ResponsePlugin] 插件装载完毕', ['rocket' => $rocket]);
48
49
        return $rocket;
50
    }
51
}
52