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

DynamicDatabaseConfigMigrateCommand::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 22
rs 9.8333
1
<?php
2
3
namespace Ikechukwukalu\Dynamicdatabaseconfig\Console\Commands;
4
5
use Ikechukwukalu\Dynamicdatabaseconfig\Models\DatabaseConfiguration;
6
use Ikechukwukalu\Dynamicdatabaseconfig\Trait\DatabaseConfig;
7
use Illuminate\Console\Command;
8
9
class DynamicDatabaseConfigMigrateCommand extends Command
10
{
11
    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...
12
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'dynamic:migrate
19
                            {ref : The ref for the database configuration}
20
                            {--P|--path= : The path where the database migration files are stored}
21
                            {--seeder= : Running a single seeder class}
22
                            {--refresh : Refreshing all migration}
23
                            {--rollback : Reverting migrations}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Migrate databases dynamically';
31
32
    /**
33
     * Execute the console command.
34
     */
35
    public function handle(): void
36
    {
37
        $path = config('dynamicdatabaseconfig.default_path');
38
        $ref = $this->argument('ref');
39
40
        if ($this->option('path')) {
41
            $path = $this->option('path');
42
        }
43
44
        [$database, $configuration, $name] = $this->getDynamicDatabaseConfiguration($ref);
45
46
        if (!$database) {
47
            $this->components->error("No database configuration was found");
48
            return;
49
        }
50
51
        $this->components->info("Running migrations for {$name} and path={$path} with ref={$ref}");
52
53
        $newConfig = $this->setNewDynamicConfig($database, $configuration);
0 ignored issues
show
Bug introduced by
$configuration of type null|string is incompatible with the type array expected by parameter $configuration of Ikechukwukalu\Dynamicdat...::setNewDynamicConfig(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $newConfig = $this->setNewDynamicConfig($database, /** @scrutinizer ignore-type */ $configuration);
Loading history...
54
        $this->addNewConfig($database, $name, $newConfig);
55
        $this->createDatabase($database, $configuration['database']);
56
        $this->execMigrationCommands($name, $path);
57
    }
58
59
    private function execMigrationCommands(string $name, null|string $path = '')
60
    {
61
        if ($this->option('refresh')) {
62
            $this->call('migrate:refresh', ['--database' => $name, '--path' => $path]);
63
        } else {
64
            $this->call('migrate', ['--database' => $name, '--path' => $path]);
65
        }
66
67
        if ($this->option('rollback')) {
68
            $this->call('migrate:rollback', ['--database' => $name, '--path' => $path]);
69
            return;
70
        }
71
72
        if ($seeder = $this->option('seeder')) {
73
            $this->call('db:seed', ['--class' => $seeder]);
74
        }
75
    }
76
}
77