Completed
Pull Request — master (#3)
by Rougin
02:43
created

DatabaseDriver::getDriver()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 11
cts 13
cp 0.8462
rs 8.9713
cc 3
eloc 13
nc 3
nop 2
crap 3.0327
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 string
23
     */
24
    protected $driver = '';
25
26
    /**
27
     * @param string $driver
28
     * @param array  $configuration
29
     */
30 18
    public function __construct($driver, $configuration = [])
31
    {
32 18
        $this->driver = $driver;
33 18
        $this->configuration = $configuration;
34 18
    }
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 18
    public function getDriver($driverName, $configuration = [])
45
    {
46 18
        $mysql  = [ 'mysql', 'mysqli' ];
47 18
        $sqlite = [ 'pdo', 'sqlite', 'sqlite3' ];
48
49 18
        list($database, $hostname, $username, $password) = $this->parseConfiguration($configuration);
50
51 18
        if (in_array($driverName, $mysql)) {
52 9
            $dsn = 'mysql:host=' . $hostname . ';dbname=' . $database;
53 9
            $pdo = new \PDO($dsn, $username, $password);
54
55 9
            return new MySQLDriver($pdo, $database);
56
        }
57
58 9
        if (in_array($driverName, $sqlite)) {
59 9
            $pdo = new \PDO($hostname);
60
61 9
            return new SQLiteDriver($pdo);
62
        }
63
64
        $message = 'Specified database driver not found!';
65
66
        throw new \Rougin\Describe\Exceptions\DatabaseDriverNotFoundException($message);
67
    }
68
69
    /**
70
     * Returns the result.
71
     *
72
     * @return array
73
     */
74 12
    public function getTable($table)
75
    {
76 12
        $driver = $this->getDriver($this->driver, $this->configuration);
77
78 12
        return $driver->getTable($table);
79
    }
80
81
    /**
82
     * Shows the list of tables.
83
     *
84
     * @return array
85
     */
86 6
    public function showTables()
87
    {
88 6
        $driver = $this->getDriver($this->driver, $this->configuration);
89
90 6
        return $driver->showTables();
91
    }
92
93
    /**
94
     * Parses the configuration into separate variables.
95
     *
96
     * @param  array  $configuration
97
     * @return array
98
     */
99 18
    protected function parseConfiguration(array $configuration)
100
    {
101 18
        $database = null;
102 18
        $hostname = null;
103 18
        $password = null;
104 18
        $username = null;
105
106 18
        foreach ($configuration as $key => $value) {
107 18
            if (isset($configuration[$key])) {
108 18
                $$key = $configuration[$key];
109 18
            }
110 18
        }
111
112 18
        return [ $database, $hostname, $username, $password ];
113
    }
114
}
115