Passed
Pull Request — master (#547)
by Songda
03:06
created

HasWechatEncryption::getPublicKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 5
c 1
b 1
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Traits;
6
7
use Yansongda\Pay\Exception\Exception;
8
use Yansongda\Pay\Exception\InvalidParamsException;
9
10
trait HasWechatEncryption
11
{
12
    /**
13
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
14
     * @throws \Yansongda\Pay\Exception\ContainerException
15
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
16
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
17
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
18
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
19
     */
20
    public function loadSerialNo(array $params): array
21
    {
22
        $config = get_wechat_config($params);
23
24
        if (empty($config->get('wechat_public_cert_path'))) {
25
            reload_wechat_public_certs($params);
26
        }
27
28
        if (empty($params['_serial_no'])) {
29
            mt_srand();
30
            $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

30
            $params['_serial_no'] = strval(array_rand(/** @scrutinizer ignore-type */ $config->get('wechat_public_cert_path')));
Loading history...
31
        }
32
33
        return $params;
34
    }
35
36
    /**
37
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
38
     * @throws \Yansongda\Pay\Exception\ContainerException
39
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
40
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
41
     */
42
    public function getPublicKey(array $params, string $serialNo): string
43
    {
44
        $config = get_wechat_config($params);
45
46
        $publicKey = $config->get('wechat_public_cert_path.'.$serialNo);
47
48
        if (empty($publicKey)) {
49
            throw new InvalidParamsException(Exception::WECHAT_SERIAL_NO_NOT_FOUND, 'Wechat serial no not found: '.$serialNo);
50
        }
51
52
        return $publicKey;
53
    }
54
}
55