Completed
Push — master ( 54e54a...70f940 )
by Mr
02:22
created

Driver::setDsn()   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
c 0
b 0
f 0
rs 10
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
     * Disconnect from database
46
     *
47
     * @return  DriverInterface
48
     */
49
    abstract public function disconnect(): DriverInterface;
50
51
    /**
52
     * Generate DSN by parameters in config
53
     *
54
     * @param $config
55
     * @return string
56
     */
57
    abstract protected function genDsn($config): string;
58
59
    /**
60
     * Generate DSN by data in config
61
     *
62
     * @return  string
63
     */
64
    public function getDsn(): string
65
    {
66
        // Get all parameters
67
        $config = $this->getConfig()->get();
68
69
        // Get driver of connection
70
        $driver = strtolower($config['driver']);
71
72
        // Generate DSN by parameters in config
73
        $dsn = $this->genDsn($config);
74
75
        return "$driver:$dsn";
76
    }
77
78
    /**
79
     * @param   string $collection
80
     * @return  DriverInterface
81
     */
82
    public function setCollection(string $collection): DriverInterface
83
    {
84
        $this->_collection = $collection;
85
        return $this;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getCollection(): string
92
    {
93
        return $this->_collection;
94
    }
95
96
    /**
97
     * Save current config
98
     *
99
     * @param   ConfigInterface $config
100
     * @return  DriverInterface
101
     */
102
    public function setConfig(ConfigInterface $config): DriverInterface
103
    {
104
        $this->_config = $config;
105
        return $this;
106
    }
107
108
    /**
109
     * Return config object
110
     *
111
     * @return  ConfigInterface
112
     */
113
    public function getConfig(): ConfigInterface
114
    {
115
        return $this->_config;
116
    }
117
118
    /**
119
     * Get some parameter from config by keyname
120
     *
121
     * @param   string $param
122
     * @return  mixed
123
     */
124
    public function getParam(string $param)
125
    {
126
        $result = $this->getConfig()->get($param);
127
        return $result ?? null;
128
    }
129
130
    /**
131
     * Save connection with database via PDO drive
132
     *
133
     * @param   \PDO $connection
134
     * @return  DriverInterface
135
     */
136
    public function setConnection(\PDO $connection): DriverInterface
137
    {
138
        $this->_connection = $connection;
139
        return $this;
140
    }
141
142
    /**
143
     * Get current PDO connection
144
     *
145
     * @return  \PDO
146
     */
147
    public function getConnection(): \PDO
148
    {
149
        return $this->_connection;
150
    }
151
}
152