Passed
Push — master ( 26c354...92479f )
by Richard
05:49 queued 10s
created

BaseCommand::generateScaffoldItems()   C

Complexity

Conditions 13
Paths 64

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 18
c 3
b 0
f 0
dl 0
loc 30
rs 6.6166
cc 13
nc 64
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PWWEB\Artomator\Commands;
4
5
use Illuminate\Support\Str;
6
use InfyOm\Generator\Commands\BaseCommand as Base;
7
use InfyOm\Generator\Generators\Scaffold\MenuGenerator;
8
use InfyOm\Generator\Generators\Scaffold\RequestGenerator;
9
use InfyOm\Generator\Utils\FileUtil;
10
use PWWEB\Artomator\Generators\ContractGenerator;
11
use PWWEB\Artomator\Generators\GraphQL\GraphQLInputGenerator;
12
use PWWEB\Artomator\Generators\GraphQL\GraphQLMutationGenerator;
13
use PWWEB\Artomator\Generators\GraphQL\GraphQLQueryGenerator;
14
use PWWEB\Artomator\Generators\GraphQL\GraphQLSubscriptionGenerator;
15
use PWWEB\Artomator\Generators\GraphQL\GraphQLTypeGenerator;
16
use PWWEB\Artomator\Generators\Scaffold\ControllerGenerator;
17
use PWWEB\Artomator\Generators\Scaffold\RoutesGenerator;
18
use PWWEB\Artomator\Generators\Scaffold\ViewGenerator;
19
use PWWEB\Artomator\Generators\Scaffold\VueGenerator;
20
use Symfony\Component\Console\Input\InputOption;
21
22
class BaseCommand extends Base
23
{
24
    /**
25
     * Handle.
26
     *
27
     * @return void
28
     */
29
    public function handle()
30
    {
31
        parent::handle();
32
        $this->commandData->config->prepareGraphQLNames($this->option('gqlName'));
33
        $this->commandData = $this->commandData->config->loadDynamicGraphQLVariables($this->commandData);
34
    }
35
36
    /**
37
     * Generate Common Items.
38
     *
39
     * @return void
40
     */
41
    public function generateCommonItems()
42
    {
43
        parent::generateCommonItems();
44
45
        if (false === $this->isSkip('repository') && true === $this->commandData->getOption('repositoryPattern')) {
46
            $contractGenerator = new ContractGenerator($this->commandData);
47
            $contractGenerator->generate();
48
        }
49
    }
50
51
    /**
52
     * Generate GraphQL Items.
53
     *
54
     * @return void
55
     */
56
    public function generateGraphQLItems()
57
    {
58
        if (false === ($this->isSkip('queries') or $this->isSkip('graphql_query'))) {
59
            $queryGenerator = new GraphQLQueryGenerator($this->commandData);
60
            $queryGenerator->generate();
61
        }
62
63
        if (false === ($this->isSkip('types') or $this->isSkip('graphql_types'))) {
64
            $typeGenerator = new GraphQLTypeGenerator($this->commandData);
65
            $typeGenerator->generate();
66
        }
67
68
        if (false === ($this->isSkip('mutations') or $this->isSkip('graphql_mutations'))) {
69
            $mutationGenerator = new GraphQLMutationGenerator($this->commandData);
70
            $mutationGenerator->generate();
71
        }
72
73
        if (false === ($this->isSkip('inputs') or $this->isSkip('graphql_inputs'))) {
74
            $mutationGenerator = new GraphQLInputGenerator($this->commandData);
75
            $mutationGenerator->generate();
76
        }
77
78
        if ((false === ($this->isSkip('subscription') or $this->isSkip('graphql_subscription'))) and config('pwweb.artomator.options.subscription')) {
79
            $subscriptionGenerator = new GraphQLSubscriptionGenerator($this->commandData);
80
            $subscriptionGenerator->generate();
81
        }
82
    }
83
84
    /**
85
     * Generate Scaffold Items.
86
     *
87
     * @return void
88
     */
89
    public function generateScaffoldItems()
90
    {
91
        if (false === $this->isSkip('requests') and false === $this->isSkip('scaffold_requests')) {
92
            $requestGenerator = new RequestGenerator($this->commandData);
93
            $requestGenerator->generate();
94
        }
95
96
        if (false === $this->isSkip('controllers') and false === $this->isSkip('scaffold_controller')) {
97
            $controllerGenerator = new ControllerGenerator($this->commandData);
98
            $controllerGenerator->generate();
99
        }
100
101
        if (false === $this->isSkip('views') and false === $this->commandData->getOption('vue')) {
102
            $viewGenerator = new ViewGenerator($this->commandData);
103
            $viewGenerator->generate();
104
        }
105
106
        if (false === $this->isSkip('views') and true === $this->commandData->getOption('vue')) {
107
            $vueGenerator = new VueGenerator($this->commandData);
108
            $vueGenerator->generate();
109
        }
110
111
        if (false === $this->isSkip('routes') and false === $this->isSkip('scaffold_routes')) {
112
            $routeGenerator = new RoutesGenerator($this->commandData);
113
            $routeGenerator->generate();
114
        }
115
116
        if (false === $this->isSkip('menu') and $this->commandData->config->getAddOn('menu.enabled')) {
117
            $menuGenerator = new MenuGenerator($this->commandData);
118
            $menuGenerator->generate();
119
        }
120
    }
121
122
    /**
123
     * Get the console command options.
124
     *
125
     * @return array
126
     */
127
    public function getOptions()
128
    {
129
        return array_merge(
130
            parent::getOptions(),
131
            [
132
                ['gqlName', null, InputOption::VALUE_REQUIRED, 'Override the name used in the GraphQL schema file'],
133
                ['vue', null, InputOption::VALUE_NONE, 'Generate Vuejs views rather than blade views'],
134
            ]
135
        );
136
    }
137
138
    /**
139
     * Perform the Post Generator Actions.
140
     *
141
     * @param bool $runMigration Boolean flag to run migrations.
142
     *
143
     * @return void
144
     */
145
    public function performPostActions($runMigration = false)
146
    {
147
        if (true === $this->commandData->getOption('save')) {
148
            $this->saveSchemaFile();
149
        }
150
151
        if (true === $runMigration) {
152
            if (true === $this->commandData->getOption('forceMigrate')) {
153
                $this->runMigration();
154
            } elseif (false === $this->commandData->getOption('fromTable') && false === $this->isSkip('migration')) {
155
                $requestFromConsole = ('cli' === php_sapi_name()) ? true : false;
156
                if (true === $this->commandData->getOption('jsonFromGUI') && true === $requestFromConsole) {
157
                    $this->runMigration();
158
                } elseif (true === $requestFromConsole && true === $this->confirm("\nDo you want to migrate database? [y|N]", false)) {
159
                    $this->runMigration();
160
                }
161
            }
162
        }
163
164
        if (true === $this->commandData->getOption('localized')) {
165
            $this->saveLocaleFile();
166
        }
167
168
        if (false === $this->isSkip('dump-autoload')) {
169
            $this->info('Generating autoload files');
170
            $this->composer->dumpOptimized();
171
        }
172
    }
173
174
    /**
175
     * Save the Schema File.
176
     *
177
     * @return void
178
     */
179
    private function saveSchemaFile()
180
    {
181
        $fileFields = [];
182
183
        foreach ($this->commandData->fields as $field) {
184
            $fileFields[] = [
185
                'name'        => $field->name,
186
                'dbType'      => $field->dbInput,
187
                'htmlType'    => $field->htmlInput,
188
                'validations' => $field->validations,
189
                'searchable'  => $field->isSearchable,
190
                'fillable'    => $field->isFillable,
191
                'primary'     => $field->isPrimary,
192
                'inForm'      => $field->inForm,
193
                'inIndex'     => $field->inIndex,
194
                'inView'      => $field->inView,
195
            ];
196
        }
197
198
        foreach ($this->commandData->relations as $relation) {
199
            $fileFields[] = [
200
                'type'     => 'relation',
201
                'relation' => $relation->type.','.implode(',', $relation->inputs),
202
            ];
203
        }
204
205
        $path = $this->commandData->config->pathSchemas;
206
207
        $fileName = $this->commandData->modelName.'.json';
208
209
        if (true === file_exists($path.$fileName) && false === $this->confirmOverwrite($fileName)) {
210
            return;
211
        }
212
        FileUtil::createFile($path, $fileName, json_encode($fileFields, JSON_PRETTY_PRINT));
213
        $this->commandData->commandComment("\nSchema File saved: ");
214
        $this->commandData->commandInfo($fileName);
215
    }
216
217
    /**
218
     * Save the Locale File.
219
     *
220
     * @return void
221
     */
222
    private function saveLocaleFile()
223
    {
224
        $locales = [
225
            'singular' => $this->commandData->modelName,
226
            'plural'   => $this->commandData->config->mPlural,
227
            'fields'   => [],
228
        ];
229
230
        foreach ($this->commandData->fields as $field) {
231
            $locales['fields'][$field->name] = Str::title(str_replace('_', ' ', $field->name));
232
        }
233
234
        $path = $this->commandData->config->pathLocales;
235
236
        $fileName = $this->commandData->config->mCamelPlural.'.php';
237
238
        if (true === file_exists($path.$fileName) && false === $this->confirmOverwrite($fileName)) {
239
            return;
240
        }
241
        $content = "<?php\n\nreturn ".var_export($locales, true).';'.\PHP_EOL;
242
        FileUtil::createFile($path, $fileName, $content);
243
        $this->commandData->commandComment("\nModel Locale File saved: ");
244
        $this->commandData->commandInfo($fileName);
245
    }
246
}
247