Passed
Pull Request — master (#1014)
by Songda
03:27 queued 01:20
created

CallbackPlugin::assembly()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 1
nop 2
dl 0
loc 18
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Douyin\V1\Pay;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Direction\NoHttpRequestDirection;
10
use Yansongda\Artful\Exception\ContainerException;
11
use Yansongda\Artful\Exception\InvalidConfigException;
12
use Yansongda\Artful\Exception\ServiceNotFoundException;
13
use Yansongda\Artful\Logger;
14
use Yansongda\Artful\Rocket;
15
use Yansongda\Pay\Exception\InvalidSignException;
16
17
use function Yansongda\Artful\filter_params;
18
use function Yansongda\Pay\get_provider_config;
19
use function Yansongda\Pay\verify_douyin_sign;
20
21
class CallbackPlugin implements PluginInterface
22
{
23
    /**
24
     * @throws ContainerException
25
     * @throws InvalidConfigException
26
     * @throws InvalidSignException
27
     * @throws ServiceNotFoundException
28
     */
29
    public function assembly(Rocket $rocket, Closure $next): Rocket
30
    {
31
        Logger::debug('[Douyin][V1][Pay][CallbackPlugin] 插件开始装载', ['rocket' => $rocket]);
32
33
        $params = $rocket->getParams();
34
        $config = get_provider_config('douyin', $params);
35
36
        $value = filter_params($params, fn ($k, $v) => '' !== $v && 'msg_signature' != $k && 'type' != $k);
37
38
        verify_douyin_sign($config, $value->all(), $params['msg_signature'] ?? '');
39
40
        $rocket->setPayload($params)
41
            ->setDirection(NoHttpRequestDirection::class)
42
            ->setDestination($rocket->getPayload());
43
44
        Logger::info('[Douyin][V1][Pay][CallbackPlugin] 插件装载完毕', ['rocket' => $rocket]);
45
46
        return $next($rocket);
47
    }
48
}
49