Passed
Push — master ( a1bcd2...a1b58e )
by Songda
02:08
created

CreatePlugin::getEncryptUserName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\Fund\Transfer;
6
7
use Yansongda\Pay\Pay;
8
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
9
use Yansongda\Pay\Rocket;
10
use Yansongda\Supports\Config;
11
12
class CreatePlugin extends GeneralPlugin
13
{
14
    /**
15
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
16
     * @throws \Yansongda\Pay\Exception\ContainerException
17
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
18
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
19
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
20
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
21
     */
22
    protected function doSomething(Rocket $rocket): void
23
    {
24
        $params = $rocket->getParams();
25
        $config = get_wechat_config($params);
26
27
        $extra = $this->getWechatId($config);
28
29
        if (!empty($params['transfer_detail_list'][0]['user_name'] ?? '')) {
30
            if (empty($config->get('wechat_public_cert_path'))) {
31
                reload_wechat_public_certs($params);
32
            }
33
34
            if (empty($params['_serial_no'])) {
35
                mt_srand();
36
                $params['_serial_no'] = strval(array_rand($config->get('wechat_public_cert_path')));
0 ignored issues
show
Bug introduced by
It seems like $config->get('wechat_public_cert_path') can also be of type null; however, parameter $array of array_rand() does only seem to accept array, 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
                $params['_serial_no'] = strval(array_rand(/** @scrutinizer ignore-type */ $config->get('wechat_public_cert_path')));
Loading history...
37
                $rocket->setParams($params);
38
            }
39
40
            $extra['transfer_detail_list'] = $this->getEncryptUserName($params);
41
        }
42
43
        $rocket->mergePayload($extra);
44
    }
45
46
    protected function getUri(Rocket $rocket): string
47
    {
48
        return 'v3/transfer/batches';
49
    }
50
51
    protected function getPartnerUri(Rocket $rocket): string
52
    {
53
        return 'v3/partner-transfer/batches';
54
    }
55
56
    protected function getWechatId(Config $config): array
57
    {
58
        $appId = [
59
            'appid' => $config->get('mp_app_id'),
60
        ];
61
62
        if (Pay::MODE_SERVICE == $config->get('mode')) {
63
            $appId = [
64
                'sub_mchid' => $config->get('sub_mch_id', ''),
65
            ];
66
        }
67
68
        return $appId;
69
    }
70
71
    /**
72
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
73
     * @throws \Yansongda\Pay\Exception\ContainerException
74
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
75
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
76
     */
77
    protected function getEncryptUserName(array $params): array
78
    {
79
        $serialNo = $params['_serial_no'] ?? '';
80
        $lists = $params['transfer_detail_list'] ?? [];
81
82
        foreach ($lists as $key => $list) {
83
            $lists[$key]['user_name'] = encrypt_wechat_contents($params, $list['user_name'], $serialNo);
84
        }
85
86
        return $lists;
87
    }
88
}
89