Passed
Pull Request — master (#472)
by
unknown
02:56
created

GeneralPlugin::isServicePartnerMode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 3
nc 2
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
        return get_wechat_base_uri($rocket->getParams()).
61
            $this->getUri($rocket);
62
    }
63
64
    protected function getHeaders(): array
65
    {
66
        return [
67
            'Accept' => 'application/json, text/plain, application/x-gzip',
68
            'User-Agent' => 'yansongda/pay-v3.0.0',
69
            'Content-Type' => 'application/json; charset=utf-8',
70
        ];
71
    }
72
73
    /**
74
     * 判断是否是服务商模式
75
     */
76
    protected function isServicePartnerMode($config)
77
    {
78
        //服务商自收款模式
79
        if(isset($config['mode']) && $config['mode'] == Pay::MODE_SERVICE) {
80
            return true; 
81
        }
82
        return false;
83
    }
84
85
    abstract protected function doSomething(Rocket $rocket): void;
86
87
    abstract protected function getUri(Rocket $rocket): string;
88
}
89