Completed
Push — master ( 99696f...35c3f5 )
by Rougin
02:37
created

AbstractDriver::exception()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
use Rougin\Describe\Column;
6
use Rougin\Describe\Exceptions\TableNotFoundException;
7
8
/**
9
 * Abstract Driver
10
 *
11
 * @package  Describe
12
 * @author   Rougin Royce Gutib <[email protected]>
13
 */
14
abstract class AbstractDriver
15
{
16
    /**
17
     * @var \PDO
18
     */
19
    protected $pdo;
20
21
    /**
22
     * Returns the list of columns based on a query.
23
     *
24
     * @param  string $table
25
     * @param  string $query
26 69
     * @param  array  $columns
27
     * @return \Rougin\Describe\Column[]
28 69
     */
29
    protected function query($table, $query, $columns = array())
30
    {
31 69
        try {
32
            $result = $this->pdo->prepare($query);
33 69
34 69
            $result->execute();
35 69
36
            $result->setFetchMode(\PDO::FETCH_OBJ);
37
        } catch (\PDOException $error) {
38
            $this->exception($table, $error->getMessage());
39 69
        }
40 63
41 63
        while ($row = $result->fetch()) {
42
            $column = $this->column(new Column, $table, $row);
43 69
44
            array_push($columns, $column);
45
        }
46
47
        empty($columns) && $this->exception($table);
48
49
        return $columns;
50
    }
51
52
    /**
53
     * Throws a TableNotFoundException.
54
     *
55
     * @param string      $table
56
     * @param string|null $text
57
     *
58
     * @throws \Rougin\Describe\Exceptions\TableNotFoundException
59
     */
60
    protected function exception($table, $text = null)
61
    {
62
        $message = 'Table "' . $table . '" not found!';
63
64
        $text !== null && $message = $text;
65
66
        throw new TableNotFoundException($message);
67
    }
68
}
69