Completed
Pull Request — master (#5)
by Rougin
04:04
created

DatabaseDriver::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
/**
6
 * Database Driver
7
 *
8
 * A database driver for using available database drivers.
9
 *
10
 * @package  Describe
11
 * @category Driver
12
 * @author   Rougin Royce Gutib <[email protected]>
13
 */
14
class DatabaseDriver implements DriverInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $configuration = [];
20
21
    /**
22
     * @var \Rougin\Describe\Driver\DriverInterface
23
     */
24
    protected $driver;
25
26
    /**
27
     * @param string $driverName
28
     * @param array  $configuration
29
     */
30 30
    public function __construct($driverName, $configuration = [])
31
    {
32 30
        $this->configuration = $configuration;
33 30
        $this->driver        = $this->getDriver($driverName, $configuration);
34 27
    }
35
36
    /**
37
     * Gets the specified driver from the specified database connection.
38
     *
39
     * @param  string $driverName
40
     * @param  array  $configuration
41
     * @return \Rougin\Describe\Driver\DriverInterface
42
     * @throws \Rougin\Describe\Exceptions\DatabaseDriverNotFoundException
43
     */
44 30
    protected function getDriver($driverName, $configuration = [])
45
    {
46 30
        $mysql  = [ 'mysql', 'mysqli' ];
47 30
        $sqlite = [ 'pdo', 'sqlite', 'sqlite3' ];
48
49 30
        list($database, $hostname, $username, $password) = $this->parseConfiguration($configuration);
50
51 30
        if (in_array($driverName, $mysql)) {
52 15
            $dsn = 'mysql:host=' . $hostname . ';dbname=' . $database;
53 15
            $pdo = new \PDO($dsn, $username, $password);
54
55 15
            return new MySQLDriver($pdo, $database);
56 15
        } elseif (in_array($driverName, $sqlite)) {
57 12
            $pdo = new \PDO($hostname);
58
59 12
            return new SQLiteDriver($pdo);
60
        }
61
62 3
        throw new \Rougin\Describe\Exceptions\DatabaseDriverNotFoundException;
63
    }
64
65
    /**
66
     * Returns a listing of columns from the specified table.
67
     *
68
     * @param  string $tableName
69
     * @return array
70
     * @throws \Rougin\Describe\Exceptions\TableNameNotFoundException
71
     */
72 18
    public function getColumns($tableName)
73
    {
74 18
        return $this->driver->getTable($tableName);
75
    }
76
77
    /**
78
     * Returns a listing of columns from the specified table.
79
     * NOTE: To be removed in v2.0.0.
80
     *
81
     * @param  string $tableName
82
     * @return array
83
     * @throws \Rougin\Describe\Exceptions\TableNameNotFoundException
84
     */
85 18
    public function getTable($tableName)
86
    {
87 18
        return $this->getColumns($tableName);
88
    }
89
90
    /**
91
     * Returns a listing of tables from the specified database.
92
     *
93
     * @return array
94
     */
95 9
    public function getTableNames()
96
    {
97 9
        return $this->driver->getTableNames();
98
    }
99
100
    /**
101
     * Shows the list of tables.
102
     * NOTE: To be removed in v2.0.0.
103
     *
104
     * @return array
105
     */
106 9
    public function showTables()
107
    {
108 9
        return $this->getTableNames();
109
    }
110
111
    /**
112
     * Parses the configuration into separate variables.
113
     *
114
     * @param  array  $configuration
115
     * @return array
116
     */
117 30
    protected function parseConfiguration(array $configuration)
118
    {
119 30
        $database = null;
120 30
        $hostname = null;
121 30
        $password = null;
122 30
        $username = null;
123
124 30
        foreach ($configuration as $key => $value) {
125 27
            if (isset($configuration[$key])) {
126 27
                $$key = $configuration[$key];
127 27
            }
128 30
        }
129
130 30
        return [ $database, $hostname, $username, $password ];
131
    }
132
}
133