Passed
Push — master ( a1b58e...e12306 )
by Songda
02:52 queued 01:03
created

CreatePlugin::getWechatExtra()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
nop 2
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 CreatePlugin 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
        $payload = $rocket->getPayload();
28
        $params = $this->loadSerialNo($rocket->getParams());
29
30
        $extra = $this->getWechatExtra($params, $payload);
0 ignored issues
show
Bug introduced by
It seems like $payload can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Wec...lugin::getWechatExtra() 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

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