Passed
Push — master ( 674b34...f82c08 )
by Guillaume
02:11
created

MysqlDatabaseColumn   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 29
dl 0
loc 194
c 0
b 0
f 0
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A setType() 0 4 1
A setCollate() 0 3 1
A setExtra() 0 3 1
A getNullable() 0 3 1
A toArray() 0 7 1
A getType() 0 3 1
A createStatement() 0 8 4
A optimizeType() 0 8 3
A alterStatement() 0 9 4
A getCollate() 0 8 2
A setTable() 0 3 1
A getExtra() 0 3 1
A getTable() 0 8 2
A getName() 0 3 1
A getColonneType() 0 8 2
A getDefaultValue() 0 3 1
1
<?php
2
3
namespace Starkerxp\DatabaseChecker\Structure;
4
5
6
use Starkerxp\DatabaseChecker\Exception\TablenameHasNotDefinedException;
7
use Starkerxp\DatabaseChecker\LoggerTrait;
8
9
class MysqlDatabaseColumn implements DatabaseInterface
10
{
11
    use LoggerTrait;
12
13
    private $table;
14
    private $name;
15
    private $type;
16
    private $length;
17
    private $nullable;
18
    private $defaultValue;
19
    private $extra;
20
    private $collate;
21
22
    /**
23
     * DatabaseColumnStructure constructor.
24
     *
25
     * @param $name
26
     *
27
     * @param $type
28
     * @param $length
29
     * @param $nullable
30
     * @param $defaultValue
31
     * @param $extra
32
     *
33
     * @throws \RuntimeException
34
     */
35
    public function __construct($name, $type, $length, $nullable, $defaultValue, $extra)
36
    {
37
        if (empty($name)) {
38
            throw new \RuntimeException('');
39
        }
40
        $this->name = $name;
41
        $this->setType($type);
42
        $this->length = $length;
43
        $this->nullable = $nullable;
44
        $this->defaultValue = $defaultValue;
45
        $this->setExtra($extra);
46
    }
47
48
    private function setType($type)
49
    {
50
        $type = strtolower($type);
51
        $this->type = $type;
52
    }
53
54
    /**
55
     * @param mixed $extra
56
     */
57
    public function setExtra($extra)
58
    {
59
        $this->extra = strtoupper($extra);
60
    }
61
62
    public function optimizeType()
63
    {
64
        $isEnum = explode('enum', $this->type);
65
        if (!empty($isEnum)) {
66
            $numberElements = substr_count(str_replace(['(', ')', "'",], '', $isEnum[1]), ',') + 1;
67
            if ($numberElements == 2) {
68
                $this->type = 'tinyint';
69
                $this->length = 1;
70
            }
71
        }
72
    }
73
74
    public function toArray()
75
    {
76
        $tmp = get_object_vars($this);
77
        unset($tmp['logger']);
78
        $tmp['type'] = strtoupper($tmp['type']);
79
80
        return $tmp;
81
    }
82
83
    /**
84
     * @return array
85
     *
86
     * @throws TablenameHasNotDefinedException
87
     */
88
    public function createStatement()
89
    {
90
        $null = $this->getNullable() ? '' : 'NOT';
91
        $default = $this->getDefaultValue() == false ? '' : ' DEFAULT ' . $this->getDefaultValue();
92
        $collate = $this->getCollate() == '' ? '' : sprintf("COLLATE '%s'", $this->getCollate());
93
        $modification = sprintf('ALTER TABLE `%s` ADD COLUMN `%s` %s %s NULL %s %s %s;', $this->getTable(), $this->getName(), $this->getColonneType(), $null, $default, $this->getExtra(), $collate);
94
95
        return [str_replace(['   ', '  ',], ' ', $modification)];
96
    }
97
98
    /**
99
     * @return mixed
100
     */
101
    public function getNullable()
102
    {
103
        return $this->nullable;
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function getDefaultValue()
110
    {
111
        return $this->defaultValue;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getCollate()
118
    {
119
        $type = $this->getType();
120
        if (!in_array($type, ['char', 'varchar', 'enum', 'longtext', 'mediumtext', 'text', 'tinytext', 'varchar'], false)) {
121
            return '';
122
        }
123
124
        return $this->collate;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getType()
131
    {
132
        return $this->type;
133
    }
134
135
    /**
136
     * @return mixed
137
     *
138
     * @throws TablenameHasNotDefinedException
139
     */
140
    public function getTable()
141
    {
142
        if (!$this->table) {
143
            $this->critical('You need to define name of your table');
144
            throw new TablenameHasNotDefinedException('table not defined');
145
        }
146
147
        return $this->table;
148
    }
149
150
    public function getName()
151
    {
152
        return $this->name;
153
    }
154
155
    public function getColonneType()
156
    {
157
        $baseType = $this->type;
158
        if (in_array($baseType, ['int', 'mediumint', 'tinyint', 'smallint', 'binary', 'varchar', 'bigint', 'char', 'float'], false)) {
159
            $baseType = $baseType . '(' . $this->length . ')';
160
        }
161
162
        return strtoupper($baseType);
163
    }
164
165
    /**
166
     * @return mixed
167
     */
168
    public function getExtra()
169
    {
170
        return $this->extra;
171
    }
172
173
    /**
174
     * @param mixed $table
175
     */
176
    public function setTable($table)
177
    {
178
        $this->table = $table;
179
    }
180
181
    /**
182
     * @param string $collate
183
     */
184
    public function setCollate($collate)
185
    {
186
        $this->collate = $collate;
187
    }
188
189
    /**
190
     * @return array
191
     *
192
     * @throws TablenameHasNotDefinedException
193
     */
194
    public function alterStatement()
195
    {
196
        $null = $this->getNullable() ? '' : 'NOT';
197
        $default = $this->getDefaultValue() == false ? '' : ' DEFAULT ' . $this->getDefaultValue();
198
        $columnName = '`' . $this->getName() . '`';
199
        $collate = $this->getCollate() == '' ? '' : sprintf("COLLATE '%s'", $this->getCollate());
200
        $modification = sprintf('ALTER TABLE `%s` CHANGE COLUMN %s %s %s %s NULL %s %s %s;', $this->getTable(), $columnName, $columnName, $this->getColonneType(), $null, $default, $this->getExtra(), $collate);
201
202
        return [str_replace(['   ', '  ',], ' ', $modification)];
203
    }
204
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
205