QueryNegotiationPlugin::assembly()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 22
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Extend\Complaints;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Exception\InvalidParamsException;
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\filter_params;
16
17
/**
18
 * @see https://pay.weixin.qq.com/docs/merchant/apis/consumer-complaint/complaints/query-negotiation-history-v2.html
19
 * @see https://pay.weixin.qq.com/docs/partner/apis/consumer-complaint/complaints/query-negotiation-history-v2.html
20
 */
21
class QueryNegotiationPlugin implements PluginInterface
22
{
23
    /**
24
     * @throws InvalidParamsException
25
     */
26
    public function assembly(Rocket $rocket, Closure $next): Rocket
27
    {
28
        Logger::debug('[Wechat][Extend][Complaints][QueryNegotiationPlugin] 插件开始装载', ['rocket' => $rocket]);
29
30
        $payload = $rocket->getPayload();
31
        $complaintId = $payload?->get('complaint_id') ?? null;
32
33
        if (empty($complaintId)) {
34
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 查询投诉单协商历史,参数缺少 `complaint_id`');
35
        }
36
37
        $query = $this->normal($payload);
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...tiationPlugin::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

37
        $query = $this->normal(/** @scrutinizer ignore-type */ $payload);
Loading history...
38
39
        $rocket->setPayload([
40
            '_method' => 'GET',
41
            '_url' => 'v3/merchant-service/complaints-v2/'.$complaintId.'/negotiation-historys'.$query,
42
            '_service_url' => 'v3/merchant-service/complaints-v2/'.$complaintId.'/negotiation-historys'.$query,
43
        ]);
44
45
        Logger::info('[Wechat][Extend][Complaints][QueryNegotiationPlugin] 插件装载完毕', ['rocket' => $rocket]);
46
47
        return $next($rocket);
48
    }
49
50
    protected function normal(Collection $payload): string
51
    {
52
        $query = filter_params($payload)->except('complaint_id')->query();
53
54
        return empty($query) ? '' : '?'.$query;
55
    }
56
}
57