Passed
Pull Request — master (#927)
by Songda
02:20
created

GetTitleUrlPlugin::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Marketing\Fapiao;
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_wechat_config;
19
use function Yansongda\Pay\get_wechat_type_key;
20
21
/**
22
 * @see https://pay.weixin.qq.com/docs/merchant/apis/fapiao/user-title/acquire-fapiao-title-url.html
23
 */
24
class GetTitleUrlPlugin 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][Fapiao][GetTitleUrlPlugin] 插件开始装载', ['rocket' => $rocket]);
34
35
        $params = $rocket->getParams();
36
        $payload = $rocket->getPayload();
37
38
        if (empty($payload)) {
39
            throw new InvalidParamsException(Exception::PARAMS_NECESSARY_PARAMS_MISSING, '参数异常: 获取抬头填写链接,缺少必要参数');
40
        }
41
42
        $rocket->setPayload([
43
            '_method' => 'GET',
44
            '_url' => 'v3/new-tax-control-fapiao/user-title/title-url?'.$this->getQuery($payload, $params)->query(),
45
        ]);
46
47
        Logger::info('[Wechat][V3][Marketing][Fapiao][GetTitleUrlPlugin] 插件装载完毕', ['rocket' => $rocket]);
48
49
        return $next($rocket);
50
    }
51
52
    /**
53
     * @throws ContainerException
54
     * @throws ServiceNotFoundException
55
     */
56
    protected function getQuery(Collection $payload, array $params): Collection
57
    {
58
        $config = get_wechat_config($params);
59
60
        return filter_params($payload)->merge([
61
            'appid' => $payload->get('appid', $config[get_wechat_type_key($params)] ?? ''),
62
        ]);
63
    }
64
}
65