Passed
Push — master ( da797a...38c1ce )
by Gombos
03:26
created

CreateSettingsTable::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 1
dl 0
loc 29
ccs 0
cts 21
cp 0
crap 12
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Glorand\Model\Settings\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Str;
8
use Schema;
9
10
class CreateSettingsTable extends Command
11
{
12
    /** @var string */
13
    protected $signature = 'model-settings:model-settings-table';
14
15
    /** @var string */
16
    protected $description = 'Create migration for the settings table';
17
18
    /**
19
     * @param \Illuminate\Filesystem\Filesystem $file
20
     * @return bool
21
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
22
     */
23
    public function handle(Filesystem $file)
24
    {
25
        $table = strtolower(config('model_settings.settings_table_name'));
26
        $table = Str::snake(trim($table));
27
        if (empty($table)) {
28
            $this->error('The name of the table is required!');
29
30
            return false;
31
        }
32
33
        if (Schema::hasTable($table)) {
34
            $this->error('Table "' . $table . '" already exists!');
35
36
            return false;
37
        }
38
39
        $fileName = date('Y_m_d_His') . '_create_' . $table . '_table.php';
40
        $path = database_path('migrations') . '/' . $fileName;
41
        $className = 'Create' . ucfirst(Str::camel($table)) . 'Table';
42
43
44
        $stub = $file->get(__DIR__ . '/../../stubs/create_settings_table.stub');
45
        $stub = str_replace('DummyClass', $className, $stub);
46
        $stub = str_replace('DummyTable', $table, $stub);
47
48
        $file->put($path, $stub);
49
        $this->line("<info>Created Migration:</info> {$fileName}");
50
51
        return true;
52
    }
53
}
54