Passed
Push — main ( 42905a...9ee423 )
by ikechukwu
02:38
created

execMigrationCommands()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 15
rs 9.9666
1
<?php
2
3
namespace Ikechukwukalu\Dynamicdatabaseconfig\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Ikechukwukalu\Dynamicdatabaseconfig\Trait\DatabaseConfig;
7
8
class EnvDatabaseConfigMigrateCommand extends Command
9
{
10
    use DatabaseConfig;
0 ignored issues
show
introduced by
The trait Ikechukwukalu\Dynamicdat...ig\Trait\DatabaseConfig requires some properties which are not provided by Ikechukwukalu\Dynamicdat...aseConfigMigrateCommand: $database, $configuration
Loading history...
11
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'env:migrate
18
                            {database : The type of database}
19
                            {name : The name of the new database connection}
20
                            {postfix : The postfix for the database configuration}
21
                            {--P|--path= : The path where the database migration files are kept}
22
                            {--seeder= : Running a single seeder class}
23
                            {--refresh : Refreshing all migration}
24
                            {--rollback : Reverting migrations}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Migrate databases from env';
32
33
    /**
34
     * Execute the console command.
35
     */
36
    public function handle(): void
37
    {
38
        $path = config('dynamicdatabaseconfig.default_path');
39
        $database = $this->argument('database');
40
        $name = $this->argument('name');
41
        $postFix = $this->argument('postfix');
42
43
        if ($this->option('path')) {
44
            $path = $this->option('path');
45
        }
46
47
        $this->components->info("Running migrations for {$name} and path={$path}");
48
49
        $newConfig = $this->setNewEnvConfig($database, $postFix);
50
        $this->addNewConfig($database, $name, $newConfig);
51
        $this->createDatabase($database, $newConfig['database']);
52
        $this->execMigrationCommands($name, $path);
53
    }
54
55
    private function execMigrationCommands(string $name, null|string $path = '')
56
    {
57
        if ($this->option('refresh')) {
58
            $this->call('migrate:refresh', ['--database' => $name, '--path' => $path]);
59
        } else {
60
            $this->call('migrate', ['--database' => $name, '--path' => $path]);
61
        }
62
63
        if ($this->option('rollback')) {
64
            $this->call('migrate:rollback', ['--database' => $name, '--path' => $path]);
65
            return;
66
        }
67
68
        if ($seeder = $this->option('seeder')) {
69
            $this->call('db:seed', ['--class' => $seeder]);
70
        }
71
    }
72
}
73