NovaResourceGenerator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
c 1
b 0
f 0
dl 0
loc 115
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A populateStub() 0 9 1
A titleCase() 0 3 1
A buildDefinition() 0 30 4
A novaDataType() 0 22 1
A buildImports() 0 7 1
A getPath() 0 3 1
A novaData() 0 16 1
1
<?php
2
3
namespace PrismX\Generators\Generators;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Facades\File;
7
use PrismX\Generators\Support\Model;
8
use PrismX\Generators\Support\AbstractGenerator;
9
10
class NovaResourceGenerator extends AbstractGenerator
11
{
12
    protected $imports = [
13
        'Laravel\Nova\Fields\ID',
14
        'Illuminate\Http\Request',
15
    ];
16
17
    public function __construct(Model $model)
18
    {
19
        parent::__construct($model);
20
        $this->stub = File::get(STUBS_PATH.'/novaResource.stub');
21
    }
22
23
    protected function getPath(): string
24
    {
25
        return "app/Nova/{$this->model->name()}.php";
26
    }
27
28
    public function populateStub(): string
29
    {
30
        $stub = $this->stub;
31
        $stub = str_replace('{{ClassName}}', $this->model->name(), $stub);
32
        $stub = str_replace('{{ModelName}}', '\\'.config('generators.model_namespace')."\\{$this->model->name()}", $stub);
33
        $stub = str_replace('{{fields}}', $this->buildDefinition(), $stub);
34
        $stub = str_replace('{{imports}}', $this->buildImports(), $stub);
35
36
        return $stub;
37
    }
38
39
    protected function buildDefinition()
40
    {
41
        $definition = '';
42
43
        foreach ($this->model->columns() as $column) {
44
            if ($column->name() === 'id') {
45
                continue;
46
            }
47
48
            if ($column->dataType() === 'id') {
49
                $name = Str::substr($column->name(), 0, -3);
50
                $class = Str::studly($column->attributes()[0] ?? $name);
51
52
                $definition .= self::INDENT;
53
                $definition .= sprintf("BelongsTo::make('%s')", $class);
54
                $definition .= ','.PHP_EOL;
55
56
                $this->imports[] = 'Laravel\Nova\Fields\BelongsTo';
57
            } else {
58
                $fieldType = $this->novaData($column->name()) ?? $this->novaDataType($column->dataType());
59
60
                $definition .= self::INDENT;
61
                $definition .= sprintf("%s::make('%s')", $fieldType, $this->titleCase($column->name()));
62
                $definition .= ','.PHP_EOL;
63
64
                $this->imports[] = "Laravel\Nova\Fields\\{$fieldType}";
65
            }
66
        }
67
68
        return trim($definition);
69
    }
70
71
    protected function titleCase($string)
72
    {
73
        return Str::title(str_replace('_', ' ', $string));
74
    }
75
76
    protected function buildImports()
77
    {
78
        return collect($this->imports)->unique()->sort(function ($a, $b) {
79
            return strlen($a) - strlen($b);
80
        })->map(function ($import) {
81
            return "use {$import};";
82
        })->implode(PHP_EOL);
83
    }
84
85
    protected function novaData(string $name)
86
    {
87
        $novaNames = [
88
            'country' => 'Country',
89
            'currency' => 'Currency',
90
            'password' => 'Password',
91
            'image' => 'Image',
92
            'picture' => 'Image',
93
            'avatar' => 'Avatar',
94
            'file' => 'File',
95
            'address' => 'Place',
96
            'address1' => 'Place',
97
            'address2' => 'Place',
98
        ];
99
100
        return $novaNames[$name] ?? null;
101
    }
102
103
    protected function novaDataType(string $type)
104
    {
105
        $novaTypes = [
106
            'id' => 'ID',
107
            'string' => 'Text',
108
            'text' => 'Trix',
109
            'longText' => 'Trix',
110
            'date' => 'Date',
111
            'time' => 'Time',
112
            'guid' => 'Text',
113
            'datetimetz' => 'DateTime',
114
            'datetime' => 'DateTime',
115
            'timestamp' => 'DateTime',
116
            'integer' => 'Number',
117
            'bigint' => 'Number',
118
            'smallint' => 'Number',
119
            'decimal' => 'Number',
120
            'float' => 'Number',
121
            'boolean' => 'Boolean',
122
        ];
123
124
        return $novaTypes[$type] ?? 'Text';
125
    }
126
}
127