Completed
Pull Request — master (#166)
by Josh
12:20
created

MigrateCommand::migrate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Nwidart\Modules\Migrations\Migrator;
7
use Nwidart\Modules\Module;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class MigrateCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'module:migrate';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Migrate the migrations from the specified module or from all modules.';
26
27
    /**
28
     * @var \Nwidart\Modules\Repository
29
     */
30
    protected $module;
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @return mixed
36
     */
37
    public function fire()
38
    {
39
        $this->module = $this->laravel['modules'];
40
41
        $name = $this->argument('module');
42
43
        if ($name) {
44
            $module = $this->module->findOrFail($name);
45
46
            return $this->migrate($module);
47
        }
48
49
        foreach ($this->module->getOrdered($this->option('direction')) as $module) {
50
            $this->line('Running for module: <info>' . $module->getName() . '</info>');
51
52
            $this->migrate($module);
53
        }
54
    }
55
56
    /**
57
     * Run the migration from the specified module.
58
     *
59
     * @param Module $module
60
     *
61
     * @return mixed
62
     */
63
    protected function migrate(Module $module)
64
    {
65
        $path = str_replace(base_path(), '', (new Migrator($module))->getPath());
66
        $this->call('migrate', [
67
            '--path' => $path,
68
            '--database' => $this->option('database'),
69
            '--pretend' => $this->option('pretend'),
70
            '--force' => $this->option('force'),
71
        ]);
72
73
        if ($this->option('seed')) {
74
            $this->call('module:seed', ['module' => $module->getName()]);
75
        }
76
    }
77
78
    /**
79
     * Get the console command arguments.
80
     *
81
     * @return array
82
     */
83 43
    protected function getArguments()
84
    {
85
        return array(
86 43
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
87 43
        );
88
    }
89
90
    /**
91
     * Get the console command options.
92
     *
93
     * @return array
94
     */
95 43
    protected function getOptions()
96
    {
97
        return array(
98 43
            array('direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'asc'),
99 43
            array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'),
100 43
            array('pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'),
101 43
            array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'),
102 43
            array('seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'),
103 43
        );
104
    }
105
}
106