Completed
Push — master ( cc0e46...765134 )
by Guillaume
02:18
created

MysqlDatabaseColumn::createStatement()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 8
nop 0
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->extra = $extra;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getCollate()
52
    {
53
        $type = $this->getType();
54
        if (!in_array($type, ['char', 'varchar', 'enum', 'longtext', 'mediumtext', 'text', 'tinytext', 'varchar'], false)) {
55
            return '';
56
        }
57
58
        return $this->collate;
59
    }
60
61
    /**
62
     * @param string $collate
63
     */
64
    public function setCollate($collate)
65
    {
66
        $this->collate = $collate;
67
    }
68
69
    private function setType($type)
70
    {
71
        $type = strtolower($type);
72
        $this->type = $type;
73
    }
74
75
    public function optimizeType()
76
    {
77
        $isEnum = explode('enum', $this->type);
78
        if (!empty($isEnum)) {
79
            $numberElements = substr_count(str_replace(['(', ')', "'",], '', $isEnum[1]), ',') + 1;
80
            if ($numberElements == 2) {
81
                $this->type = 'tinyint';
82
                $this->length = 1;
83
            }
84
        }
85
    }
86
87
88
    /**
89
     * @return string
90
     */
91
    public function getType()
92
    {
93
        return $this->type;
94
    }
95
96
    /**
97
     * @return array
98
     *
99
     * @throws TablenameHasNotDefinedException
100
     */
101
    public function createStatement()
102
    {
103
        $null = $this->getNullable() ? '' : 'NOT';
104
        $default = $this->getDefaultValue() == false ? '' : ' DEFAULT ' . $this->getDefaultValue();
105
        $collate = $this->getCollate() == '' ? '' : sprintf("COLLATE '%s'", $this->getCollate());
106
        $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);
107
108
        return [str_replace(['   ', '  ',], ' ', $modification)];
109
    }
110
111
    /**
112
     * @return mixed
113
     *
114
     * @throws TablenameHasNotDefinedException
115
     */
116
    public function getTable()
117
    {
118
        if (!$this->table) {
119
            $this->critical('You need to define name of your table');
120
            throw new TablenameHasNotDefinedException('table not defined');
121
        }
122
123
        return $this->table;
124
    }
125
126
    /**
127
     * @return mixed
128
     */
129
    public function getNullable()
130
    {
131
        return $this->nullable;
132
    }
133
134
    /**
135
     * @return mixed
136
     */
137
    public function getDefaultValue()
138
    {
139
        return $this->defaultValue;
140
    }
141
142
    public function getName()
143
    {
144
        return $this->name;
145
    }
146
147
    public function getColonneType()
148
    {
149
        $baseType = $this->type;
150
        if (in_array($baseType, ['int', 'mediumint', 'tinyint', 'smallint', 'binary', 'varchar', 'bigint', 'char', 'float'], false)) {
151
            $baseType = $baseType . '(' . $this->length . ')';
152
        }
153
154
        return strtoupper($baseType);
155
    }
156
157
    /**
158
     * @return mixed
159
     */
160
    public function getExtra()
161
    {
162
        return $this->extra;
163
    }
164
165
    /**
166
     * @param mixed $table
167
     */
168
    public function setTable($table)
169
    {
170
        $this->table = $table;
171
    }
172
173
    /**
174
     * @return array
175
     *
176
     * @throws TablenameHasNotDefinedException
177
     */
178
    public function alterStatement()
179
    {
180
        $null = $this->getNullable() ? '' : 'NOT';
181
        $default = $this->getDefaultValue() == false ? '' : ' DEFAULT ' . $this->getDefaultValue();
182
        $columnName = '`' . $this->getName() . '`';
183
        $collate = $this->getCollate() == '' ? '' : sprintf("COLLATE '%s'", $this->getCollate());
184
        $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);
185
186
        return [str_replace(['   ', '  ',], ' ', $modification)];
187
    }
188
}
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...
189