Passed
Push — master ( 07b934...bffbb7 )
by 世昌
02:21
created

Connector::applyConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\component\database\connector;
3
4
use PDO;
5
use PDOException;
6
use nebula\component\database\exception\ConnectionException;
7
8
/**
9
 * 连接器
10
 */
11
abstract class Connector
12
{
13
    protected static $connetionCount = 0;
14
15
    /**
16
     * PDO实例
17
     *
18
     * @var PDO|null
19
     */
20
    protected $pdo = 0;
21
    /**
22
     * 连接器类型
23
     *
24
     * @var string
25
     */
26
    protected static $type;
27
    /**
28
     * 配置信息
29
     *
30
     * @var string
31
     */
32
    protected $config;
33
    /**
34
     * 应用配置
35
     *
36
     * @param array $config
37
     * @return void
38
     */
39
    public function applyConfig(array $config) {
40
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type array is incompatible with the declared type string of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
    }
42
    /**
43
     * 获取PDO链接描述
44
     *
45
     * @return string
46
     */
47
    abstract public function getDsn():string;
48
49
    /**
50
     * 获取类型
51
     *
52
     * @return string
53
     */
54
    public static function type():string {
55
        return static::$type;
56
    }
57
58
    /**
59
     * 链接数据库
60
     *
61
     * @return void
62
     */
63
    public function connect()
64
    {
65
        // 链接数据库
66
        if (is_null($this->pdo)) {
67
            try {
68
                $this->pdo = new PDO($this->getDsn(), $this->user, $this->password);
0 ignored issues
show
Bug Best Practice introduced by
The property password does not exist on nebula\component\database\connector\Connector. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property user does not exist on nebula\component\database\connector\Connector. Did you maybe forget to declare it?
Loading history...
69
                static::$connetionCount ++;
70
            } catch (PDOException $e) {
71
                throw new ConnectionException($e->getMessage(), $e->getCode());
72
            }
73
        }
74
    }
75
76
    public function getPdo()
77
    {
78
        return $this->pdo;
79
    }
80
81
    /**
82
     * 判断是否连接
83
     *
84
     * @return boolean
85
     */
86
    public function isConnected():bool
87
    {
88
        return $this->pdo !== null;
89
    }
90
}
91