Passed
Push — master ( 045b84...663db2 )
by Ferry
02:48
created

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

99
        $process = new Process(/** @scrutinizer ignore-type */ $composer_path.' dump-autoload');
Loading history...
100
        $process->setWorkingDirectory(base_path())->run();
101
102
    }
103
}
104