Completed
Push — master ( 5e8a98...722d19 )
by Songda
30s queued 25s
created

GeneralPlugin::getPartnerUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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