| Total Complexity | 47 |
| Total Lines | 270 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Pulling often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pulling, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 | |||
| 20 | $migrations = $this->tableFilters(); |
||
| 21 | |||
| 22 | $list = []; |
||
| 23 | |||
| 24 | foreach ($migrations as $table=>$item){ |
||
| 25 | $list[] = strtolower($table); |
||
| 26 | } |
||
| 27 | |||
| 28 | echo 'Migrations Pull Tasks:'; |
||
| 29 | echo PHP_EOL; |
||
| 30 | echo 'Toplam : '.count($dbtables).' Table'; |
||
| 31 | echo PHP_EOL; |
||
| 32 | |||
| 33 | foreach ($dbtables as $dbtablekey=>$dbtable){ |
||
| 34 | |||
| 35 | $dbtablekey = $dbtablekey+1; |
||
| 36 | |||
| 37 | $informations = $this->tableInformations($dbtable); |
||
| 38 | |||
| 39 | $dbtable = ucfirst($dbtable); |
||
| 40 | $makeDirectory = $directory.''.DIRECTORY_SEPARATOR.''.$dbtable; |
||
| 41 | |||
| 42 | if(!file_exists($makeDirectory)){ |
||
| 43 | files()->makeDirectory($makeDirectory,0755,true); |
||
| 44 | $exist=false; |
||
| 45 | } |
||
| 46 | else{ |
||
| 47 | $exist=true; |
||
| 48 | } |
||
| 49 | |||
| 50 | $migrationName = time().'_'.$dbtable.''; |
||
| 51 | |||
| 52 | if($exist===false){ |
||
| 53 | |||
| 54 | $content = $this->getContentFile($this->getStubPath().''.DIRECTORY_SEPARATOR.'pullCreate.stub',[ |
||
| 55 | 'className' => $dbtable, |
||
| 56 | 'informations' => $informations |
||
| 57 | ]); |
||
| 58 | |||
| 59 | $contentResult = files()->put($makeDirectory.''.DIRECTORY_SEPARATOR.''.$migrationName.'.php',$content); |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | if(isset($contentResult) && $contentResult!==false){ |
||
| 64 | echo $dbtablekey.'- '.$migrationName.' ---> Ok'; |
||
| 65 | } |
||
| 66 | elseif($exist){ |
||
| 67 | echo $dbtablekey.'- '.$migrationName.' ---> (Already Exist)'; |
||
| 68 | } |
||
| 69 | else{ |
||
| 70 | echo $dbtablekey.'- '.$migrationName.' ---> Fail'; |
||
| 71 | } |
||
| 72 | |||
| 73 | echo PHP_EOL; |
||
| 74 | |||
| 75 | /**if(substr($dbtable,-1)=='s'){ |
||
| 76 | app()->command('model create','model:'.strtolower(substr($dbtable,0,-1))); |
||
| 77 | } |
||
| 78 | else{ |
||
| 79 | app()->command('model create','model:'.strtolower($dbtable)); |
||
| 80 | }**/ |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * get table informations |
||
| 86 | * |
||
| 87 | * @param $table |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | private function tableInformations($table) |
||
| 91 | { |
||
| 92 | $foreignKeys = $this->schema->getConnection()->getForeignKeys($table); |
||
| 93 | |||
| 94 | $columns = $this->schema->getConnection()->showColumnsFrom($table); |
||
| 95 | |||
| 96 | $status = $this->schema->getConnection()->getTableStatus($table); |
||
| 97 | |||
| 98 | $indexes = $this->schema->getConnection()->showIndexes($table); |
||
| 99 | $multipleIndexes = $this->getMultipleIndex($indexes); |
||
| 100 | |||
| 101 | |||
| 102 | $list = []; |
||
| 103 | |||
| 104 | foreach ($columns as $key=>$data){ |
||
| 105 | |||
| 106 | $data['Type'] = rtrim(str_replace('unsigned','',$data['Type'])); |
||
| 107 | |||
| 108 | $field = $data['Field']; |
||
| 109 | $list[] = '$wizard->name(\''.$field.'\')'; |
||
| 110 | $list[] = '->'.$this->getColumnTransformers($data['Type']).''; |
||
| 111 | |||
| 112 | //default block |
||
| 113 | if(!is_null($data['Default'])){ |
||
| 114 | $default = $data['Default']; |
||
| 115 | $list[] = '->default(\''.$default.'\')'; |
||
| 116 | } |
||
| 117 | |||
| 118 | $getIndex = $this->getIndexInformation($indexes,$data['Field']); |
||
| 119 | |||
| 120 | //unique block |
||
| 121 | if($getIndex['Non_unique']=='0' && $getIndex['Key_name']!=='PRIMARY'){ |
||
| 122 | $indexName = $getIndex['Key_name']; |
||
| 123 | $list[] = '->unique(\''.$indexName.'\')'; |
||
| 124 | } |
||
| 125 | |||
| 126 | //index block |
||
| 127 | if($getIndex['Non_unique']=='1' && $getIndex['Key_name']!=='PRIMARY'){ |
||
| 128 | $columnName = $getIndex['Column_name']; |
||
|
|
|||
| 129 | $indexName = $getIndex['Key_name']; |
||
| 130 | |||
| 131 | if(count($multipleIndexes[$indexName])==1){ |
||
| 132 | $list[] = '->index(\''.$indexName.'\')'; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | //comment block |
||
| 137 | if(strlen($data['Comment'])>0){ |
||
| 138 | $comment = $data['Comment']; |
||
| 139 | $list[] = '->comment(\''.$comment.'\')'; |
||
| 140 | } |
||
| 141 | |||
| 142 | //auto increment block |
||
| 143 | if($data['Extra']=='auto_increment'){ |
||
| 144 | $list[] = '->auto_increment()'; |
||
| 145 | } |
||
| 146 | |||
| 147 | $list[] = '; |
||
| 148 | '; |
||
| 149 | } |
||
| 150 | |||
| 151 | //table collation |
||
| 152 | $charset = $this->schema->getConnection()->getCharsetForCollation(''.$status['Collation'].''); |
||
| 153 | $list[] = '$wizard->table()->collation(\''.$charset.'\'); |
||
| 154 | '; |
||
| 155 | |||
| 156 | //table indexes |
||
| 157 | foreach ($multipleIndexes as $indexName=>$values) { |
||
| 158 | if(count($values)>1){ |
||
| 159 | $values = '\''.implode('\',\'',$values).'\''; |
||
| 160 | $list[] = ' $wizard->table()->indexes(\''.$indexName.'\',['.$values.']); |
||
| 161 | '; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | if(count($foreignKeys)){ |
||
| 166 | |||
| 167 | $constraintName = $foreignKeys['CONSTRAINT_NAME']; |
||
| 168 | $key = $foreignKeys['COLUMN_NAME']; |
||
| 169 | $referenceTable = $foreignKeys['REFERENCED_TABLE_NAME']; |
||
| 170 | $referenceColumn = $foreignKeys['REFERENCED_COLUMN_NAME']; |
||
| 171 | |||
| 172 | if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){ |
||
| 173 | $list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\'); |
||
| 174 | '; |
||
| 175 | } |
||
| 176 | |||
| 177 | if($foreignKeys['UPDATE_RULE']!=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){ |
||
| 178 | $rule = $foreignKeys['UPDATE_RULE']; |
||
| 179 | $list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onUpdate()->'.strtolower($rule).'(); |
||
| 180 | '; |
||
| 181 | } |
||
| 182 | |||
| 183 | if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']!=='RESTRICT'){ |
||
| 184 | $rule = $foreignKeys['DELETE_RULE']; |
||
| 185 | $list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onDelete()->'.strtolower($rule).'(); |
||
| 186 | '; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | return implode('',$list); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * get column transformers |
||
| 195 | * |
||
| 196 | * @param $column |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | private function getColumnTransformers($column) |
||
| 200 | { |
||
| 201 | if($column=='datetime'){ |
||
| 202 | return 'datetime()'; |
||
| 203 | } |
||
| 204 | elseif($column=='longtext'){ |
||
| 205 | return 'longtext()'; |
||
| 206 | } |
||
| 207 | elseif($column=='date'){ |
||
| 208 | return 'date()'; |
||
| 209 | } |
||
| 210 | elseif($column=='text'){ |
||
| 211 | return 'text()'; |
||
| 212 | } |
||
| 213 | elseif($column=='timestamp'){ |
||
| 214 | return 'timestamp()'; |
||
| 215 | } |
||
| 216 | elseif($column=='mediumint'){ |
||
| 217 | return 'mediumint()'; |
||
| 218 | } |
||
| 219 | elseif($column=='tinyint'){ |
||
| 220 | return 'tinyint()'; |
||
| 221 | } |
||
| 222 | elseif($column=='float'){ |
||
| 223 | return 'float()'; |
||
| 224 | } |
||
| 225 | elseif($column=='mediumtext'){ |
||
| 226 | return 'mediumtext()'; |
||
| 227 | } |
||
| 228 | elseif($column=='mediumblob'){ |
||
| 229 | return 'mediumblob()'; |
||
| 230 | } |
||
| 231 | elseif($column=='blob'){ |
||
| 232 | return 'blob()'; |
||
| 233 | } |
||
| 234 | elseif(preg_match('@enum.*\((.*?)\)@',$column,$enum)){ |
||
| 235 | return 'enum(['.$enum[1].'])'; |
||
| 236 | } |
||
| 237 | else{ |
||
| 238 | return $column; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * get index information |
||
| 244 | * |
||
| 245 | * @param $index |
||
| 246 | * @param $field |
||
| 247 | * @return void|mixed |
||
| 248 | */ |
||
| 249 | private function getIndexInformation($index,$field) |
||
| 250 | { |
||
| 251 | foreach ($index as $key=>$item) { |
||
| 252 | |||
| 253 | if($item['Column_name'] == $field){ |
||
| 254 | return $index[$key]; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | return null; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * get index information |
||
| 263 | * |
||
| 264 | * @param $index |
||
| 265 | * @param $field |
||
| 266 | * @return void|mixed |
||
| 267 | */ |
||
| 268 | private function getMultipleIndex($index) |
||
| 278 | } |
||
| 279 | } |