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

CreateSettingsFieldForModel   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 56
ccs 27
cts 27
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 43 4
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 CreateSettingsFieldForModel extends Command
11
{
12
    /** @var string */
13
    protected $signature = 'model-settings:model-settings-field';
14
15
    /** @var string */
16
    protected $description = 'Create migration for the (update) selected table, adding settings field';
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 = $this->ask('What is the name of the table?');
26 1
        $table = strtolower(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 1
        if (!Schema::hasTable($table)) {
33 1
            $this->error('Unable to find table "' . $table . '" on the current DB!');
34
35 1
            return false;
36
        }
37
38 1
        $defaultFieldName = config('model_settings.settings_field_name');
39 1
        $fieldName = $this->ask(
40 1
            'What is the name of the settings field name?',
41 1
            $defaultFieldName
42
        );
43 1
        $fieldName = strtolower(Str::snake(trim($fieldName)));
44
45 1
        if (Schema::hasColumn($table, $fieldName)) {
46 1
            $this->error('Field "' . $fieldName . '" already exists on table "' . $table . '"');
47
48 1
            return false;
49
        }
50
51 1
        $fileName = date('Y_m_d_His') . '_update_' . $table . '_table_add_' . $fieldName . '.php';
52 1
        $path = database_path('migrations') . '/' . $fileName;
53 1
        $className = 'Update' . ucfirst($table) . 'TableAdd' . ucfirst(Str::camel($fieldName));
54
55
56 1
        $stub = $file->get(__DIR__ . '/../../stubs/create_settings_field.stub');
57 1
        $stub = str_replace('DummyClass', $className, $stub);
58 1
        $stub = str_replace('DummyTable', $table, $stub);
59 1
        $stub = str_replace('settingsFieldName', $fieldName, $stub);
60
61 1
        $file->put($path, $stub);
62
63 1
        $this->line("<info>Created Migration:</info> {$fileName}");
64
65 1
        return true;
66
    }
67
}
68