CreateModelMigrationAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 15 1
A getColumns() 0 12 2
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Actions\Migration;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Facades\File;
7
use Mtolhuys\LaravelSchematics\Services\RuleParser;
8
use Mtolhuys\LaravelSchematics\Actions\Migration\Traits\CreatesMigrations;
9
use Mtolhuys\LaravelSchematics\Services\StubWriter;
10
11
class CreateModelMigrationAction
12
{
13
    use CreatesMigrations;
14
15
    /**
16
     * @param $request
17
     */
18
    public function execute($request)
19
    {
20
        $model = ucfirst($request['name']);
21
        $table = Str::plural(Str::snake($model));
22
        $stub = __DIR__ . '/../../../resources/stubs/migration/model.stub';
23
        $this->filename = 'database/migrations/'
24
            . date('Y_m_d_His')
25
            . "_create_{$table}_table.php";
26
27
        (new StubWriter(base_path($this->filename), $stub))->write([
28
            '$classname$' => 'Create' . Str::plural($model) . 'Table',
29
            '$columns$' => rtrim($this->getColumns($request)),
30
            '$table$' => $table,
31
        ]);
32
    }
33
34
    /**
35
     * @param $request
36
     * @return string
37
     */
38
    private function getColumns($request): string
39
    {
40
        $columns = RuleParser::fieldsToMigrationMethods(
41
            $this->getFields($request['fields'])
42
        );
43
44
        if (json_decode($request['options']['hasTimestamps'], false)) {
45
            $columns .= '$table->timestamps();';
46
        }
47
48
        return $columns;
49
    }
50
}
51