Completed
Push — master ( 842882...29bedd )
by Mr
01:58
created

Driver::getParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace DrMVC\Database\Drivers;
4
5
use DrMVC\Config\ConfigInterface;
6
7
abstract class Driver implements DriverInterface, QueryInterface
8
{
9
    /**
10
     * @var \PDO
11
     */
12
    protected $_connection;
13
14
    /**
15
     * @var ConfigInterface
16
     */
17
    private $_config;
18
19
    /**
20
     * Name of collection
21
     * @var string
22
     */
23
    private $_collection;
24
25
    /**
26
     * Driver constructor.
27
     *
28
     * @param   ConfigInterface $config object with current configuration
29
     * @param   string $collection name of collection
30
     */
31
    public function __construct(ConfigInterface $config, string $collection)
32
    {
33
        $this->setConfig($config);
34
        $this->setCollection($this->getParam('prefix') . $collection);
35
    }
36
37
    /**
38
     * Initiate connection with database
39
     *
40
     * @return  DriverInterface
41
     */
42
    abstract public function connect(): DriverInterface;
43
44
    /**
45
     * Close database connection
46
     *
47
     * @return  DriverInterface
48
     */
49
    public function disconnect(): DriverInterface
50
    {
51
        $this->setConnection(null);
52
        return $this;
53
    }
54
55
    /**
56
     * Generate DSN by parameters in config
57
     *
58
     * @param $config
59
     * @return string
60
     */
61
    abstract protected function genDsn($config): string;
62
63
    /**
64
     * Generate DSN by data in config
65
     *
66
     * @return  string
67
     */
68
    public function getDsn(): string
69
    {
70
        // Get all parameters
71
        $config = $this->getConfig()->get();
72
73
        // Generate DSN by parameters in config
74
        return $this->genDsn($config);
75
    }
76
77
    /**
78
     * @param   string $collection
79
     * @return  DriverInterface
80
     */
81
    public function setCollection(string $collection): DriverInterface
82
    {
83
        $this->_collection = $collection;
84
        return $this;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getCollection(): string
91
    {
92
        return $this->_collection;
93
    }
94
95
    /**
96
     * Save current config
97
     *
98
     * @param   ConfigInterface $config
99
     * @return  DriverInterface
100
     */
101
    public function setConfig(ConfigInterface $config): DriverInterface
102
    {
103
        $this->_config = $config;
104
        return $this;
105
    }
106
107
    /**
108
     * Return config object
109
     *
110
     * @return  ConfigInterface
111
     */
112
    public function getConfig(): ConfigInterface
113
    {
114
        return $this->_config;
115
    }
116
117
    /**
118
     * Get some parameter from config by keyname
119
     *
120
     * @param   string $param
121
     * @return  mixed
122
     */
123
    public function getParam(string $param)
124
    {
125
        $result = $this->getConfig()->get($param);
126
        return $result ?? null;
127
    }
128
129
    /**
130
     * Save connection with database via driver
131
     *
132
     * @param   mixed $connection
133
     * @return  DriverInterface
134
     */
135
    public function setConnection($connection): DriverInterface
136
    {
137
        $this->_connection = $connection;
138
        return $this;
139
    }
140
141
    /**
142
     * Get current connection
143
     *
144
     * @return  \PDO
145
     */
146
    public function getConnection(): \PDO
147
    {
148
        return $this->_connection;
149
    }
150
}
151