Completed
Push — master ( 7b7ebd...db1323 )
by Sébastien
02:06
created

AbstractSchemaSource::getTableInformation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace Soluble\Schema\Source;
3
4
use Soluble\Schema\Exception;
5
6
abstract class AbstractSchemaSource implements SchemaSourceInterface
7
{
8
    /**
9
     * Default schema name
10
     * @var string
11
     */
12
    protected $schema;
13
14
15
16
    /**
17
     * {@inheritdoc}
18
     */
19 1
    public function getColumns($table)
20
    {
21 1
        return array_keys($this->getColumnsInformation($table));
22
    }
23
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function getTableInformation($table)
29
    {
30 1
        $infos = $this->getTablesInformation();
31 1
        return $infos[$table];
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function getTables()
38
    {
39 1
        return array_keys($this->getTablesInformation());
40
    }
41
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function hasTable($table)
47
    {
48 1
        $tables = $this->getTables();
49 1
        return in_array($table, $tables);
50
    }
51
52
    /**
53
     * Check whether a table parameter is valid and exists
54
     *
55
     * @throws Exception\InvalidArgumentException
56
     * @throws Exception\ErrorException
57
     * @throws Exception\ExceptionInterface
58
     * @throws Exception\TableNotFoundException
59
     *
60
     * @param string $table
61
     * @return AbstractSource
62
    */
63
    protected function validateTable($table)
64
    {
65
        $this->checkTableArgument($table);
66
        if (!$this->hasTable($table)) {
67
            throw new Exception\TableNotFoundException(__METHOD__ . ": Table '$table' does not exists in database '{$this->schema}'");
68
        }
69
        return $this;
70
    }
71
72
73
    /**
74
     * Check whether a schema parameter is valid
75
     *
76
     * @throws Exception\InvalidArgumentException
77
78
     * @param string $schema
79
     * @return AbstractSource
80
     */
81 23
    protected function validateSchema($schema)
82
    {
83 23
        if (!is_string($schema) || trim($schema) == '') {
84 2
            throw new Exception\InvalidArgumentException(__METHOD__ . ": Schema name must be a valid string or an empty string detected");
85
        }
86 23
        return $this;
87
    }
88
89
    /**
90
     * Set default schema
91
     *
92
     * @throws Exception\InvalidArgumentException
93
     * @param string $schema
94
     * @return AbstractSource
95
     */
96 23
    protected function setDefaultSchema($schema)
97
    {
98 23
        $this->validateSchema($schema);
99 23
        $this->schema = $schema;
100 23
        return $this;
101
    }
102
103
    /**
104
     *
105
     * @param string $table
106
     * @throws Exception\InvalidArgumentException
107
     */
108 16
    protected function checkTableArgument($table = null)
109
    {
110 16
        if ($table !== null) {
111 13
            if (!is_string($table) || trim($table) == '') {
112 2
                throw new Exception\InvalidArgumentException(__METHOD__ . " Table name must be a valid string or an empty string detected");
113
            }
114 11
        }
115 14
    }
116
}
117