Completed
Push — master ( b547c8...ab91a6 )
by Mark
10s
created

MigrateDisorganise::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 0
1
<?php
2
3
namespace Jaybizzle\MigrationsOrganiser\Commands;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Jaybizzle\MigrationsOrganiser\Migrator;
7
use Symfony\Component\Console\Input\InputOption;
8
use Illuminate\Database\Console\Migrations\BaseCommand;
9
10
class MigrateDisorganise extends BaseCommand
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'migrate:disorganise';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Move migrations from a yyyy/mm folder structure back to the base migrations folder';
25
26
    /**
27
     * The migrator instance.
28
     *
29
     * @var \Jaybizzle\MigrationsOrganiser\Migrator
30
     */
31
    protected $migrator;
32
33
    /**
34
     * The filesystem instance.
35
     *
36
     * @var \Illuminate\Filesystem\Filesystem
37
     */
38
    protected $files;
39
40
    /**
41
     * The basePath for the migrations.
42
     */
43
    protected $basePath;
44
45
    /**
46
     * Create a new migrator instance.
47
     *
48
     * @param \Illuminate\Filesystem\Filesystem        $files
49
     * @param \Illuminate\Database\Migrations\Migrator $migrator
50
     */
51
    public function __construct(Filesystem $files, Migrator $migrator)
52
    {
53
        parent::__construct();
54
        $this->migrator = $migrator;
55
        $this->files = $files;
56
    }
57
58
    /**
59
     * Fire the command. (Compatibility for < 5.5).
60
     */
61
    public function fire()
62
    {
63
        $this->handle();
64
    }
65
66
    /**
67
     * Create date folder structure and move migrations into.
68
     *
69
     * @return void
70
     */
71
    public function handle()
72
    {
73
        $this->basePath = $this->getMigrationPath();
74
        $migrations = $this->migrator->getMigrationFiles($this->basePath);
0 ignored issues
show
Documentation introduced by
$this->basePath is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
        $count = count($migrations);
76
77
        if ($count == 0) {
78
            $this->comment('No migrations to move');
79
80
            return;
81
        }
82
83
        foreach ($migrations as $migration_name => $migration_path) {
84
            $datePath = $this->migrator->getDateFolderStructure($migration_name);
85
            // Move the migration into base migration folder
86
            $this->files->move($this->basePath.'/'.$datePath.$migration_name.'.php', $this->basePath.'/'.$migration_name.'.php');
87
        }
88
89
        $this->info('Migrations disorganised successfully ('.$count.' migrations moved)');
90
        $this->cleanup();
91
    }
92
93
    /**
94
     * Decide whether or not to delete directories.
95
     *
96
     * @return void
97
     */
98
    public function cleanup()
99
    {
100
        if ($this->option('force')) {
101
            $this->deleteDirs();
102
        } elseif ($this->confirm('Delete all subdirectories in migrations folder?', true)) {
103
            $this->deleteDirs();
104
        }
105
    }
106
107
    /**
108
     * Delete subdirectories in the migrations folder.
109
     *
110
     * @return void
111
     */
112
    public function deleteDirs()
113
    {
114
        $dirs = $this->files->directories($this->basePath);
115
116
        foreach ($dirs as $dir) {
117
            $this->files->deleteDirectory($dir);
118
        }
119
120
        $this->info('Subdirectories deleted');
121
    }
122
123
    /**
124
     * Get the console command options.
125
     *
126
     * @return array
127
     */
128
    protected function getOptions()
129
    {
130
        return [
131
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to delete migration folder subdirectories without prompt.'],
132
        ];
133
    }
134
}
135