Passed
Pull Request — master (#756)
by Songda
01:53
created

QueryPlugin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 40
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUri() 0 3 1
A doSomething() 0 3 1
A getPartnerUri() 0 15 3
A getMethod() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Ecommerce\Refund;
6
7
use Yansongda\Pay\Exception\Exception;
8
use Yansongda\Pay\Exception\InvalidParamsException;
9
10
use function Yansongda\Pay\get_wechat_config;
11
12
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
13
use Yansongda\Pay\Rocket;
14
15
/**
16
 * @see https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_6_2.shtml
17
 */
18
class QueryPlugin extends GeneralPlugin
19
{
20
    /**
21
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
22
     */
23
    protected function getUri(Rocket $rocket): string
24
    {
25
        throw new InvalidParamsException(Exception::NOT_IN_SERVICE_MODE);
26
    }
27
28
    /**
29
     * @throws \Yansongda\Pay\Exception\ContainerException
30
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
31
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
32
     */
33
    protected function getPartnerUri(Rocket $rocket): string
34
    {
35
        $payload = $rocket->getPayload();
36
        $config = get_wechat_config($rocket->getParams());
37
        $subMchId = $payload->get('sub_mchid', $config['sub_mch_id'] ?? '');
38
39
        if (!is_null($payload->get('refund_id'))) {
40
            return 'v3/ecommerce/refunds/id/'.$payload->get('refund_id').'?sub_mchid='.$subMchId;
41
        }
42
43
        if (!is_null($payload->get('out_refund_no'))) {
44
            return 'v3/ecommerce/refunds/out-refund-no/'.$payload->get('out_refund_no').'?sub_mchid='.$subMchId;
45
        }
46
47
        throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
48
    }
49
50
    protected function getMethod(): string
51
    {
52
        return 'GET';
53
    }
54
55
    protected function doSomething(Rocket $rocket): void
56
    {
57
        $rocket->setPayload(null);
58
    }
59
}
60