Completed
Push — master ( b8fc4b...08375c )
by Mohamed
10:19
created

ResourceCommand::createFormRequests()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace Microboard\Commands;
4
5
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
9
class ResourceCommand extends WorkingWithStubs
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'microboard:resource
17
                            {model : The name of resource class}
18
                            {--views-directory=admin}
19
                            {--view-namespace=}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Command description';
27
28
    /**
29
     * Execute the console command.
30
     *
31
     * @return mixed
32
     */
33
    public function handle()
34
    {
35
        $model = $this->qualifyClass($this->getNameInput());
36
        $this->setVariables($model);
37
38
        // create permissions for this model
39
        // create policy file
40
        // create datatable file
41
        try {
42
            $this->createLanguageFiles();
43
            $this->createDatatable();
44
            $this->createController();
45
            $this->createFormRequests();
46
            $this->createResourceViews();
47
        } catch (FileNotFoundException $e) {
48
            $this->error($e->getMessage());
49
        }
50
    }
51
52
    /**
53
     * Get the desired class name from the input.
54
     *
55
     * @return string
56
     */
57
    protected function getNameInput()
58
    {
59
        return trim($this->argument('model'));
60
    }
61
62
    /**
63
     * Set model variables
64
     *
65
     * @var string $model
66
     */
67
    public function setVariables($model)
68
    {
69
        $base_name = class_basename($model);
70
71
        $this->variables = [
72
            '%NAMESPACE%' => $this->rootNamespace(),
73
            '%RESOURCE_MODEL%' => $model,
74
            '%RESOURCE_MODEL_BASE%' => class_basename($model),
75
            '%RESOURCE_NAME_PLURAL%' => (string)Str::of($base_name)->snake()->plural(),
76
            '%RESOURCE_NAME_SINGULAR%' => (string)Str::of($base_name)->snake()->singular(),
77
            '%VIEW_NAMESPACE%' => $this->option('view-namespace'),
78
            '%VIEW_DIRECTORY%' => $this->option('views-directory') . '/' . ((string)Str::of($base_name)->snake()->plural()) . '/',
79
            '%BLADE_VIEW_DIRECTORY%' => $this->option('views-directory') . '.' . ((string)Str::of($base_name)->snake()->plural()),
80
            '%COLUMNS%' => (new $model)->getFillable()
81
        ];
82
    }
83
84
    /**
85
     * Create resource views
86
     *
87
     * @throws FileNotFoundException
88
     */
89
    private function createResourceViews()
90
    {
91
        $transPrefix = $this->variables['%VIEW_NAMESPACE%'] . $this->variables['%RESOURCE_NAME_PLURAL%'];
92
        $columns = '';
93
94 View Code Duplication
        foreach ($this->variables['%COLUMNS%'] as $column) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
            if (in_array($column, (new $this->variables['%RESOURCE_MODEL%'])->getHidden())) {
96
                continue;
97
            }
98
99
            $columns .= "\n\t\t\t\t\t<tr><th width=\"25%\">@lang('{$transPrefix}.fields.{$column}')</th>" .
100
                "<td>{{ \${$this->variables['%RESOURCE_NAME_SINGULAR%']}->{$column} }}</td></tr>";
101
        }
102
103
        foreach (['index', 'show', 'edit', 'create'] as $view) {
104
            $this->createFromStub(
105
                __DIR__ . '/../../stubs/resource/' . $view . '.stub',
106
                'resources/views/' . $this->variables['%VIEW_DIRECTORY%'],
107
                $view . '.blade.php',
108
                array_merge($this->variables, ['%COLUMNS%' => $columns])
109
            );
110
        }
111
112
        $this->createFromStub(
113
            __DIR__ . '/../../stubs/resource/form.stub',
114
            'resources/views/' . $this->variables['%VIEW_DIRECTORY%'],
115
            'form.blade.php',
116
            array_merge($this->variables, $this->buildFormInputs())
117
        );
118
    }
119
120
    /**
121
     * @return string[]
122
     * @throws FileNotFoundException
123
     */
124
    private function buildFormInputs()
125
    {
126
        $columns = '';
127
128
        foreach ($this->variables['%COLUMNS%'] as $column) {
129
            if (in_array($column, (new $this->variables['%RESOURCE_MODEL%'])->getHidden())) {
130
                continue;
131
            }
132
133
            $this->variables['%COLUMN_NAME%'] = $column;
134
135
            $variables = Arr::only($this->variables, [
136
                '%RESOURCE_NAME_SINGULAR%', '%VIEW_NAMESPACE%', '%RESOURCE_NAME_PLURAL%', '%COLUMN_NAME%'
137
            ]);
138
139
            $columns .= str_replace(
140
                array_keys($variables),
141
                array_values($variables),
142
                $this->files->get(__DIR__ . '/../../stubs/resource/form-input.stub')
143
            );
144
        }
145
146
        return [
147
            '%COLUMNS%' => $columns
148
        ];
149
    }
150
151
    /**
152
     * Create lang file for this model
153
     */
154
    private function createLanguageFiles()
155
    {
156
        $columns = "\n\t\t'id' => '#',";
157
158 View Code Duplication
        foreach ($this->variables['%COLUMNS%'] as $column) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
            if (in_array($column, (new $this->variables['%RESOURCE_MODEL%'])->getHidden())) {
160
                continue;
161
            }
162
163
            $columns .= "\n\t\t'{$column}' => '{$column}',";
164
        }
165
166
        $this->createFromStub(
167
            __DIR__ . '/../../stubs/resource.stub',
168
            'resources/lang/ar/',
169
            $this->variables['%RESOURCE_NAME_PLURAL%'] . '.php',
170
            array_merge($this->variables, ['%COLUMNS%' => $columns])
171
        );
172
    }
173
174
    /**
175
     * Create datatable class
176
     */
177
    private function createDatatable()
178
    {
179
        $transPrefix = $this->variables['%VIEW_NAMESPACE%'] . $this->variables['%RESOURCE_NAME_PLURAL%'];
180
        $columns = "\n\t\t\tColumn::make('id')->title(trans('{$transPrefix}.fields.id'))->width('1%'),";
181
182 View Code Duplication
        foreach ($this->variables['%COLUMNS%'] as $column) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
            if (in_array($column, (new $this->variables['%RESOURCE_MODEL%'])->getHidden())) {
184
                continue;
185
            }
186
187
            $columns .= "\n\t\t\tColumn::make('{$column}')->title(trans('{$transPrefix}.fields.{$column}')),";
188
        }
189
190
        $this->createFromStub(
191
            __DIR__ . '/../../stubs/resource-datatable.stub',
192
            'app/DataTables/',
193
            Str::studly($this->variables['%RESOURCE_NAME_SINGULAR%'] . 'DataTable') . '.php',
194
            array_merge($this->variables, ['%COLUMNS%' => $columns])
195
        );
196
    }
197
198
    /**
199
     * Create datatable class
200
     */
201
    private function createController()
202
    {
203
        $this->createFromStub(
204
            __DIR__ . '/../../stubs/resource-controller.stub',
205
            'app/Http/Controllers/',
206
            Str::studly($this->variables['%RESOURCE_NAME_SINGULAR%'] . 'Controller') . '.php',
207
            Arr::only($this->variables, [
208
                '%RESOURCE_MODEL%', '%RESOURCE_NAME_PLURAL%', '%RESOURCE_NAME_SINGULAR%',
209
                '%RESOURCE_MODEL_BASE%', '%NAMESPACE%', '%VIEW_NAMESPACE%', '%BLADE_VIEW_DIRECTORY%'
210
            ])
211
        );
212
    }
213
214
    /**
215
     * Create datatable class
216
     */
217
    private function createFormRequests()
218
    {
219
        $columns = '';
220
221
        foreach ($this->variables['%COLUMNS%'] as $column) {
222
            $columns .= "\n\t\t\t'{$column}' => ['nullable'],";
223
        }
224
225
        foreach (['store', 'update'] as $method) {
226
            $this->createFromStub(
227
                __DIR__ . '/../../stubs/' . $method . '-resource-form-request.stub',
228
                'app/Http/Requests/' . $this->variables['%RESOURCE_MODEL_BASE%'] . '/',
229
                Str::studly($method . 'FormRequest') . '.php',
230
                array_merge($this->variables, ['%COLUMNS%' => $columns])
231
            );
232
        }
233
    }
234
}
235