Passed
Push — master ( 25a8ca...66dd56 )
by Ferry
02:55
created

MigrationData::generateSeeder()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 54
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 16
nop 0
dl 0
loc 54
rs 8.4444
c 0
b 0
f 0

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 namespace crocodicstudio\crudbooster\commands;
2
3
use App;
4
use Cache;
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Str;
8
use Symfony\Component\Process\Process;
9
10
class MigrationData extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'crudbooster:data_migration';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'CRUDBooster: Generate CB Table Data Migration';
25
26
    /**
27
     * Execute the console command.
28
     *
29
     * @return mixed
30
     */
31
    public function handle()
32
    {
33
        $this->info("== Data Migration ==");
34
        $this->generateSeeder();
35
        $this->info("== Data Migration Finished ==");
36
    }
37
38
    private function generateSeeder() {
39
        set_time_limit(120);
40
        $tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();
41
        $php_string = "";
42
        $additional_tables = cbConfig("ADDITIONAL_DATA_MIGRATION");
43
44
        foreach($tables as $table) {
45
            if(substr($table,0,3) == "cb_" || in_array($table, $additional_tables)) {
46
                $this->info("Create seeder for table : ".$table);
47
                $rows = DB::table($table)->get();
48
                $data = [];
49
                foreach($rows as $i=>$row) {
50
                    $data[$i] = [];
51
                    foreach($row as $key=>$val) {
52
                        $data[$i][$key] = $val;
53
                    }
54
                }
55
                if(count($data)!=0) {
56
                    $php_string .= 'DB::table(\''.$table.'\')->delete();'."\n\t\t\t";
57
                    $php_string .= 'DB::table(\''.$table.'\')->insert('.min_var_export($data).');'."\n\t\t\t";
58
                }
59
            }
60
        }
61
        $seederFileTemplate = '
62
<?php
63
use Illuminate\Database\Seeder;
64
use Illuminate\Support\Facades\DB;
65
class CbMigrationSeeder extends Seeder
66
{
67
    public function run()
68
    {
69
        $this->command->info(\'Please wait updating the data...\');                
70
        $this->call(\'CbMigrationData\');        
71
        $this->command->info(\'Updating the data completed !\');
72
    }
73
}
74
class CbMigrationData extends Seeder {
75
    public function run() {        
76
    	'.$php_string.'
77
    }
78
}
79
	';
80
        file_put_contents(base_path('database/seeds/CbMigrationSeeder.php'), $seederFileTemplate);
81
82
        $composer_path = '';
83
        if (file_exists(getcwd().'/composer.phar')) {
84
            $composer_path = '"'.PHP_BINARY.'" '.getcwd().'/composer.phar';
85
        }else{
86
            $composer_path = 'composer';
87
        }
88
89
        $this->info('Dumping auto loads new file seeder !');
90
        $process = new Process($composer_path.' dump-autoload');
0 ignored issues
show
Bug introduced by
$composer_path . ' dump-autoload' of type string is incompatible with the type array expected by parameter $command of Symfony\Component\Process\Process::__construct(). ( Ignorable by Annotation )

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

90
        $process = new Process(/** @scrutinizer ignore-type */ $composer_path.' dump-autoload');
Loading history...
91
        $process->setWorkingDirectory(base_path())->run();
92
93
    }
94
}
95