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

DatabaseDriver::getDriver()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

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