Completed
Pull Request — master (#5)
by Rougin
02:15
created

AbstractDriver::getColumnsFromQuery()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 10
nc 8
nop 2
crap 3
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
/**
6
 * Abstract Driver
7
 *
8
 * @package  Describe
9
 * @category Driver
10
 * @author   Rougin Royce Gutib <[email protected]>
11
 */
12
abstract class AbstractDriver
13
{
14
    /**
15
     * @var \PDO
16
     */
17
    protected $pdo;
18
19
    /**
20
     * Returns the list of columns based on the specified query.
21
     *
22
     * @param  string $tableName
23
     * @param  string $query
24
     * @return void
25
     */
26 69
    public function getColumnsFromQuery($tableName, $query)
27
    {
28 69
        $columns = [];
29
30
        try {
31 69
            $information = $this->pdo->prepare($query);
32
33 69
            $information->execute();
34 69
            $information->setFetchMode(\PDO::FETCH_OBJ);
35 69
        } catch (\PDOException $e) {
36
            // Table not found
37
        }
38
39 69
        while ($row = $information->fetch()) {
40 63
            array_push($columns, $this->setColumn($tableName, $row));
41 63
        }
42
43 69
        return $columns;
44
    }
45
46
    /**
47
     * Prepares the defined columns.
48
     *
49
     * @param  string $tableName
50
     * @param  mixed  $row
51
     * @return \Rougin\Describe\Column
52
     */
53
    abstract protected function setColumn($tableName, $row);
54
}
55