Passed
Pull Request — master (#662)
by Songda
02:09 queued 23s
created

GeneralPlugin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 63
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethod() 0 3 1
A getRequest() 0 6 1
A assembly() 0 10 1
A getHeaders() 0 4 1
A getUrl() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Unipay;
6
7
use Closure;
8
use Psr\Http\Message\RequestInterface;
9
use Yansongda\Pay\Contract\PluginInterface;
10
use Yansongda\Pay\Logger;
11
use Yansongda\Pay\Pay;
12
use Yansongda\Pay\Provider\Unipay;
13
use Yansongda\Pay\Request;
14
use Yansongda\Pay\Rocket;
15
16
abstract class GeneralPlugin implements PluginInterface
17
{
18
    /**
19
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
20
     * @throws \Yansongda\Pay\Exception\ContainerException
21
     */
22
    public function assembly(Rocket $rocket, Closure $next): Rocket
23
    {
24
        Logger::info('[unipay][GeneralPlugin] 通用插件开始装载', ['rocket' => $rocket]);
25
26
        $rocket->setRadar($this->getRequest($rocket));
27
        $this->doSomething($rocket);
28
29
        Logger::info('[unipay][GeneralPlugin] 通用插件装载完毕', ['rocket' => $rocket]);
30
31
        return $next($rocket);
32
    }
33
34
    /**
35
     * @throws \Yansongda\Pay\Exception\ContainerException
36
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
37
     */
38
    protected function getRequest(Rocket $rocket): RequestInterface
39
    {
40
        return new Request(
41
            $this->getMethod(),
42
            $this->getUrl($rocket),
43
            $this->getHeaders(),
44
        );
45
    }
46
47
    protected function getMethod(): string
48
    {
49
        return 'POST';
50
    }
51
52
    /**
53
     * @throws \Yansongda\Pay\Exception\ContainerException
54
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
55
     */
56
    protected function getUrl(Rocket $rocket): string
57
    {
58
        $url = $this->getUri($rocket);
59
60
        if (0 === strpos($url, 'http')) {
61
            return $url;
62
        }
63
64
        $config = get_unipay_config($rocket->getParams());
65
66
        return Unipay::URL[$config['mode'] ?? Pay::MODE_NORMAL].$url;
67
    }
68
69
    protected function getHeaders(): array
70
    {
71
        return [
72
            'User-Agent' => 'yansongda/pay-v3',
73
        ];
74
    }
75
76
    abstract protected function doSomething(Rocket $rocket): void;
77
78
    abstract protected function getUri(Rocket $rocket): string;
79
}
80