Passed
Pull Request — master (#909)
by Songda
04:52 queued 02:52
created

ResponsePlugin   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A service() 0 4 1
A normal() 0 4 1
A assembly() 0 28 3
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\Pay;
15
use Yansongda\Pay\Rocket;
16
use Yansongda\Supports\Collection;
17
18
use function Yansongda\Pay\get_wechat_config;
19
20
/**
21
 * @see https://pay.weixin.qq.com/docs/merchant/apis/consumer-complaint/complaints/response-complaint-v2.html
22
 * @see https://pay.weixin.qq.com/docs/partner/apis/consumer-complaint/complaints/response-complaint-v2.html
23
 */
24
class ResponsePlugin implements PluginInterface
25
{
26
    /**
27
     * @throws InvalidParamsException
28
     * @throws ContainerException
29
     * @throws ServiceNotFoundException
30
     */
31
    public function assembly(Rocket $rocket, Closure $next): Rocket
32
    {
33
        Logger::debug('[Wechat][Extend][Complaints][ResponsePlugin] 插件开始装载', ['rocket' => $rocket]);
34
35
        $config = get_wechat_config($rocket->getParams());
36
        $payload = $rocket->getPayload();
37
        $complaintId = $payload?->get('complaint_id') ?? null;
38
39
        if (empty($complaintId)) {
40
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 反馈处理完成,参数缺少 `complaint_id`');
41
        }
42
43
        if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) {
44
            $data = $this->service($payload, $config);
0 ignored issues
show
Bug introduced by
It seems like $payload can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Wec...sponsePlugin::service() does only seem to accept Yansongda\Supports\Collection, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            $data = $this->service(/** @scrutinizer ignore-type */ $payload, $config);
Loading history...
45
        }
46
47
        $rocket->mergePayload(array_merge(
48
            [
49
                '_method' => 'POST',
50
                '_url' => 'v3/merchant-service/complaints-v2/'.$complaintId.'/response',
51
                '_service_url' => 'v3/merchant-service/complaints-v2/'.$complaintId.'/response',
52
            ],
53
            $data ?? $this->normal($payload, $config)
0 ignored issues
show
Bug introduced by
It seems like $payload can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Wec...esponsePlugin::normal() does only seem to accept Yansongda\Supports\Collection, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            $data ?? $this->normal(/** @scrutinizer ignore-type */ $payload, $config)
Loading history...
54
        ));
55
56
        Logger::info('[Wechat][Extend][Complaints][ResponsePlugin] 插件装载完毕', ['rocket' => $rocket]);
57
58
        return $next($rocket);
59
    }
60
61
    protected function normal(Collection $payload, array $config): array
62
    {
63
        return [
64
            'complainted_mchid' => $payload->get('complainted_mchid', $config['mch_id']),
65
        ];
66
    }
67
68
    protected function service(Collection $payload, array $config): array
69
    {
70
        return [
71
            'complainted_mchid' => $payload->get('complainted_mchid', $config['sub_mch_id']),
72
        ];
73
    }
74
}
75