Passed
Pull Request — master (#753)
by Songda
01:52
created

GeneralPlugin::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 4
c 2
b 1
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
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
11
use function Yansongda\Pay\get_wechat_base_uri;
12
use function Yansongda\Pay\get_wechat_config;
13
14
use Yansongda\Pay\Logger;
15
use Yansongda\Pay\Pay;
16
use Yansongda\Pay\Request;
17
use Yansongda\Pay\Rocket;
18
19
abstract class GeneralPlugin implements PluginInterface
20
{
21
    /**
22
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
23
     * @throws \Yansongda\Pay\Exception\ContainerException
24
     */
25
    public function assembly(Rocket $rocket, Closure $next): Rocket
26
    {
27
        Logger::info('[wechat][GeneralPlugin] 通用插件开始装载', ['rocket' => $rocket]);
28
29
        $rocket->setRadar($this->getRequest($rocket));
30
        $this->doSomething($rocket);
31
32
        Logger::info('[wechat][GeneralPlugin] 通用插件装载完毕', ['rocket' => $rocket]);
33
34
        return $next($rocket);
35
    }
36
37
    /**
38
     * @throws \Yansongda\Pay\Exception\ContainerException
39
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
40
     */
41
    protected function getRequest(Rocket $rocket): RequestInterface
42
    {
43
        return new Request(
44
            $this->getMethod(),
45
            $this->getUrl($rocket),
46
            $this->getHeaders(),
47
        );
48
    }
49
50
    protected function getMethod(): string
51
    {
52
        return 'POST';
53
    }
54
55
    /**
56
     * @throws \Yansongda\Pay\Exception\ContainerException
57
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
58
     */
59
    protected function getUrl(Rocket $rocket): string
60
    {
61
        $params = $rocket->getParams();
62
63
        $url = Pay::MODE_SERVICE === (get_wechat_config($params)['mode'] ?? null) ? $this->getPartnerUri($rocket) : $this->getUri($rocket);
64
65
        return 0 === strpos($url, 'http') ? $url : (get_wechat_base_uri($params).$url);
66
    }
67
68
    protected function getHeaders(): array
69
    {
70
        return [
71
            'Accept' => 'application/json, text/plain, application/x-gzip',
72
            'User-Agent' => 'yansongda/pay-v3',
73
            'Content-Type' => 'application/json; charset=utf-8',
74
        ];
75
    }
76
77
    protected function getConfigKey(array $params): string
78
    {
79
        $key = ($params['_type'] ?? 'mp').'_app_id';
80
81
        if ('app_app_id' === $key) {
82
            $key = 'app_id';
83
        }
84
85
        return $key;
86
    }
87
88
    abstract protected function doSomething(Rocket $rocket): void;
89
90
    abstract protected function getUri(Rocket $rocket): string;
91
92
    protected function getPartnerUri(Rocket $rocket): string
93
    {
94
        return $this->getUri($rocket);
95
    }
96
}
97