Completed
Push — master ( 250f6a...af675f )
by Sam
07:31
created

ViewFullForm   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 178
Duplicated Lines 17.42 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 31
loc 178
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators;
2
3
use Illuminate\Support\Str;
4
use Symfony\Component\Console\Input\InputOption;
5
6
class ViewFullForm extends ViewGenerator {
7
8
    /**
9
     * The name and signature of the console command.
10
     *
11
     * @var string
12
     */
13
    protected $name = 'jig:generate:full-form';
14
15
    /**
16
     * The console command description.
17
     *
18
     * @var string
19
     */
20
    protected $description = 'Generate a full-form view template';
21
22
    /**
23
     * Path for view
24
     *
25
     * @var string
26
     */
27
    protected string $view = 'full-form';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
28
29
    /**
30
     * Path for js view
31
     *
32
     * @var string
33
     */
34
    protected string $viewJs = 'form-js';
35
36
    /**
37
     * Name of view, will be used in directory
38
     *
39
     * @var string
40
     */
41
    protected string $fileName;
42
43
    /**
44
     * Route to process form
45
     *
46
     * @var string
47
     */
48
    protected string $route;
49
50
    /**
51
     * @var string
52
     */
53
    protected string $formJsRelativePath;
54
55
    /**
56
     * Execute the console command.
57
     *
58
     * @return void
59
     */
60
    public function handle()
61
    {
62
        $force = $this->option('force');
63
64
        //TODO check if exists
65
        //TODO make global for all generator
66
        //TODO also with prefix
67
        if(!empty($template = $this->option('template'))) {
68
            $this->view = 'templates.'.$template.'.full-form';
69
            $this->viewJs = 'templates.'.$template.'.form-js';
70
        }
71
72
        $this->fileName = $this->option('file-name') ?: $this->modelViewsDirectory;
73
        $this->formJsRelativePath = str_replace([DIRECTORY_SEPARATOR, '/', '\\'], '-', $this->fileName);
74
        if (!$this->option('file-name')) {
75
            $this->fileName = $this->fileName . DIRECTORY_SEPARATOR . 'form';
76
        }
77
78
        $this->route = $this->option('route');
79
        if (!$this->route){
80
            if ($this->option('file-name')){
81
                $this->route = 'admin/'.$this->fileName;
82
            } else {
83
                $this->route = 'admin/'.$this->resource.'/update';
84
            }
85
        }
86
87
        $viewPath = resource_path('views/admin/'.$this->fileName.'.blade.php');
88
        if ($this->alreadyExists($viewPath) && !$force) {
89
            $this->error('File '.$viewPath.' already exists!');
90
        } else {
91
            if ($this->alreadyExists($viewPath) && $force) {
92
                $this->warn('File '.$viewPath.' already exists! File will be deleted.');
93
                $this->files->delete($viewPath);
94
            }
95
96
            $this->makeDirectory($viewPath);
97
98
            $this->files->put($viewPath, $this->buildForm());
99
100
            $this->info('Generating '.$viewPath.' finished');
101
        }
102
103
        $formJsPath = resource_path('js/admin/'.$this->formJsRelativePath.'/Form.js');
104
        $bootstrapJsPath = resource_path('js/admin/index.js');
105
106
        if ($this->alreadyExists($formJsPath) && !$force) {
107
            $this->error('File '.$formJsPath.' already exists!');
108
        } else {
109
            if ($this->alreadyExists($formJsPath) && $force) {
110
                $this->warn('File '.$formJsPath.' already exists! File will be deleted.');
111
                $this->files->delete($formJsPath);
112
            }
113
114
            $this->makeDirectory($formJsPath);
115
116
            $this->files->put($formJsPath, $this->buildFormJs());
117
            $this->info('Generating '.$formJsPath.' finished');
118
119
        }
120
121
		$indexJsPath = resource_path('js/admin/'.$this->formJsRelativePath.'/index.js');
122
		if ($this->alreadyExists($indexJsPath) && !$force) {
123
			$this->error('File '.$indexJsPath.' already exists!');
124
		} else {
125
			if ($this->alreadyExists($indexJsPath) && $force) {
126
				$this->warn('File '.$indexJsPath.' already exists! File will be deleted.');
127
				$this->files->delete($indexJsPath);
128
			}
129
			$this->makeDirectory($indexJsPath);
130
		}
131
132
		if ($this->appendIfNotAlreadyAppended($indexJsPath, "import './Form';".PHP_EOL)){
133
			$this->info('Appending Form to '.$indexJsPath.' finished');
134
		};
135
		if ($this->appendIfNotAlreadyAppended($bootstrapJsPath, "import './". $this->formJsRelativePath ."';".PHP_EOL)){
136
			$this->info('Appending '.$this->formJsRelativePath.'/index.js to '.$bootstrapJsPath.' finished');
137
		};
138
139
    }
140
141
    protected function buildForm(): string
142
    {
143
144
        return view('jig::'.$this->view, [
145
            'modelBaseName' => $this->modelBaseName,
146
            'modelVariableName' => $this->modelVariableName,
147
            'route' => $this->route,
148
            'modelJSName' => $this->formJsRelativePath,
149
            'modelDotNotation' => $this->modelDotNotation,
150
            'modelLangFormat' => $this->modelLangFormat,
151
            'modelTitle' => $this->readColumnsFromTable($this->tableName)->filter(function($column){
152
                return in_array($column['name'], ['title', 'name', 'first_name', 'email']);
153
            })->first(null, ['name'=>'id'])['name'],
154
155
            'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName)->sortByDesc(function($column) {
156
                return $column['type'] == "json";
157
            }),
158
            'hasTranslatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
159
                return $column['type'] == "json";
160
            })->count() > 0,
161
            'translatableTextarea' => ['perex', 'text'],
162
            'relations' => $this->relations,
163
        ])->render();
164
    }
165
166
    protected function buildFormJs(): string
167
    {
168
        return view('jig::'.$this->viewJs, [
169
            'modelJSName' => $this->formJsRelativePath,
170
171
            'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName),
172
        ])->render();
173
    }
174
175
    protected function getOptions() {
176
        return [
177
            ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'],
178
            ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'],
179
            ['file-name', 'nm', InputOption::VALUE_OPTIONAL, 'Specify a blade file path'],
180
            ['route', 'r', InputOption::VALUE_OPTIONAL, 'Specify custom route for form'],
181
            ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating full form'],
182
        ];
183
    }
184
185
}
186