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

QueryMerchantConfigsPlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 12
c 1
b 0
f 0
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 20 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Extend\ProfitSharing;
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\Pay;
15
use Yansongda\Pay\Rocket;
16
17
use function Yansongda\Pay\get_wechat_config;
18
19
/**
20
 * @see https://pay.weixin.qq.com/docs/partner/apis/profit-sharing/merchants/query-merchant-ratio.html
21
 */
22
class QueryMerchantConfigsPlugin implements PluginInterface
23
{
24
    /**
25
     * @throws ContainerException
26
     * @throws InvalidParamsException
27
     * @throws ServiceNotFoundException
28
     */
29
    public function assembly(Rocket $rocket, Closure $next): Rocket
30
    {
31
        Logger::debug('[Wechat][Extend][ProfitSharing][QueryMerchantConfigsPlugin] 插件开始装载', ['rocket' => $rocket]);
32
33
        $payload = $rocket->getPayload();
34
        $config = get_wechat_config($rocket->getParams());
35
        $subMchId = $payload?->get('sub_mch_id') ?? $config['sub_mch_id'] ?? 'null';
36
37
        if (Pay::MODE_NORMAL === ($config['mode'] ?? Pay::MODE_NORMAL)) {
38
            throw new InvalidParamsException(Exception::PARAMS_PLUGIN_ONLY_SUPPORT_SERVICE_MODE, '参数异常: 查询最大分账比例,只支持服务商模式,当前配置为普通商户模式');
39
        }
40
41
        $rocket->setPayload([
42
            '_method' => 'GET',
43
            '_service_url' => 'v3/profitsharing/merchant-configs/'.$subMchId,
44
        ]);
45
46
        Logger::info('[Wechat][Extend][ProfitSharing][QueryMerchantConfigsPlugin] 插件装载完毕', ['rocket' => $rocket]);
47
48
        return $next($rocket);
49
    }
50
}
51