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

Describe::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Rougin\Describe;
4
5
/**
6
 * Describe
7
 *
8
 * Gets information of a table schema from a database.
9
 *
10
 * @package Describe
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
class Describe
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $columns = [];
19
20
    /**
21
     * @var \Rougin\Describe\Driver\DriverInterface
22
     */
23
    protected $driver;
24
25
    /**
26
     * @param \Rougin\Describe\Driver\DriverInterface $driver
27
     */
28 78
    public function __construct(\Rougin\Describe\Driver\DriverInterface $driver)
29
    {
30 78
        $this->driver = $driver;
31 78
    }
32
33
    /**
34
     * Gets the primary key in the specified table.
35
     *
36
     * @param  string $tableName
37
     * @return string
38
     */
39 12
    public function getPrimaryKey($tableName)
40
    {
41 12
        $result = '';
42
43 12
        if (empty($this->columns)) {
44 12
            $this->columns = $this->driver->getTable($tableName);
45 12
        }
46
47 12
        foreach ($this->columns as $column) {
48 12
            if ($column->isPrimaryKey()) {
49 12
                $result = $column->getField();
50 12
            }
51 12
        }
52
53 12
        return $result;
54
    }
55
56
    /**
57
     * Returns the result.
58
     *
59
     * @param  string $tableName
60
     * @return array
61
     * @throws \Rougin\Describe\Exceptions\TableNameNotFoundException
62
     */
63 54
    public function getTable($tableName)
64 3
    {
65 54
        $table = $this->driver->getTable($tableName);
66
67 54
        if (empty($table) || is_null($table)) {
68 6
            $message = '"' . $tableName . '" table not found in database!';
69
70 6
            throw new Exceptions\TableNameNotFoundException($message);
71
        }
72
73 48
        return $table;
74
    }
75
76
    /**
77
     * Shows the list of tables.
78
     *
79
     * @return array
80
     */
81 12
    public function showTables()
82
    {
83 12
        return $this->driver->showTables();
84
    }
85
86
    /**
87
     * Calls methods from this class in underscore case.
88
     *
89
     * @param  string $method
90
     * @param  mixed  $parameters
91
     * @return mixed
92
     */
93
    public function __call($method, $parameters)
94
    {
95
        return MagicMethodHelper::call($this, $method, $parameters);
96
    }
97
}
98