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

QueryUserCouponsPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 18
c 2
b 0
f 0
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normal() 0 9 2
A assembly() 0 22 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Marketing\Coupon;
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\Rocket;
15
use Yansongda\Supports\Collection;
16
17
use function Yansongda\Pay\get_wechat_config;
18
use function Yansongda\Pay\get_wechat_config_type_key;
19
20
/**
21
 * @see https://pay.weixin.qq.com/docs/merchant/apis/cash-coupons/coupon/list-coupons-by-filter.html
22
 * @see https://pay.weixin.qq.com/docs/partner/apis/cash-coupons/coupon/list-coupons-by-filter.html
23
 */
24
class QueryUserCouponsPlugin 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][Marketing][Coupon][QueryUserCouponsPlugin] 插件开始装载', ['rocket' => $rocket]);
34
35
        $params = $rocket->getParams();
36
        $config = get_wechat_config($params);
37
        $payload = $rocket->getPayload();
38
        $openId = $payload?->get('openid') ?? null;
39
40
        if (empty($openId)) {
41
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 根据商户号查用户的券,参数缺少 `openid`');
42
        }
43
44
        $rocket->setPayload([
45
            '_method' => 'GET',
46
            '_url' => 'v3/marketing/favor/users/'.$openId.'/coupons?'.$this->normal($payload, $params, $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...CouponsPlugin::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

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