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

QueryBatchIdPlugin::assembly()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 21
rs 9.9332
c 1
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Marketing\Transfer;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\Exception;
10
use Yansongda\Pay\Exception\InvalidParamsException;
11
use Yansongda\Pay\Logger;
12
use Yansongda\Pay\Rocket;
13
use Yansongda\Supports\Collection;
14
15
/**
16
 * @see https://pay.weixin.qq.com/docs/merchant/apis/batch-transfer-to-balance/transfer-batch/get-transfer-batch-by-no.html
17
 */
18
class QueryBatchIdPlugin implements PluginInterface
19
{
20
    /**
21
     * @throws InvalidParamsException
22
     */
23
    public function assembly(Rocket $rocket, Closure $next): Rocket
24
    {
25
        Logger::debug('[Wechat][Marketing][Transfer][QueryBatchIdPlugin] 插件开始装载', ['rocket' => $rocket]);
26
27
        $payload = $rocket->getPayload();
28
        $batchId = $payload?->get('batch_id') ?? null;
29
30
        if (empty($batchId)) {
31
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 通过微信批次单号查询批次单,参数缺少 `batch_id`');
32
        }
33
34
        $rocket->mergePayload(array_merge(
35
            [
36
                '_method' => 'GET',
37
                '_url' => 'v3/transfer/batches/batch-id/'.$batchId.'?'.$this->normal($payload),
38
            ],
39
        ));
40
41
        Logger::info('[Wechat][Marketing][Transfer][QueryBatchIdPlugin] 插件装载完毕', ['rocket' => $rocket]);
42
43
        return $next($rocket);
44
    }
45
46
    protected function normal(?Collection $payload): string
47
    {
48
        return $payload?->query() ?? '';
49
    }
50
}
51