Passed
Pull Request — master (#662)
by Songda
03:38
created

GeneralPlugin::getUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 3
nc 4
nop 1
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\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
     */
21
    public function assembly(Rocket $rocket, Closure $next): Rocket
22
    {
23
        Logger::info('[unipay][GeneralPlugin] 通用插件开始装载', ['rocket' => $rocket]);
24
25
        $rocket->setRadar($this->getRequest($rocket));
26
        $this->doSomething($rocket);
27
28
        Logger::info('[unipay][GeneralPlugin] 通用插件装载完毕', ['rocket' => $rocket]);
29
30
        return $next($rocket);
31
    }
32
33
    /**
34
     * @throws \Yansongda\Pay\Exception\ContainerException
35
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
36
     */
37
    protected function getRequest(Rocket $rocket): RequestInterface
38
    {
39
        return new Request(
40
            $this->getMethod(),
41
            $this->getUrl($rocket),
42
            $this->getHeaders(),
43
        );
44
    }
45
46
    protected function getMethod(): string
47
    {
48
        return 'POST';
49
    }
50
51
    /**
52
     * @throws \Yansongda\Pay\Exception\ContainerException
53
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
54
     */
55
    protected function getUrl(Rocket $rocket): string
56
    {
57
        $params = $rocket->getParams();
58
59
        $url = Pay::MODE_SERVICE == get_wechat_config($params)->get('mode') ? $this->getPartnerUri($rocket) : $this->getUri($rocket);
60
61
        return 0 === strpos($url, 'http') ? $url : (get_wechat_base_uri($params).$url);
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',
69
            'Content-Type' => 'application/json; charset=utf-8',
70
        ];
71
    }
72
73
    abstract protected function doSomething(Rocket $rocket): void;
74
75
    abstract protected function getUri(Rocket $rocket): string;
76
77
    protected function getPartnerUri(Rocket $rocket): string
78
    {
79
        return $this->getUri($rocket);
80
    }
81
}
82