Test Setup Failed
Push — master ( 7e4be1...55ee1d )
by Php Easy Api
04:25
created

QueryStack::generateEntity()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 91
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 54
c 0
b 0
f 0
nc 17
nop 1
dl 0
loc 91
rs 8.0703

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /**
19
     * @return mixed
20
     */
21
    public function showTables()
22
    {
23
       $tables = $this->connection()->query('SHOW TABLES')->fetchAll();
24
25
       $list = [];
26
27
        foreach ($tables as $key=>$table) {
28
            $list[] = $table[0];
29
       }
30
31
        return $list;
32
    }
33
34
    /**
35
     * @param $table
36
     * @return mixed
37
     */
38
    public function showColumnsFrom($table)
39
    {
40
        return $this->connection()->query('SHOW FULL COLUMNS FROM '.$table)->fetchAll();
41
    }
42
43
    /**
44
     * get charset for collation
45
     *
46
     * @param $collation
47
     * @return mixed
48
     */
49
    public function getCharsetForCollation($collation)
50
    {
51
        $collation = $this->connection()->query('SHOW COLLATION LIKE \''.$collation.'%\'')->fetchAll();
52
53
        if(isset($collation[0])){
54
            return $collation[0]['Charset'];
55
        }
56
57
        return 'utf8';
58
    }
59
60
    /**
61
     * get table status
62
     *
63
     * @param $table
64
     * @return array
65
     */
66
    public function getTableStatus($table)
67
    {
68
        $status = $this->connection()->query('show table status like \''.$table.'\'')->fetchAll();
69
70
        if(isset($status[0])){
71
            return $status[0];
72
        }
73
74
        return [];
75
    }
76
77
    /**
78
     * get show indexes
79
     *
80
     * @param $table
81
     * @return mixed
82
     */
83
    public function showIndexes($table)
84
    {
85
        return $this->connection()->query('SHOW INDEXES FROM '.$table)->fetchAll();
86
    }
87
88
    /**
89
     * get foreign keys from table
90
     *
91
     * @param $table
92
     * @return array
93
     */
94
    public function getForeignKeys($table)
95
    {
96
        $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

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