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

QueryNegotiationPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
dl 0
loc 32
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normal() 0 5 2
A assembly() 0 20 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\Exception;
10
use Yansongda\Pay\Exception\InvalidParamsException;
11
use Yansongda\Pay\Logger;
12
use Yansongda\Pay\Rocket;
13
use Yansongda\Supports\Collection;
14
15
/**
16
 * @see https://pay.weixin.qq.com/docs/merchant/apis/consumer-complaint/complaints/query-negotiation-history-v2.html
17
 * @see https://pay.weixin.qq.com/docs/partner/apis/consumer-complaint/complaints/query-negotiation-history-v2.html
18
 */
19
class QueryNegotiationPlugin implements PluginInterface
20
{
21
    /**
22
     * @throws InvalidParamsException
23
     */
24
    public function assembly(Rocket $rocket, Closure $next): Rocket
25
    {
26
        Logger::debug('[Wechat][Extend][Complaints][QueryNegotiationPlugin] 插件开始装载', ['rocket' => $rocket]);
27
28
        $payload = $rocket->getPayload();
29
        $complaintId = $payload?->get('complaint_id') ?? null;
30
31
        if (empty($complaintId)) {
32
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 查询投诉单协商历史,参数缺少 `complaint_id`');
33
        }
34
35
        $rocket->setPayload([
36
            '_method' => 'GET',
37
            '_url' => 'v3/merchant-service/complaints-v2/'.$complaintId.'/negotiation-historys'.$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
            '_url' => 'v3/merchant-service/complaints-v2/'.$complaintId.'/negotiation-historys'.$this->normal(/** @scrutinizer ignore-type */ $payload),
Loading history...
38
            '_service_url' => 'v3/merchant-service/complaints-v2/'.$complaintId.'/negotiation-historys'.$this->normal($payload),
39
        ]);
40
41
        Logger::info('[Wechat][Extend][Complaints][QueryNegotiationPlugin] 插件装载完毕', ['rocket' => $rocket]);
42
43
        return $next($rocket);
44
    }
45
46
    protected function normal(Collection $payload): string
47
    {
48
        $query = $payload->except('complaint_id')->query();
49
50
        return empty($query) ? '' : '?'.$query;
51
    }
52
}
53