Passed
Push — master ( f73cee...5bfef3 )
by Gombos
03:36
created

CreateSettingsTable::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 29
ccs 18
cts 18
cp 1
crap 3
rs 9.7
1
<?php
2
3
namespace Glorand\Model\Settings\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Facades\Schema;
8
use Illuminate\Support\Str;
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 1
    public function handle(Filesystem $file)
24
    {
25 1
        $table = strtolower(config('model_settings.settings_table_name'));
26 1
        $table = Str::snake(trim($table));
27 1
        if (empty($table)) {
28 1
            $this->error('The name of the table is required!');
29
30 1
            return false;
31
        }
32
33 1
        if (Schema::hasTable($table)) {
34 1
            $this->error('Table "' . $table . '" already exists!');
35
36 1
            return false;
37
        }
38
39 1
        $fileName = date('Y_m_d_His') . '_create_' . $table . '_table.php';
40 1
        $path = database_path('migrations') . '/' . $fileName;
41 1
        $className = 'Create' . ucfirst(Str::camel($table)) . 'Table';
42
43
44 1
        $stub = $file->get(__DIR__ . '/../../stubs/create_settings_table.stub');
45 1
        $stub = str_replace('DummyClass', $className, $stub);
46 1
        $stub = str_replace('DummyTable', $table, $stub);
47
48 1
        $file->put($path, $stub);
49 1
        $this->line("<info>Created Migration:</info> {$fileName}");
50
51 1
        return true;
52
    }
53
}
54