Passed
Push — master ( f0ed0e...da797a )
by Gombos
01:32
created

CreateSettingsFieldForModel::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 4
nop 1
dl 0
loc 36
ccs 0
cts 27
cp 0
crap 20
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace Glorand\Model\Settings\Console;
4
5
use Illuminate\Console\Command;
0 ignored issues
show
Bug introduced by Gombos Lorand
The type Illuminate\Console\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Filesystem\Filesystem;
0 ignored issues
show
Bug introduced by Gombos Lorand
The type Illuminate\Filesystem\Filesystem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Support\Str;
8
use Schema;
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
    public function handle(Filesystem $file)
24
    {
25
        $table = strtolower($this->ask('What is the name of the table?'));
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
        $fieldName = 'settings';
33
        if (!Schema::hasTable($table)) {
34
            $this->error('Unable to find table "' . $table . '" on the current DB!');
35
36
            return false;
37
        }
38
        if (Schema::hasColumn($table, $fieldName)) {
39
            $this->error('Field "' . $fieldName . '" already exists on table "' . $table . '"');
40
41
            return false;
42
        }
43
44
        $fileName = date('Y_m_d_His') . '_update_' . $table . '_table_add_' . $fieldName . '.php';
45
        $path = database_path('migrations') . '/' . $fileName;
0 ignored issues
show
Bug introduced by Gombos Lorand
The function database_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        $path = /** @scrutinizer ignore-call */ database_path('migrations') . '/' . $fileName;
Loading history...
46
        $className = 'Update' . ucfirst($table) . 'TableAddSettings';
47
48
49
        $stub = $file->get(__DIR__ . '/../../stubs/create_settings_field.stub');
50
        $stub = str_replace('DummyClass', $className, $stub);
51
        $stub = str_replace('DummyTable', $table, $stub);
52
        $stub = str_replace('settingsFieldName', $fieldName, $stub);
53
54
        $file->put($path, $stub);
55
56
        $this->line("<info>Created Migration:</info> {$fileName}");
57
58
        return true;
59
    }
60
}
61