MigrateResetCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 33
dl 0
loc 103
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 7 1
A getArguments() 0 4 1
A handle() 0 19 3
A reset() 0 25 5
1
<?php
2
3
namespace Salah3id\Domains\Commands;
4
5
use Illuminate\Console\Command;
6
use Salah3id\Domains\Migrations\Migrator;
7
use Salah3id\Domains\Traits\MigrationLoaderTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class MigrateResetCommand extends Command
12
{
13
    use MigrationLoaderTrait;
14
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'domain:migrate-reset';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Reset the domains migrations.';
28
29
    /**
30
     * @var \Salah3id\Domains\Contracts\RepositoryInterface
31
     */
32
    protected $domain;
33
34
    /**
35
     * Execute the console command.
36
     */
37
    public function handle(): int
38
    {
39
        $this->domain = $this->laravel['domains'];
40
41
        $name = $this->argument('domain');
42
43
        if (!empty($name)) {
44
            $this->reset($name);
45
46
            return 0;
47
        }
48
49
        foreach ($this->domain->getOrdered($this->option('direction')) as $domain) {
50
            $this->line('Running for domain: <info>' . $domain->getName() . '</info>');
51
52
            $this->reset($domain);
53
        }
54
55
        return 0;
56
    }
57
58
    /**
59
     * Rollback migration from the specified domain.
60
     *
61
     * @param $domain
62
     */
63
    public function reset($domain)
64
    {
65
        if (is_string($domain)) {
66
            $domain = $this->domain->findOrFail($domain);
67
        }
68
69
        $migrator = new Migrator($domain, $this->getLaravel());
70
71
        $database = $this->option('database');
72
73
        if (!empty($database)) {
74
            $migrator->setDatabase($database);
75
        }
76
77
        $migrated = $migrator->reset();
78
79
        if (count($migrated)) {
80
            foreach ($migrated as $migration) {
81
                $this->line("Rollback: <info>{$migration}</info>");
82
            }
83
84
            return;
85
        }
86
87
        $this->comment('Nothing to rollback.');
88
    }
89
90
    /**
91
     * Get the console command arguments.
92
     *
93
     * @return array
94
     */
95
    protected function getArguments()
96
    {
97
        return [
98
            ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'],
99
        ];
100
    }
101
102
    /**
103
     * Get the console command options.
104
     *
105
     * @return array
106
     */
107
    protected function getOptions()
108
    {
109
        return [
110
            ['direction', 'd', InputOption::VALUE_OPTIONAL, 'The direction of ordering.', 'desc'],
111
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
112
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
113
            ['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],
114
        ];
115
    }
116
}
117