Completed
Pull Request — master (#1)
by Rougin
07:32
created

CodeIgniterDriver::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 12

Duplication

Lines 5
Ratio 21.74 %

Code Coverage

Tests 14
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 5
loc 23
ccs 14
cts 16
cp 0.875
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 12
nc 8
nop 1
crap 4.0312
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 18
        $mysql  = [ 'mysql', 'mysqli' ];
29 18
        $sqlite = [ 'pdo', 'sqlite', 'sqlite3' ];
30
31
        // NOTE: To be removed in v1.0.0
32 18
        if (isset($database['default'])) {
33 18
            $database = $database['default'];
34 18
        }
35
36 18
        if (in_array($database['dbdriver'], $mysql)) {
37 9
            $dsn = 'mysql:host=' . $database['hostname'] . ';dbname=' . $database['database'];
38 9
            $pdo = new \PDO($dsn, $database['username'], $database['password']);
39
40
            $this->driver = new MySQLDriver($pdo, $database['database']);
41
        }
42
43 9 View Code Duplication
        if (in_array($database['dbdriver'], $sqlite)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44 9
            $pdo = new \PDO($database['hostname']);
45
46 9
            $this->driver = new SQLiteDriver($pdo);
47 9
        }
48 9
    }
49
50
    /**
51
     * Returns the result.
52
     *
53
     * @return array
54
     */
55 6
    public function getTable($table)
56
    {
57 6
        return $this->driver->getTable($table);
58
    }
59
60
    /**
61
     * Shows the list of tables.
62
     *
63
     * @return array
64
     */
65 3
    public function showTables()
66
    {
67 3
        return $this->driver->showTables();
68
    }
69
}
70