Test Setup Failed
Push — master ( da4e0b...40bc14 )
by Php Easy Api
04:17
created

QueryStack::showColumnsFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Migratio\GrammarStructure\Mysql\Traits;
4
5
use Resta\Support\Generator\Generator;
6
use Resta\Support\Utils;
7
8
trait QueryStack
9
{
10
    /**
11
     * @return mixed
12
     */
13
    public function connection()
14
    {
15
        return $this->schema->getConnection();
16
    }
17
18
    public function registerMigration($name)
19
    {
20
        return $this->connection()->query(
21
            "INSERT INTO migrations SET name='".$name."'
22
            ");
23
    }
24
25
    public function checkMigration($name)
26
    {
27
        $query = $this->connection()->query("SELECT * FROM migrations WHERE name='".$name."'")
28
            ->fetchAll();
29
        
30
        return $query;
31
    }
32
33
    /**
34
     * @return mixed
35
     */
36
    public function showTables()
37
    {
38
        $tables = $this->connection()->query('SHOW TABLES')->fetchAll();
39
40
        $list = [];
41
42
        foreach ($tables as $key=>$table) {
43
            $list[] = $table[0];
44
        }
45
46
        return $list;
47
    }
48
49
    /**
50
     * @param $table
51
     * @return mixed
52
     */
53
    public function showColumnsFrom($table)
54
    {
55
        return $this->connection()->query('SHOW FULL COLUMNS FROM '.$table)->fetchAll();
56
    }
57
58
    /**
59
     * get charset for collation
60
     *
61
     * @param $collation
62
     * @return mixed
63
     */
64
    public function getCharsetForCollation($collation)
65
    {
66
        $collation = $this->connection()->query('SHOW COLLATION LIKE \''.$collation.'%\'')->fetchAll();
67
68
        if(isset($collation[0])){
69
            return $collation[0]['Charset'];
70
        }
71
72
        return 'utf8';
73
    }
74
75
    /**
76
     * get table status
77
     *
78
     * @param $table
79
     * @return array
80
     */
81
    public function getTableStatus($table)
82
    {
83
        $status = $this->connection()->query('show table status like \''.$table.'\'')->fetchAll();
84
85
        if(isset($status[0])){
86
            return $status[0];
87
        }
88
89
        return [];
90
    }
91
92
    /**
93
     * get show indexes
94
     *
95
     * @param $table
96
     * @return mixed
97
     */
98
    public function showIndexes($table)
99
    {
100
        return $this->connection()->query('SHOW INDEXES FROM '.$table)->fetchAll();
101
    }
102
103
    /**
104
     * get foreign keys from table
105
     *
106
     * @param $table
107
     * @return array
108
     */
109
    public function getForeignKeys($table)
110
    {
111
        $config = $this->getConfig();
0 ignored issues
show
Bug introduced by
It seems like getConfig() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        /** @scrutinizer ignore-call */ 
112
        $config = $this->getConfig();
Loading history...
112
113
        $database = (isset($config['database'])) ? $config['database'] : null;
114
115
        $query = $this->connection()->query('SELECT rc.MATCH_OPTION,rc.UPDATE_RULE,rc.DELETE_RULE,r.*
116
        FROM information_schema.REFERENTIAL_CONSTRAINTS as rc
117
        LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE as r ON r.TABLE_NAME=rc.TABLE_NAME
118
        WHERE rc.CONSTRAINT_SCHEMA = \''.$database.'\'
119
        AND rc.TABLE_NAME = \''.$table.'\' AND r.REFERENCED_TABLE_NAME is not null')->fetchAll();
120
121
        if(isset($query[0])){
122
            return $query[0];
123
        }
124
125
        return [];
126
    }
127
128
    /**
129
     * set query basic
130
     *
131
     * @param $query
132
     * @return mixed
133
     */
134
    public function setQueryBasic($query)
135
    {
136
        try {
137
138
            $query =$this->connection()->query($query);
139
140
            return [
141
                'result'=>true,
142
                'query'=>$query,
143
                'message'=>null,
144
            ];
145
        }
146
        catch (\PDOException $exception){
147
148
            return [
149
                'result'=>false,
150
                'query'=>$query,
151
                'message'=>$exception->getMessage(),
152
            ];
153
        }
154
155
    }
156
157
    /**
158
     * @param $table
159
     */
160
    public function generateEntity($table)
161
    {
162
        if(in_array($table,$this->showTables()) || in_array($table = strtolower($table),$this->showTables())){
163
164
            $entityDirectory = path()->model().''.DIRECTORY_SEPARATOR.'Entity';
165
166
            if(!file_exists(app()->path()->model())){
167
                files()->makeDirectory(app()->path()->model());
168
            }
169
170
            if(!file_exists($entityDirectory)){
171
                files()->makeDirectory($entityDirectory);
172
            }
173
174
            $columns = $this->showColumnsFrom($table);
175
176
            $list = [];
177
178
            foreach ($columns as $column) {
179
                $list[] = $column['Field'];
180
            }
181
182
            if(!file_exists($entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table))){
183
                $generator = new Generator($entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table),$table.'');
184
                $generator->createClass();
185
            }
186
            else{
187
                $generator = new Generator($entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table),$table.'');
188
            }
189
190
191
            $abstractClassPath = $entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table).''.DIRECTORY_SEPARATOR.'Entity';
192
            $abstractNamespace = Utils::getNamespace($abstractClassPath.''.DIRECTORY_SEPARATOR.''.ucfirst($table).'Abstract');
193
194
            $generator->createClassExtend($abstractNamespace,ucfirst($table).'Abstract');
195
196
            $generator = new Generator($abstractClassPath,$table.'Abstract');
197
198
            $generator->createClass();
199
200
            $method =array_merge([
201
                '__construct'
202
            ],array_merge($list,['__get']));
203
204
            $generator->createMethod($method);
205
206
            $generator->createMethodParameters([
207
                '__construct' => '$query',
208
                '__get' => '$name'
209
            ]);
210
211
            $methodBodyList = [];
212
            $createMethodAccessibleProperty = [];
213
            $createMethodDocument = [];
214
            $createClassDocument = [];
215
216
            foreach ($list as $item) {
217
                $methodBodyList[$item] = 'return self::$query->'.$item.';';
218
                $createClassDocument[] = '@property $this '.$item;
219
                $createMethodAccessibleProperty[$item] = 'protected static';
220
                $createMethodDocument[$item] = [
221
                    '@return mixed'
222
                ];
223
            }
224
225
            $generator->createClassDocument($createClassDocument);
226
227
            $generator->createMethodDocument(array_merge($createMethodDocument,[
228
                '__construct' => [
229
                    ''.$table.' constructor.',
230
                    '@param null|object $query'
231
                ],
232
                '__get' =>[
233
                    'access entity object with magic method',
234
                    '',
235
                    '@param $name',
236
                    '@return mixed'
237
                ]
238
            ]));
239
240
            $createMethodBody = array_merge([
241
                '__construct' => 'self::$query = $query;',
242
                '__get' => 'return static::{$name}();'
243
            ],$methodBodyList);
244
245
            $generator->createMethodBody($createMethodBody);
246
247
            $generator->createMethodAccessibleProperty($createMethodAccessibleProperty);
248
249
250
            $generator->createClassProperty([
251
                'protected static $query;'
252
            ]);
253
254
            $generator->createClassPropertyDocument([
255
                'protected static $query' => [
256
                    '@var object|null'
257
                ]
258
            ]);
259
        }
260
261
    }
262
263
}
264
265