Passed
Push — master ( 013b57...d213bb )
by Songda
02:56
created

V3/Marketing/Coupon/Coupons/QueryUserPlugin.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Marketing\Coupon\Coupons;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Exception\ContainerException;
10
use Yansongda\Artful\Exception\InvalidParamsException;
11
use Yansongda\Artful\Exception\ServiceNotFoundException;
12
use Yansongda\Artful\Logger;
13
use Yansongda\Artful\Rocket;
14
use Yansongda\Pay\Exception\Exception;
15
use Yansongda\Supports\Collection;
16
17
use function Yansongda\Artful\filter_params;
18
use function Yansongda\Pay\get_wechat_config;
19
use function Yansongda\Pay\get_wechat_type_key;
20
21
/**
22
 * @see https://pay.weixin.qq.com/docs/merchant/apis/cash-coupons/coupon/list-coupons-by-filter.html
23
 * @see https://pay.weixin.qq.com/docs/partner/apis/cash-coupons/coupon/list-coupons-by-filter.html
24
 */
25
class QueryUserPlugin implements PluginInterface
26
{
27
    /**
28
     * @throws InvalidParamsException
29
     * @throws ContainerException
30
     * @throws ServiceNotFoundException
31
     */
32
    public function assembly(Rocket $rocket, Closure $next): Rocket
33
    {
34
        Logger::debug('[Wechat][V3][Marketing][Coupon][Coupons][QueryUserPlugin] 插件开始装载', ['rocket' => $rocket]);
35
36
        $params = $rocket->getParams();
37
        $config = get_wechat_config($params);
38
        $payload = $rocket->getPayload();
39
        $openId = $payload?->get('openid') ?? null;
40
41
        if (empty($openId)) {
42
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 根据商户号查用户的券,参数缺少 `openid`');
43
        }
44
45
        $rocket->setPayload([
46
            '_method' => 'GET',
47
            '_url' => 'v3/marketing/favor/users/'.$openId.'/coupons?'.$this->normal($payload, $params, $config),
0 ignored issues
show
It seems like $payload can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Wec...eryUserPlugin::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

47
            '_url' => 'v3/marketing/favor/users/'.$openId.'/coupons?'.$this->normal(/** @scrutinizer ignore-type */ $payload, $params, $config),
Loading history...
48
            '_service_url' => 'v3/marketing/favor/users/'.$openId.'/coupons?'.$this->normal($payload, $params, $config),
49
        ]);
50
51
        Logger::info('[Wechat][V3][Marketing][Coupon][Coupons][QueryUserPlugin] 插件装载完毕', ['rocket' => $rocket]);
52
53
        return $next($rocket);
54
    }
55
56
    protected function normal(Collection $payload, array $params, array $config): string
57
    {
58
        $appId = $payload->get('appid');
59
60
        if (is_null($appId)) {
61
            $payload->set('appid', $config[get_wechat_type_key($params)] ?? '');
62
        }
63
64
        return filter_params($payload)->except('openid')->query();
65
    }
66
}
67