Completed
Push — master ( cb1113...b09b3a )
by Maarten
16s queued 11s
created

CreateModelMigrationAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 23 2
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
10
class CreateModelMigrationAction
11
{
12
    use CreatesMigrations;
13
14
    /**
15
     * @param $request
16
     */
17
    public function execute($request)
18
    {
19
        $model = ucfirst($request['name']);
20
        $table = Str::plural(Str::snake($model));
21
        $stub = __DIR__ . '/../../../resources/stubs/migration/model.stub';
22
        $this->filename = 'database/migrations/'
23
            . date('Y_m_d_His')
24
            . "_create_{$table}_table.php";
25
26
        if(! File::isDirectory(dirname(base_path($this->filename)))){
27
            File::makeDirectory(dirname(base_path($this->filename)), 0777, true, true);
28
        }
29
30
        File::put(base_path($this->filename), str_replace([
31
            '$classname$',
32
            '$table$',
33
            '$columns$'
34
        ], [
35
            'Create' . Str::plural($model) . 'Table',
36
            $table,
37
            rtrim($this->getColumns($request))
38
        ], File::get($stub)));
39
    }
40
41
    /**
42
     * @param $request
43
     * @return string
44
     */
45
    private function getColumns($request): string
46
    {
47
        $columns = RuleParser::fieldsToMigrationMethods(
48
            $this->getFields($request['fields'])
49
        );
50
51
        if (json_decode($request['options']['hasTimestamps'], false)) {
52
            $columns .= '$table->timestamps();';
53
        }
54
55
        return $columns;
56
    }
57
}
58