DbDropCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 View Code Duplication
class DbDropCommand extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'db:drop
22
        {--dry-run}
23
        {--database= : The database name}
24
    ';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Drops the database.';
32
33
    /**
34
     * Create a new command instance.
35
     *
36
     * @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...
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
    }
42
43
    /**
44
     * Execute the console command.
45
     *
46
     * @return mixed
47
     */
48
    public function handle(GrammarFactory $grammars)
49
    {
50
        $dryRun = (bool) $this->option('dry-run');
51
52
        if ($dryRun) {
53
            $this->info('[DRY] Running in dry-run.');
54
        }
55
56
        $connection = config('database.default');
57
        $configs = config(sprintf('database.connections.%s', $connection));
58
        $configs['database'] = $this->option('database') ?: $configs['database'];
59
60
        $builder = new Builder(
61
            $this->makeConnector($configs, $dryRun),
62
            $grammars
63
        );
64
65
        if ($builder->dropDatabase($configs) === false) {
66
            $this->error('Could not drop the database.');
67
            return 1;
68
        }
69
70
        $this->info(sprintf('Database "%s" drop successfully.', $configs['database']));
71
    }
72
73
    private function makeConnector(array $configs, bool $dryRun = false): Connector
74
    {
75
        if ($dryRun === true) {
76
            return new DryRunConnector($this->output);
77
        }
78
79
        return PDOConnector::make($configs);
80
    }
81
}
82