Passed
Push — next ( a64239...381d12 )
by Bas
06:24 queued 02:50
created

FreshCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 97.96%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 46
c 2
b 0
f 1
dl 0
loc 80
ccs 48
cts 49
cp 0.9796
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 16 1
B handle() 0 48 6
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Console\Migrations;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Illuminate\Database\Console\Migrations\FreshCommand as IlluminateFreshCommand;
8
use Illuminate\Database\Events\DatabaseRefreshed;
9
use LaravelFreelancerNL\Aranguent\Console\Concerns\ArangoCommands;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class FreshCommand extends IlluminateFreshCommand
13
{
14
    use ArangoCommands;
15
16
    /**
17
     * Execute the console command.
18
     *
19
     * @return int
20
     */
21 46
    public function handle()
22
    {
23 46
        if ($this->isProhibited() ||
24 46
            ! $this->confirmToProceed()) {
25
            return Command::FAILURE;
26
        }
27
28 46
        $database = $this->input->getOption('database');
29
30 46
        $this->migrator->usingConnection($database, function () use ($database) {
31 46
            if ($this->migrator->repositoryExists()) {
32 37
                $this->newLine();
33
34 37
                $this->components->task('Dropping all tables', fn() => $this->callSilent('db:wipe', array_filter([
35 37
                    '--database' => $database,
36 37
                    '--drop-all' => $this->option('drop-all'),
37 37
                    '--drop-analyzers' => $this->option('drop-analyzers'),
38 37
                    '--drop-graphs' => $this->option('drop-graphs'),
39 37
                    '--drop-views' => $this->option('drop-views'),
40 37
                    '--drop-types' => $this->option('drop-types'),
41 37
                    '--force' => true,
42 37
                ])) == 0);
43
            }
44 46
        });
45
46 44
        $this->newLine();
47
48 44
        $this->call('migrate', array_filter([
49 44
            '--database' => $database,
50 44
            '--path' => $this->input->getOption('path'),
51 44
            '--realpath' => $this->input->getOption('realpath'),
52 44
            '--schema-path' => $this->input->getOption('schema-path'),
53 44
            '--force' => true,
54 44
            '--step' => $this->option('step'),
55 44
        ]));
56
57 44
        if ($this->laravel->bound(Dispatcher::class)) {
58
            /** @phpstan-ignore offsetAccess.nonOffsetAccessible   */
59 44
            $this->laravel[Dispatcher::class]->dispatch(
60 44
                new DatabaseRefreshed($database, $this->needsSeeding()),
61 44
            );
62
        }
63
64 44
        if ($this->needsSeeding()) {
65 44
            $this->runSeeder($database);
66
        }
67
68 44
        return 0;
69
    }
70
71
    /**
72
     * Get the console command options.
73
     *
74
     * @return array<int, array<int, int|string|null>>
75
     */
76 451
    protected function getOptions()
77
    {
78 451
        return [
79 451
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
80 451
            ['drop-all', null, InputOption::VALUE_NONE, 'Drop all tables, views, custom analyzers and named graphs (ArangoDB only)'],
81 451
            ['drop-analyzers', null, InputOption::VALUE_NONE, 'Drop all tables and custom analyzers (ArangoDB only)'],
82 451
            ['drop-graphs', null, InputOption::VALUE_NONE, 'Drop all tables and named graphs (ArangoDB only)'],
83 451
            ['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views (ArangoDB only)'],
84 451
            ['drop-types', null, InputOption::VALUE_NONE, 'Drop all tables and types (Postgres only)'],
85 451
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
86 451
            ['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
87 451
            ['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
88 451
            ['schema-path', null, InputOption::VALUE_OPTIONAL, 'The path to a schema dump file'],
89 451
            ['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run'],
90 451
            ['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder'],
91 451
            ['step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually'],
92 451
        ];
93
    }
94
}
95