Passed
Pull Request — master (#894)
by Songda
01:58
created

AddRadarPlugin::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
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\Alipay;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\ContainerException;
10
use Yansongda\Pay\Exception\ServiceNotFoundException;
11
use Yansongda\Pay\Logger;
12
use Yansongda\Pay\Pay;
13
use Yansongda\Pay\Provider\Alipay;
14
use Yansongda\Pay\Request;
15
use Yansongda\Pay\Rocket;
16
use Yansongda\Supports\Collection;
17
18
use function Yansongda\Pay\get_alipay_config;
19
20
class AddRadarPlugin implements PluginInterface
21
{
22
    /**
23
     * @throws ContainerException
24
     * @throws ServiceNotFoundException
25
     */
26
    public function assembly(Rocket $rocket, Closure $next): Rocket
27
    {
28
        Logger::debug('[Alipay][AddRadarPlugin] 插件开始装载', ['rocket' => $rocket]);
29
30
        $params = $rocket->getParams();
31
32
        $rocket->setRadar(new Request(
33
            $this->getMethod($params),
34
            $this->getUrl($params),
35
            $this->getHeaders(),
36
            $this->getBody($rocket->getPayload()),
0 ignored issues
show
Bug introduced by
It seems like $rocket->getPayload() can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Ali...dRadarPlugin::getBody() does only seem to accept Yansongda\Supports\Collection, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
            $this->getBody(/** @scrutinizer ignore-type */ $rocket->getPayload()),
Loading history...
37
        ));
38
39
        Logger::info('[Alipay][AddRadarPlugin] 插件装载完毕', ['rocket' => $rocket]);
40
41
        return $next($rocket);
42
    }
43
44
    protected function getMethod(array $params): string
45
    {
46
        return strtoupper($params['_method'] ?? 'POST');
47
    }
48
49
    /**
50
     * @throws ContainerException
51
     * @throws ServiceNotFoundException
52
     */
53
    protected function getUrl(array $params): string
54
    {
55
        $config = get_alipay_config($params);
56
57
        return Alipay::URL[$config['mode'] ?? Pay::MODE_NORMAL];
58
    }
59
60
    protected function getHeaders(): array
61
    {
62
        return [
63
            'Content-Type' => 'application/x-www-form-urlencoded',
64
            'User-Agent' => 'yansongda/pay-v3',
65
        ];
66
    }
67
68
    protected function getBody(Collection $payload): string
69
    {
70
        return $payload->query();
71
    }
72
}
73