Passed
Pull Request — master (#479)
by Songda
02:04
created

GeneralPlugin   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
eloc 20
c 2
b 1
f 0
dl 0
loc 68
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaders() 0 6 1
A getMethod() 0 3 1
A getRequest() 0 6 1
A assembly() 0 10 1
A getUrl() 0 7 2
A getPartnerUri() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat;
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\Request;
13
use Yansongda\Pay\Rocket;
14
15
abstract class GeneralPlugin implements PluginInterface
16
{
17
    /**
18
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
19
     * @throws \Yansongda\Pay\Exception\ContainerException
20
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
21
     */
22
    public function assembly(Rocket $rocket, Closure $next): Rocket
23
    {
24
        Logger::info('[wechat][GeneralPlugin] 通用插件开始装载', ['rocket' => $rocket]);
25
26
        $rocket->setRadar($this->getRequest($rocket));
27
        $this->doSomething($rocket);
28
29
        Logger::info('[wechat][GeneralPlugin] 通用插件装载完毕', ['rocket' => $rocket]);
30
31
        return $next($rocket);
32
    }
33
34
    /**
35
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
36
     * @throws \Yansongda\Pay\Exception\ContainerException
37
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
38
     */
39
    protected function getRequest(Rocket $rocket): RequestInterface
40
    {
41
        return new Request(
42
            $this->getMethod(),
43
            $this->getUrl($rocket),
44
            $this->getHeaders(),
45
        );
46
    }
47
48
    protected function getMethod(): string
49
    {
50
        return 'POST';
51
    }
52
53
    /**
54
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
55
     * @throws \Yansongda\Pay\Exception\ContainerException
56
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
57
     */
58
    protected function getUrl(Rocket $rocket): string
59
    {
60
        $params = $rocket->getParams();
61
        $mode = get_wechat_config($params)->get('mode');
62
63
        return get_wechat_base_uri($params).
64
            (Pay::MODE_SERVICE == $mode ? $this->getPartnerUri($rocket) : $this->getUri($rocket));
65
    }
66
67
    protected function getHeaders(): array
68
    {
69
        return [
70
            'Accept' => 'application/json, text/plain, application/x-gzip',
71
            'User-Agent' => 'yansongda/pay-v3.0',
72
            'Content-Type' => 'application/json; charset=utf-8',
73
        ];
74
    }
75
76
    abstract protected function doSomething(Rocket $rocket): void;
77
78
    abstract protected function getUri(Rocket $rocket): string;
79
80
    protected function getPartnerUri(Rocket $rocket): string
81
    {
82
        return $this->getUri($rocket);
83
    }
84
}
85