Completed
Push — master ( 5a0a40...7a953d )
by Rougin
02:22
created

CodeIgniterDriver::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
/**
6
 * CodeIgniter Driver
7
 *
8
 * A database driver specifically used for CodeIgniter.
9
 *
10
 * @package  Describe
11
 * @category Driver
12
 * @author   Rougin Royce Gutib <[email protected]>
13
 */
14
class CodeIgniterDriver implements DriverInterface
15
{
16
    /**
17
     * @var \Rougin\Describe\Driver\DriverInterface|null
18
     */
19
    protected $driver = null;
20
21
    /**
22
     * Gets the specified driver from the specified database connection.
23
     *
24
     * @param array $database
25
     */
26 18
    public function __construct(array $database)
27
    {
28
        // NOTE: To be removed in v1.0.0
29 18
        if (isset($database['default'])) {
30 18
            $database = $database['default'];
31 18
        }
32
33 18
        $this->driver = $this->getDriver($database);
34 18
    }
35
36
    /**
37
     * Returns the result.
38
     *
39
     * @return array
40
     */
41 12
    public function getTable($table)
42
    {
43 12
        return $this->driver->getTable($table);
44
    }
45
46
    /**
47
     * Shows the list of tables.
48
     *
49
     * @return array
50
     */
51 6
    public function showTables()
52
    {
53 6
        return $this->driver->showTables();
54
    }
55
56
    /**
57
     * Returns the driver to be used.
58
     *
59
     * @param  array  $database
60
     * @return \Rougin\Describe\Driver\DriverInterface|null
61
     */
62 18
    protected function getDriver(array $database)
63
    {
64 18
        $driver = null;
65 18
        $mysql  = [ 'mysql', 'mysqli' ];
66 18
        $sqlite = [ 'pdo', 'sqlite', 'sqlite3' ];
67
68 18
        if (in_array($database['dbdriver'], $mysql)) {
69 9
            $dsn = 'mysql:host=' . $database['hostname'] . ';dbname=' . $database['database'];
70 9
            $pdo = new \PDO($dsn, $database['username'], $database['password']);
71
72 9
            $driver = new MySQLDriver($pdo, $database['database']);
73 9
        }
74
75 18
        if (in_array($database['dbdriver'], $sqlite)) {
76 9
            $pdo = new \PDO($database['hostname']);
77
78 9
            $driver = new SQLiteDriver($pdo);
79 9
        }
80
81 18
        return $driver;
82
    }
83
}
84