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

QueryStockMerchantsPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

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

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\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
19
/**
20
 * @see https://pay.weixin.qq.com/docs/merchant/apis/cash-coupons/stock/list-available-merchants.html
21
 * @see https://pay.weixin.qq.com/docs/partner/apis/cash-coupons/stock/list-available-merchants.html
22
 */
23
class QueryStockMerchantsPlugin implements PluginInterface
24
{
25
    /**
26
     * @throws ContainerException
27
     * @throws InvalidParamsException
28
     * @throws ServiceNotFoundException
29
     */
30
    public function assembly(Rocket $rocket, Closure $next): Rocket
31
    {
32
        Logger::debug('[Wechat][Marketing][Coupon][QueryStockMerchantsPlugin] 插件开始装载', ['rocket' => $rocket]);
33
34
        $params = $rocket->getParams();
35
        $config = get_wechat_config($params);
36
        $payload = $rocket->getPayload();
37
        $stockId = $payload?->get('stock_id') ?? null;
38
39
        if (empty($stockId)) {
40
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 查询代金券可用商户,参数缺少 `stock_id`');
41
        }
42
43
        $rocket->setPayload([
44
            '_method' => 'GET',
45
            '_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

45
            '_url' => 'v3/marketing/favor/stocks/'.$stockId.'/merchants?'.$this->normal(/** @scrutinizer ignore-type */ $payload, $config),
Loading history...
46
            '_service_url' => 'v3/marketing/favor/stocks/'.$stockId.'/merchants?'.$this->normal($payload, $config),
47
        ]);
48
49
        Logger::info('[Wechat][Marketing][Coupon][QueryStockMerchantsPlugin] 插件装载完毕', ['rocket' => $rocket]);
50
51
        return $next($rocket);
52
    }
53
54
    public function normal(Collection $payload, array $config): string
55
    {
56
        $stockCreatorMchId = $payload->get('stock_creator_mchid');
57
58
        if (is_null($stockCreatorMchId)) {
59
            $payload->set('stock_creator_mchid', $config['mch_id'] ?? '');
60
        }
61
62
        return $payload->except('stock_id')->query();
63
    }
64
}
65