DbReCreateCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 20 2
A makeConnector() 0 8 2
1
<?php
2
3
namespace Tonysm\LaravelParatest\Console;
4
5
use Tonysm\LaravelParatest\Database\{
6
    Connector,
7
    PDOConnector,
8
    DryRunConnector,
9
    Schema\Builder,
10
    Schema\GrammarFactory
11
};
12
use Illuminate\Console\Command;
13
14
class DbReCreateCommand extends Command
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'db:recreate
22
        {--dry-run}
23
    ';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Re-creates the database.';
31
32
    /**
33
     * Create a new command instance.
34
     *
35
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
    }
41
42
    /**
43
     * Execute the console command.
44
     *
45
     * @return mixed
46
     */
47
    public function handle(GrammarFactory $grammars)
48
    {
49
        $dryRun = (bool) $this->option('dry-run');
50
51
        if ($dryRun) {
52
            $this->info('[DRY] Running in dry-run.');
53
        }
54
55
        $connection = config('database.default');
56
        $configs = config(sprintf('database.connections.%s', $connection));
57
58
        $builder = new Builder(
59
            $this->makeConnector($configs, $dryRun),
60
            $grammars
61
        );
62
63
        $builder->recreateDatabase($configs);
64
65
        $this->info(sprintf('Database "%s" re-created successfully.', $configs['database']));
66
    }
67
68
    private function makeConnector(array $configs, bool $dryRun = false): Connector
69
    {
70
        if ($dryRun === true) {
71
            return new DryRunConnector($this->output);
72
        }
73
74
        return PDOConnector::make($configs);
75
    }
76
}
77