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 DbCreateCommand extends Command |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'db:create |
22
|
|
|
{--dry-run} |
23
|
|
|
{--database= : The database name to test} |
24
|
|
|
'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Creates the database.'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new command instance. |
35
|
|
|
* |
36
|
|
|
* @return void |
|
|
|
|
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->createDatabase($configs) === false) { |
66
|
|
|
$this->error('Could not create the database.'); |
67
|
|
|
return 1; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->info(sprintf('Database "%s" created 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
|
|
|
|
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.