Passed
Pull Request — master (#559)
by Songda
02:24
created

AddReceiverPlugin::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\Profitsharing;
6
7
use Yansongda\Pay\Pay;
8
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
9
use Yansongda\Pay\Rocket;
10
use Yansongda\Pay\Traits\HasWechatEncryption;
11
use Yansongda\Supports\Collection;
12
13
class AddReceiverPlugin extends GeneralPlugin
14
{
15
    use HasWechatEncryption;
16
17
    /**
18
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
19
     * @throws \Yansongda\Pay\Exception\ContainerException
20
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
21
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
22
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
23
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
24
     */
25
    protected function doSomething(Rocket $rocket): void
26
    {
27
        $params = $rocket->getParams();
28
        $config = get_wechat_config($rocket->getParams());
29
        $extra = $this->getWechatId($config, $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\Wec...erPlugin::getWechatId() 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

29
        $extra = $this->getWechatId($config, /** @scrutinizer ignore-type */ $rocket->getPayload());
Loading history...
30
31
        if (!empty($params['receivers'][0]['name'] ?? '')) {
32
            $params = $this->loadSerialNo($params);
33
34
            $rocket->setParams($params);
35
36
            $extra['receivers'] = $this->getEncryptUserName($params);
37
        }
38
39
        $rocket->mergePayload($extra);
40
    }
41
42
    protected function getUri(Rocket $rocket): string
43
    {
44
        return 'v3/profitsharing/receivers/add';
45
    }
46
47
    protected function getWechatId(Collection $config, Collection $payload): array
48
    {
49
        $wechatId = [
50
            'appid' => $config->get('mp_app_id'),
51
        ];
52
53
        if (Pay::MODE_SERVICE == $config->get('mode')) {
54
            $wechatId['sub_mchid'] = $payload->get('sub_mchid', $config->get('sub_mch_id', ''));
55
        }
56
57
        return $wechatId;
58
    }
59
60
    /**
61
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
62
     * @throws \Yansongda\Pay\Exception\ContainerException
63
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
64
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
65
     */
66
    protected function getEncryptUserName(array $params): array
67
    {
68
        $lists = $params['receivers'] ?? [];
69
        $publicKey = $this->getPublicKey($params, $params['_serial_no'] ?? '');
70
71
        foreach ($lists as $key => $list) {
72
            $lists[$key]['name'] = encrypt_wechat_contents($list['name'], $publicKey);
73
        }
74
75
        return $lists;
76
    }
77
}
78