QueryDetailPlugin::assembly()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 38
rs 9.3222
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\ContainerException;
10
use Yansongda\Artful\Exception\InvalidConfigException;
11
use Yansongda\Artful\Exception\InvalidParamsException;
12
use Yansongda\Artful\Exception\ServiceNotFoundException;
13
use Yansongda\Artful\Logger;
14
use Yansongda\Artful\Rocket;
15
use Yansongda\Pay\Exception\Exception;
16
use Yansongda\Supports\Collection;
17
18
use function Yansongda\Pay\decrypt_wechat_contents;
19
use function Yansongda\Pay\get_provider_config;
20
21
/**
22
 * @see https://pay.weixin.qq.com/docs/merchant/apis/consumer-complaint/complaints/query-complaint-v2.html
23
 * @see https://pay.weixin.qq.com/docs/partner/apis/consumer-complaint/complaints/query-complaint-v2.html
24
 */
25
class QueryDetailPlugin implements PluginInterface
26
{
27
    /**
28
     * @throws InvalidParamsException
29
     * @throws InvalidConfigException
30
     * @throws ContainerException
31
     * @throws ServiceNotFoundException
32
     */
33
    public function assembly(Rocket $rocket, Closure $next): Rocket
34
    {
35
        Logger::debug('[Wechat][Extend][Complaints][QueryDetailPlugin] 插件开始装载', ['rocket' => $rocket]);
36
37
        $complaintId = $rocket->getPayload()?->get('complaint_id') ?? null;
38
39
        if (empty($complaintId)) {
40
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 查询投诉单详情,参数缺少 `complaint_id`');
41
        }
42
43
        $rocket->setPayload([
44
            '_method' => 'GET',
45
            '_url' => 'v3/merchant-service/complaints-v2/'.$complaintId,
46
            '_service_url' => 'v3/merchant-service/complaints-v2/'.$complaintId,
47
        ]);
48
49
        Logger::info('[Wechat][Extend][Complaints][QueryDetailPlugin] 插件装载完毕', ['rocket' => $rocket]);
50
51
        /** @var Rocket $rocket */
52
        $rocket = $next($rocket);
53
54
        Logger::debug('[Wechat][Extend][Complaints][QueryDetailPlugin] 插件开始后置装载', ['rocket' => $rocket]);
55
56
        $destination = $rocket->getDestination();
57
58
        if ($destination instanceof Collection && !empty($payerPhone = $destination->get('payer_phone'))) {
59
            $decryptPayerPhone = decrypt_wechat_contents($payerPhone, get_provider_config('wechat', $rocket->getParams()));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $decryptPayerPhone is correct as decrypt_wechat_contents(... $rocket->getParams())) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
61
            if (empty($decryptPayerPhone)) {
62
                throw new InvalidConfigException(Exception::DECRYPT_WECHAT_ENCRYPTED_CONTENTS_INVALID, '参数异常: 查询投诉单详情,参数 `payer_phone` 解密失败');
63
            }
64
65
            $destination->set('payer_phone', $decryptPayerPhone);
66
        }
67
68
        Logger::debug('[Wechat][Extend][Complaints][QueryDetailPlugin] 插件后置装载完毕', ['rocket' => $rocket]);
69
70
        return $rocket;
71
    }
72
}
73