Passed
Pull Request — master (#1002)
by
unknown
02:31
created

PrepayPlugin   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 23 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Epay\Pay\Scan;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Exception\ContainerException;
10
use Yansongda\Artful\Exception\InvalidConfigException;
11
use Yansongda\Artful\Exception\ServiceNotFoundException;
12
use Yansongda\Artful\Logger;
13
use Yansongda\Artful\Rocket;
14
use Yansongda\Pay\Exception\Exception;
15
16
use function Yansongda\Pay\get_provider_config;
17
18
class PrepayPlugin implements PluginInterface
19
{
20
    /**
21
     * @throws InvalidConfigException
22
     * @throws ServiceNotFoundException
23
     * @throws ContainerException
24
     */
25
    public function assembly(Rocket $rocket, Closure $next): Rocket
26
    {
27
        Logger::debug('[Epay][Pay][Scan][PrepayPlugin] 插件开始装载', ['rocket' => $rocket]);
28
29
        $backUrl = $rocket->getPayload()['notify_url'] ?? null;
30
        if (empty($backUrl)) {
31
            $params = $rocket->getParams();
32
            $config = get_provider_config('epay', $params);
33
34
            $backUrl = $config['notify_url'] ?? null;
35
        }
36
37
        if (!$backUrl) {
38
            throw new InvalidConfigException(Exception::CONFIG_EPAY_INVALID, 'Missing Epay Config -- [notify_url]');
39
        }
40
        $rocket->mergePayload([
41
            'service' => 'atPay',
42
            'backUrl' => $backUrl,
43
        ]);
44
45
        Logger::info('[Epay][Pay][Scan][PrepayPlugin] 插件装载完毕', ['rocket' => $rocket]);
46
47
        return $next($rocket);
48
    }
49
}
50