Completed
Push — master ( 4802b7...673fc6 )
by Oleg
06:20 queued 02:07
created

Connection::getDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php /** ConnectionMicro */
2
3
namespace Micro\Db\Drivers;
4
5
use Micro\Base\Exception;
6
use Micro\Db\Adapter;
7
8
/**
9
 * Threads class file.
10
 *
11
 * @author Oleg Lunegov <[email protected]>
12
 * @link https://github.com/linpax/microphp-framework
13
 * @copyright Copyright (c) 2013 Oleg Lunegov
14
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
15
 * @package Micro
16
 * @subpackage Db\Drivers
17
 * @version 1.0
18
 * @since 1.0
19
 */
20
class Connection implements Adapter
21
{
22
    /** @var IDriver $driver */
23
    private $driver;
24
25
26
    /**
27
     * Make connection to database with PDO driver
28
     *
29
     * @access public
30
     *
31
     * @param string $dsn DSN connection string
32
     * @param array $config Configuration of connection
33
     * @param array $options Other options
34
     *
35
     * @result void
36
     * @throws Exception
37
     */
38
    public function __construct($dsn, array $config = [], array $options = [])
39
    {
40
        $this->setDriver($dsn, $config, $options);
41
    }
42
43
    /**
44
     * Get DB driver
45
     *
46
     * @access public
47
     * @return IDriver
48
     */
49
    public function getDriver()
50
    {
51
        return $this->driver;
52
    }
53
54
    /**
55
     * Set active connection driver
56
     *
57
     * @access public
58
     *
59
     * @param string $dsn DSN connection string
60
     * @param array $config Configuration of connection
61
     * @param array $options Other options
62
     *
63
     * @return void
64
     * @throws Exception
65
     */
66
    public function setDriver($dsn, array $config = [], array $options = [])
67
    {
68
        $class = '\Micro\Db\Drivers\\'.ucfirst(substr($dsn, 0, strpos($dsn, ':'))).'Driver';
69
70
        if (!class_exists($class)) {
71
            throw new Exception('DB driver `'.$class.'` not supported');
72
        }
73
74
        unset($this->driver);
75
76
        $this->driver = new $class($dsn, $config, $options);
77
    }
78
}
79