QueryMerchantsPlugin   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 22 2
A normal() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Marketing\Coupon\Stock;
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_provider_config;
19
20
/**
21
 * @see https://pay.weixin.qq.com/docs/merchant/apis/cash-coupons/stock/list-available-merchants.html
22
 * @see https://pay.weixin.qq.com/docs/partner/apis/cash-coupons/stock/list-available-merchants.html
23
 */
24
class QueryMerchantsPlugin implements PluginInterface
25
{
26
    /**
27
     * @throws ContainerException
28
     * @throws InvalidParamsException
29
     * @throws ServiceNotFoundException
30
     */
31
    public function assembly(Rocket $rocket, Closure $next): Rocket
32
    {
33
        Logger::debug('[Wechat][V3][Marketing][Coupon][Stock][QueryMerchantsPlugin] 插件开始装载', ['rocket' => $rocket]);
34
35
        $params = $rocket->getParams();
36
        $config = get_provider_config('wechat', $params);
37
        $payload = $rocket->getPayload();
38
        $stockId = $payload?->get('stock_id') ?? null;
39
40
        if (empty($stockId)) {
41
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 查询代金券可用商户,参数缺少 `stock_id`');
42
        }
43
44
        $rocket->setPayload([
45
            '_method' => 'GET',
46
            '_url' => 'v3/marketing/favor/stocks/'.$stockId.'/merchants?'.$this->normal($payload, $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...rchantsPlugin::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/stocks/'.$stockId.'/merchants?'.$this->normal(/** @scrutinizer ignore-type */ $payload, $config),
Loading history...
47
            '_service_url' => 'v3/marketing/favor/stocks/'.$stockId.'/merchants?'.$this->normal($payload, $config),
48
        ]);
49
50
        Logger::info('[Wechat][V3][Marketing][Coupon][Stock][QueryMerchantsPlugin] 插件装载完毕', ['rocket' => $rocket]);
51
52
        return $next($rocket);
53
    }
54
55
    public function normal(Collection $payload, array $config): string
56
    {
57
        $stockCreatorMchId = $payload->get('stock_creator_mchid');
58
59
        if (is_null($stockCreatorMchId)) {
60
            $payload->set('stock_creator_mchid', $config['mch_id'] ?? '');
61
        }
62
63
        return filter_params($payload)->except('stock_id')->query();
64
    }
65
}
66