Completed
Push — master ( 56c44b...57c31b )
by Songda
05:33 queued 11s
created

Functions.php ➔ get_alipay_cert()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use Yansongda\Pay\Contract\ConfigInterface;
6
use Yansongda\Pay\Pay;
7
use Yansongda\Supports\Config;
8
use Yansongda\Supports\Str;
9
10 View Code Duplication
if (!function_exists('get_alipay_config')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
    /**
12
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
13
     * @throws \Yansongda\Pay\Exception\ContainerException
14
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
15
     */
16
    function get_alipay_config(array $params): Config
17
    {
18
        $alipay = Pay::get(ConfigInterface::class)->get('alipay');
19
20
        $config = $params['_config'] ?? 'default';
21
22
        return new Config($alipay[$config] ?? []);
23
    }
24
}
25
26
if (!function_exists('get_alipay_cert')) {
27
    /**
28
     * @return false|resource|string
29
     */
30
    function get_alipay_cert(string $key)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
    {
32
        if (Str::endsWith($key, '.crt')) {
33
            $key = file_get_contents($key);
34
        } elseif (Str::endsWith($key, '.pem')) {
35
            $key = openssl_pkey_get_private(
36
                Str::startsWith($key, 'file://') ? $key : 'file://'.$key
37
            );
38
        } else {
39
            $key = "-----BEGIN RSA PRIVATE KEY-----\n".
40
                wordwrap($key, 64, "\n", true).
41
                "\n-----END RSA PRIVATE KEY-----";
42
        }
43
44
        return $key;
45
    }
46
}
47
48 View Code Duplication
if (!function_exists('get_wechat_config')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    /**
50
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
51
     * @throws \Yansongda\Pay\Exception\ContainerException
52
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
53
     */
54
    function get_wechat_config(array $params): Config
55
    {
56
        $wechat = Pay::get(ConfigInterface::class)->get('wechat');
57
58
        $config = $params['_config'] ?? 'default';
59
60
        return new Config($wechat[$config] ?? []);
61
    }
62
}
63