Passed
Pull Request — master (#909)
by Songda
02:13
created

ResponsePlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 15
c 1
b 0
f 0
dl 0
loc 29
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\Wechat\Extend\Complaints;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\ContainerException;
10
use Yansongda\Pay\Exception\Exception;
11
use Yansongda\Pay\Exception\InvalidParamsException;
12
use Yansongda\Pay\Exception\ServiceNotFoundException;
13
use Yansongda\Pay\Logger;
14
use Yansongda\Pay\Rocket;
15
16
use function Yansongda\Pay\get_wechat_config;
17
18
/**
19
 * @see https://pay.weixin.qq.com/docs/merchant/apis/consumer-complaint/complaints/response-complaint-v2.html
20
 * @see https://pay.weixin.qq.com/docs/partner/apis/consumer-complaint/complaints/response-complaint-v2.html
21
 */
22
class ResponsePlugin implements PluginInterface
23
{
24
    /**
25
     * @throws InvalidParamsException
26
     * @throws ContainerException
27
     * @throws ServiceNotFoundException
28
     */
29
    public function assembly(Rocket $rocket, Closure $next): Rocket
30
    {
31
        Logger::debug('[Wechat][Extend][Complaints][ResponsePlugin] 插件开始装载', ['rocket' => $rocket]);
32
33
        $config = get_wechat_config($rocket->getParams());
34
        $payload = $rocket->getPayload();
35
        $complaintId = $payload?->get('complaint_id') ?? null;
36
37
        if (empty($complaintId)) {
38
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 回复用户,参数缺少 `complaint_id`');
39
        }
40
41
        $rocket->mergePayload([
42
            '_method' => 'POST',
43
            '_url' => 'v3/merchant-service/complaints-v2/'.$complaintId.'/response',
44
            '_service_url' => 'v3/merchant-service/complaints-v2/'.$complaintId.'/response',
45
            'complainted_mchid' => $payload->get('complainted_mchid', $config['mch_id']),
46
        ])->exceptPayload('complaint_id');
47
48
        Logger::info('[Wechat][Extend][Complaints][ResponsePlugin] 插件装载完毕', ['rocket' => $rocket]);
49
50
        return $next($rocket);
51
    }
52
}
53