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

CodeIgniterDriver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 72
ccs 20
cts 22
cp 0.9091
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getTable() 0 4 1
A showTables() 0 4 1
A getDriver() 0 22 3
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 v2.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
     * @throws \Rougin\Describe\Exceptions\DatabaseDriverNotFoundException
62
     */
63 18
    protected function getDriver(array $database)
64
    {
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
            return new MySQLDriver($pdo, $database['database']);
73
        }
74
75 9
        if (in_array($database['dbdriver'], $sqlite)) {
76 9
            $pdo = new \PDO($database['hostname']);
77
78 9
            return new SQLiteDriver($pdo);
79
        }
80
81
        $message = 'Specified database driver not found!';
82
83
        throw new \Rougin\Describe\Exceptions\DatabaseDriverNotFoundException($message);
84
    }
85
}
86