MigrateRefreshCommand::handle()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 29
rs 9.7333
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
namespace Salah3id\Domains\Commands;
4
5
use Illuminate\Console\Command;
6
use Salah3id\Domains\Traits\DomainCommandTrait;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class MigrateRefreshCommand extends Command
11
{
12
    use DomainCommandTrait;
13
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $name = 'domain:migrate-refresh';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Rollback & re-migrate the domains migrations.';
27
28
    /**
29
     * Execute the console command.
30
     */
31
    public function handle(): int
32
    {
33
        $domain = $this->argument('domain');
34
35
        if ($domain && !$this->getDomainName()) {
36
            $this->error("Domain [$domain] does not exists.");
37
38
            return E_ERROR;
39
        }
40
41
        $this->call('domain:migrate-reset', [
42
            'domain' => $this->getDomainName(),
43
            '--database' => $this->option('database'),
44
            '--force' => $this->option('force'),
45
        ]);
46
47
        $this->call('domain:migrate', [
48
            'domain' => $this->getDomainName(),
49
            '--database' => $this->option('database'),
50
            '--force' => $this->option('force'),
51
        ]);
52
53
        if ($this->option('seed')) {
54
            $this->call('domain:seed', [
55
                'domain' => $this->getDomainName(),
56
            ]);
57
        }
58
59
        return 0;
60
    }
61
62
    /**
63
     * Get the console command arguments.
64
     *
65
     * @return array
66
     */
67
    protected function getArguments()
68
    {
69
        return [
70
            ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'],
71
        ];
72
    }
73
74
    /**
75
     * Get the console command options.
76
     *
77
     * @return array
78
     */
79
    protected function getOptions()
80
    {
81
        return [
82
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
83
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
84
            ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],
85
        ];
86
    }
87
88
    public function getDomainName()
89
    {
90
        $domain = $this->argument('domain');
91
92
        if (!$domain) {
93
            return null;
94
        }
95
96
        $domain = app('domains')->find($domain);
97
98
        return $domain ? $domain->getStudlyName() : null;
99
    }
100
}
101