Completed
Push — master ( ec2154...751b89 )
by Rougin
04:27
created

DatabaseDriver::getDriver()   D

Complexity

Conditions 10
Paths 96

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 31
cts 31
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 28
nc 96
nop 2
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
use PDO;
6
7
/**
8
 * Database Driver
9
 *
10
 * A database driver for using available database drivers.
11
 *
12
 * @package  Describe
13
 * @category Driver
14
 * @author   Rougin Royce Gutib <[email protected]>
15
 */
16
class DatabaseDriver implements DriverInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $configuration = [];
22
23
    /**
24
     * @var string
25
     */
26
    protected $driver = '';
27
28
    /**
29
     * @param string $driver
30
     * @param array  $configuration
31
     */
32 18
    public function __construct($driver, $configuration = [])
33
    {
34 18
        $this->driver = $driver;
35 18
        $this->configuration = $configuration;
36 18
    }
37
38
    /**
39
     * Gets the specified driver from the specified database connection.
40
     *
41
     * @param string $driverName
42
     * @param array  $configuration
43
     * @return \Rougin\Describe\Driver\DriverInterface|null
44
     */
45 18
    public function getDriver($driverName, $configuration = [])
46
    {
47 18
        $driver   = null;
48 18
        $database = null;
49 18
        $hostname = null;
50 18
        $password = null;
51 18
        $username = null;
52
53 18
        if (isset($configuration['database'])) {
54 9
            $database = $configuration['database'];
55 9
        }
56
57 18
        if (isset($configuration['hostname'])) {
58 18
            $hostname = $configuration['hostname'];
59 18
        }
60
61 18
        if (isset($configuration['password'])) {
62 9
            $password = $configuration['password'];
63 9
        }
64
65 18
        if (isset($configuration['username'])) {
66 9
            $username = $configuration['username'];
67 9
        }
68
69
        switch ($driverName) {
70 18
            case 'mysql':
71 18
            case 'mysqli':
72 9
                $dsn    = 'mysql:host=' . $hostname . ';dbname=' . $database;
73 9
                $pdo    = new PDO($dsn, $username, $password);
74 9
                $driver = new MySQLDriver($pdo, $database);
75
76 9
                break;
77 9
            case 'pdo':
78 9
            case 'sqlite':
79 9
            case 'sqlite3':
80 9
                $pdo    = new PDO($hostname);
81 9
                $driver = new SQLiteDriver($pdo);
82
83 9
                break;
84
        }
85
86 18
        return $driver;
87
    }
88
89
    /**
90
     * Returns the result.
91
     *
92
     * @return array
93
     */
94 12
    public function getTable($table)
95
    {
96 12
        $driver = $this->getDriver($this->driver, $this->configuration);
97
98 12
        return $driver->getTable($table);
99
    }
100
101
    /**
102
     * Shows the list of tables.
103
     *
104
     * @return array
105
     */
106 6
    public function showTables()
107
    {
108 6
        $driver = $this->getDriver($this->driver, $this->configuration);
109
110 6
        return $driver->showTables();
111
    }
112
}
113