Test Setup Failed
Push — master ( 4c0917...56b13c )
by Php Easy Api
04:17
created

Pulling::getColumnTransformers()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 1
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Migratio\Resource\PullManager;
4
5
use Migratio\Resource\BaseManager;
6
use Resta\Exception\FileNotFoundException;
7
8
class Pulling extends BaseManager
9
{
10
    /**
11
     * @return void|mixed
12
     *
13
     * @throws FileNotFoundException
14
     */
15
    public function get()
16
    {
17
        $directory = $this->config['paths'][0];
18
        $dbtables = $this->schema->getConnection()->showTables();
19
        $migrations = $this->tableFilters();
20
21
        $list = [];
22
23
        foreach ($migrations as $table=>$item){
24
            $list[] = strtolower($table);
25
        }
26
27
        foreach ($dbtables as $dbtable){
28
            //if(!in_array($dbtable,$list)){
29
30
                $informations = $this->tableInformations($dbtable);
31
32
                $dbtable = ucfirst($dbtable);
33
                $makeDirectory = $directory.''.DIRECTORY_SEPARATOR.''.$dbtable;
34
                files()->makeDirectory($makeDirectory,0755,true);
35
36
                $migrationName = time().'_'.$dbtable.'';
37
38
                $content = $this->getContentFile($this->getStubPath().''.DIRECTORY_SEPARATOR.'pullCreate.stub',[
39
                    'className' => $dbtable,
40
                    'informations' => $informations
41
                ]);
42
43
                files()->put($makeDirectory.''.DIRECTORY_SEPARATOR.''.$migrationName.'.php',$content);
44
            //}
45
        }
46
    }
47
48
    /**
49
     * get table informations
50
     *
51
     * @param $table
52
     * @return string
53
     */
54
    private function tableInformations($table)
55
    {
56
        $foreignKeys = $this->schema->getConnection()->getForeignKeys($table);
57
58
        $columns = $this->schema->getConnection()->showColumnsFrom($table);
59
60
        $status = $this->schema->getConnection()->getTableStatus($table);
61
62
        $indexes = $this->schema->getConnection()->showIndexes($table);
63
        $multipleIndexes = $this->getMultipleIndex($indexes);
64
65
        $list = [];
66
67
        foreach ($columns as $key=>$data){
68
69
            $field      = $data['Field'];
70
            $list[]     = '$wizard->name(\''.$field.'\')';
71
            $list[]     = '->'.$this->getColumnTransformers($data['Type']).'';
72
73
            //default block
74
            if(!is_null($data['Default'])){
75
                $default = $data['Default'];
76
                $list[]     = '->default(\''.$default.'\')';
77
            }
78
79
            $getIndex = $this->getIndexInformation($indexes,$data['Field']);
80
81
            //unique block
82
            if($getIndex['Non_unique']=='0' && $getIndex['Key_name']!=='PRIMARY'){
83
                $indexName = $getIndex['Key_name'];
84
                $list[]     = '->unique(\''.$indexName.'\')';
85
            }
86
87
            //index block
88
            if($getIndex['Non_unique']=='1' && $getIndex['Key_name']!=='PRIMARY'){
89
                $columnName = $getIndex['Column_name'];
0 ignored issues
show
Unused Code introduced by
The assignment to $columnName is dead and can be removed.
Loading history...
90
                $indexName = $getIndex['Key_name'];
91
92
                if(count($multipleIndexes[$indexName])==1){
93
                    $list[] = '->index(\''.$indexName.'\')';
94
                }
95
            }
96
97
            //comment block
98
            if(strlen($data['Comment'])>0){
99
                $comment = $data['Comment'];
100
                $list[] = '->comment(\''.$comment.'\')';
101
            }
102
103
            //auto increment block
104
            if($data['Extra']=='auto_increment'){
105
                $list[] = '->auto_increment()';
106
            }
107
108
            $list[] = ';
109
            ';
110
        }
111
112
        //table collation
113
        $charset = $this->schema->getConnection()->getCharsetForCollation(''.$status['Collation'].'');
114
        $list[] = '$wizard->table()->collation(\''.$charset.'\');
115
        ';
116
117
        //table indexes
118
        foreach ($multipleIndexes as $indexName=>$values) {
119
            if(count($values)>1){
120
                $values = '\''.implode('\',\'',$values).'\'';
121
                $list[] = '    $wizard->table()->indexes(\''.$indexName.'\',['.$values.']);
122
                ';
123
            }
124
        }
125
126
        if(count($foreignKeys)){
127
128
            $constraintName = $foreignKeys['CONSTRAINT_NAME'];
129
            $key = $foreignKeys['COLUMN_NAME'];
130
            $referenceTable = $foreignKeys['REFERENCED_TABLE_NAME'];
131
            $referenceColumn = $foreignKeys['REFERENCED_COLUMN_NAME'];
132
133
            if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){
134
                $list[] = '    $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\');
135
                ';
136
            }
137
138
            if($foreignKeys['UPDATE_RULE']!=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){
139
                $rule = $foreignKeys['UPDATE_RULE'];
140
                $list[] = '    $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onUpdate()->'.strtolower($rule).'();
141
                ';
142
            }
143
144
            if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']!=='RESTRICT'){
145
                $rule = $foreignKeys['DELETE_RULE'];
146
                $list[] = '    $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onDelete()->'.strtolower($rule).'();
147
                ';
148
            }
149
        }
150
151
        return implode('',$list);
152
    }
153
154
    /**
155
     * get column transformers
156
     *
157
     * @param $column
158
     * @return string
159
     */
160
    private function getColumnTransformers($column)
161
    {
162
        if($column=='datetime'){
163
            return 'datetime()';
164
        }
165
        elseif($column=='longtext'){
166
            return 'longtext()';
167
        }
168
        elseif($column=='date'){
169
            return 'date()';
170
        }
171
        elseif(preg_match('@enum.*\((.*?)\)@',$column,$enum)){
172
            return 'enum(['.$enum[1].'])';
173
        }
174
        else{
175
            return $column;
176
        }
177
    }
178
179
    /**
180
     * get index information
181
     *
182
     * @param $index
183
     * @param $field
184
     * @return void|mixed
185
     */
186
    private function getIndexInformation($index,$field)
187
    {
188
        foreach ($index as $key=>$item) {
189
190
            if($item['Column_name'] == $field){
191
                return $index[$key];
192
            }
193
        }
194
195
        return null;
196
    }
197
198
    /**
199
     * get index information
200
     *
201
     * @param $index
202
     * @param $field
203
     * @return void|mixed
204
     */
205
    private function getMultipleIndex($index)
206
    {
207
        $list = [];
208
        foreach ($index as $key=>$item) {
209
            if($item['Non_unique']==1 && $item['Key_name']!=='PRIMARY'){
210
                $list[$item['Key_name']][] = $item['Column_name'];
211
            }
212
        }
213
214
        return $list;
215
    }
216
}