Completed
Push — master ( 792f9b...0f2fc9 )
by Sébastien
02:58
created

AbstractSchemaSource::setSchemaSignature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
namespace Soluble\Schema\Source;
3
4
use Soluble\Schema\Exception;
5
use Soluble\DbWrapper\Adapter\AdapterInterface;
6
7
abstract class AbstractSchemaSource implements SchemaSourceInterface
8
{
9
    /**
10
     * Default schema name
11
     * @var string
12
     */
13
    protected $schema;
14
15
16
    /**
17
     * Schema signature
18
     * @var string
19
     */
20
    protected $schemaSignature;
21
22
23
    /**
24
     * @var AdapterInterface
25
     */
26
    protected $adapter;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param AdapterInterface $adapter
32
     * @param string|null $schema default schema, taken from adapter if not given
33
     * @throws Exception\InvalidArgumentException for invalid connection
34
     * @throws Exception\InvalidUsageException thrown if no schema can be found.
35
     */
36 23
    public function __construct(AdapterInterface $adapter, $schema = null)
37
    {
38 23
        $this->adapter = $adapter;
39 23
        if ($schema === null) {
40 23
            $schema = $this->adapter->getConnection()->getCurrentSchema();
41 23
            if ($schema === false || $schema == '') {
42
                $msg = "Database name (schema) parameter missing and no default schema set on connection";
43
                throw new Exception\InvalidUsageException($msg);
44
            }
45 23
        }
46 23
        $this->setDefaultSchema($schema);
47 23
        $this->setSchemaSignature();
48 23
    }
49
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function getColumns($table)
55
    {
56 1
        return array_keys($this->getColumnsInformation($table));
57
    }
58
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function getTableInformation($table)
64
    {
65 1
        $infos = $this->getTablesInformation();
66 1
        return $infos[$table];
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 1
    public function getTables()
73
    {
74 1
        return array_keys($this->getTablesInformation());
75
    }
76
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 1
    public function hasTable($table)
82
    {
83 1
        $tables = $this->getTables();
84 1
        return in_array($table, $tables);
85
    }
86
87
    /**
88
     * Check whether a table parameter is valid and exists
89
     *
90
     * @throws Exception\InvalidArgumentException
91
     * @throws Exception\ErrorException
92
     * @throws Exception\ExceptionInterface
93
     * @throws Exception\TableNotFoundException
94
     *
95
     * @param string $table
96
     * @return AbstractSource
97
    */
98
    protected function validateTable($table)
99
    {
100
        $this->checkTableArgument($table);
101
        if (!$this->hasTable($table)) {
102
            throw new Exception\TableNotFoundException(__METHOD__ . ": Table '$table' does not exists in database '{$this->schema}'");
103
        }
104
        return $this;
105
    }
106
107
108
    /**
109
     * Check whether a schema parameter is valid
110
     *
111
     * @throws Exception\InvalidArgumentException
112
113
     * @param string $schema
114
     * @return AbstractSource
115
     */
116 23
    protected function validateSchema($schema)
117
    {
118 23
        if (!is_string($schema) || trim($schema) == '') {
119 2
            throw new Exception\InvalidArgumentException(__METHOD__ . ": Schema name must be a valid string or an empty string detected");
120
        }
121 23
        return $this;
122
    }
123
124
    /**
125
     * Set default schema
126
     *
127
     * @throws Exception\InvalidArgumentException
128
     * @param string $schema
129
     * @return AbstractSource
130
     */
131 23
    protected function setDefaultSchema($schema)
132
    {
133 23
        $this->validateSchema($schema);
134 23
        $this->schema = $schema;
135 23
        return $this;
136
    }
137
138
    /**
139
     *
140
     * @param string $table
141
     * @throws Exception\InvalidArgumentException
142
     */
143 16
    protected function checkTableArgument($table = null)
144
    {
145 16
        if ($table !== null) {
146 13
            if (!is_string($table) || trim($table) == '') {
147 2
                throw new Exception\InvalidArgumentException(__METHOD__ . " Table name must be a valid string or an empty string detected");
148
            }
149 11
        }
150 14
    }
151
152
    /**
153
     * Return current schema signature for caching
154
     *
155
     * @return void
156
     */
157 23
    protected function setSchemaSignature()
158
    {
159 23
        $host   = $this->adapter->getConnection()->getHost();
160 23
        $schema = $this->schema;
161 23
        $this->schemaSignature = "$host:$schema";
162 23
    }
163
}
164