Factory::init()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 2
1
<?php
2
namespace tinymeng\Chinaums;
3
4
define('CHINAUMS_ROOT_PATH', dirname(__DIR__));
5
use Exception;
6
use tinymeng\tools\StringTool;
7
8
/**
9
 * @method static \tinymeng\Chinaums\Provider\Chinaums Chinaums(null|array $config=[]) 银联商务支付
10
 * @method static \tinymeng\Chinaums\Provider\Unionpay Unionpay(null|array $config=[]) 网银支付
11
 * @method static \tinymeng\Chinaums\Provider\Alipay Alipay(null|array $config=[]) 支付宝支付
12
 * @method static \tinymeng\Chinaums\Provider\Wechat Wechat(null|array $config=[]) 微信支付
13
 * @method static \tinymeng\Chinaums\Provider\Contract Contract(null|array $config=[]) 自助签约
14
 */
15
class Factory
16
{
17
18
    /**
19
     * @var array $config
20
     */
21
    protected static $config;
22
23
    /**
24
     * @param $gateway
25
     * @param null|array $config
26
     * @return mixed
27
     * @throws Exception
28
     */
29
    public static function init($gateway, $config=[])
30
    {
31
        $gateway = StringTool::uFirst($gateway);
32
        $class = __NAMESPACE__ . '\\Provider\\' . $gateway;
33
        if (class_exists($class)) {
34
            if(empty(self::$config)) self::config($config);
35
            $objcet = new $class(self::$config);
36
            return $objcet;
37
        } else {
38
            throw new Exception("err:{$class}类不存在");
39
        }
40
    }
41
42
    /**
43
     * @param $config
44
     * @return false|void
45
     */
46
    public static function config($config)
47
    {
48
        $configFile = CHINAUMS_ROOT_PATH."/config/chinaums.php";
49
        if (!file_exists($configFile)) {
50
            return false;
51
        }
52
        $baseConfig = require $configFile;
53
        self::$config = array_replace_recursive($baseConfig,$config);
54
    }
55
56
    /**
57
     * @param $gateway
58
     * @param $config
59
     * @return mixed
60
     * @throws Exception
61
     */
62
    public static function __callStatic($gateway, $config=[])
63
    {
64
        return self::init($gateway, ...$config);
0 ignored issues
show
Bug introduced by
$config is expanded, but the parameter $config of tinymeng\Chinaums\Factory::init() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        return self::init($gateway, /** @scrutinizer ignore-type */ ...$config);
Loading history...
65
    }
66
}
67