DatabaseDriver::getTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
use Rougin\Describe\Exceptions\DriverNotFoundException;
6
7
/**
8
 * Database Driver
9
 *
10
 * A database driver for using available database drivers.
11
 *
12
 * @package Describe
13
 * @author  Rougin Gutib <[email protected]>
14
 */
15
class DatabaseDriver implements DriverInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $data = array();
21
22
    /**
23
     * @var \Rougin\Describe\Driver\DriverInterface
24
     */
25
    protected $driver;
26
27
    /**
28
     * @var array
29
     */
30
    protected $mysql = array('mysql', 'mysqli');
31
32
    /**
33
     * @var array
34
     */
35
    protected $sqlite = array('pdo', 'sqlite', 'sqlite3');
36
37
    /**
38
     * Initializes the driver instance.
39
     *
40
     * @param string $name
41
     * @param array  $data
42
     */
43 33
    public function __construct($name, $data = array())
44
    {
45 33
        $this->data = $data;
46
47 33
        $this->driver = $this->driver($name, $data);
48 33
    }
49
50
    /**
51
     * Returns an array of Column instances from a table.
52
     *
53
     * @param  string $table
54
     * @return \Rougin\Describe\Column[]
55
     */
56 42
    public function columns($table)
57
    {
58 42
        return $this->driver->columns($table);
59
    }
60
61
    /**
62
     * Returns an array of Column instances from a table.
63
     * NOTE: To be removed in v2.0.0. Use columns() instead.
64
     *
65
     * @param  string $table
66
     * @return \Rougin\Describe\Column[]
67
     */
68 18
    public function getColumns($table)
69
    {
70 18
        return $this->columns($table);
71
    }
72
73
    /**
74
     * Returns an array of Column instances from a table.
75
     * NOTE: To be removed in v2.0.0. Use getColumns() instead.
76
     *
77
     * @param  string $table
78
     * @return \Rougin\Describe\Column[]
79
     */
80 6
    public function getTable($table)
81
    {
82 6
        return $this->getColumns($table);
83
    }
84
85
    /**
86
     * Returns an array of table names.
87
     * NOTE: To be removed in v2.0.0. Use tables() instead.
88
     *
89
     * @return array
90
     */
91 12
    public function getTableNames()
92
    {
93 12
        return $this->tables();
94
    }
95
96
    /**
97
     * Returns an array of table names.
98
     * NOTE: To be removed in v2.0.0. Use getTableNames() instead.
99
     *
100
     * @return array
101
     */
102 6
    public function showTables()
103
    {
104 6
        return $this->getTableNames();
105
    }
106
107
    /**
108
     * Returns an array of Table instances.
109
     *
110
     * @return \Rougin\Describe\Table[]
111
     */
112 18
    public function tables()
113
    {
114 18
        return $this->driver->tables();
115
    }
116
117
    /**
118
     * Returns the Driver instance from the configuration.
119
     *
120
     * @param  string $name
121
     * @param  array  $data
122
     * @return \Rougin\Describe\Driver\DriverInterface
123
     *
124
     * @throws \Rougin\Describe\Exceptions\DriverNotFoundException
125
     */
126 63
    protected function driver($name, $data = array())
127
    {
128 63
        if (in_array($name, $this->mysql) === true) {
129 30
            $dsn = (string) 'mysql:host=%s;dbname=%s';
130
131 30
            $dsn = sprintf($dsn, $data['hostname'], $data['database']);
132
133 30
            $pdo = new \PDO($dsn, $data['username'], $data['password']);
134
135 30
            return new MySQLDriver($pdo, (string) $data['database']);
136
        }
137
138 33
        if (in_array($name, $this->sqlite)) {
139 33
            return new SQLiteDriver(new \PDO($data['hostname']));
140
        }
141
142 3
        $message = 'Database driver "%s" not found.';
143
144 3
        $message = sprintf($message, (string) $name);
145
146 3
        throw new DriverNotFoundException($message);
147
    }
148
}
149