| Conditions | 20 |
| Paths | > 20000 |
| Total Lines | 159 |
| Code Lines | 100 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 85 | public function store(Request $request) |
||
| 86 | { |
||
| 87 | $paths = []; |
||
| 88 | $message = ''; |
||
| 89 | |||
| 90 | try { |
||
| 91 | $model_name = Str::camel( |
||
| 92 | ucfirst(str_singular(lcfirst(ucwords($request->get('model_name'))))) |
||
| 93 | ); |
||
| 94 | |||
| 95 | $force = false; |
||
| 96 | if (in_array('force', $request->get('create'))) { |
||
| 97 | $force = true; |
||
| 98 | } |
||
| 99 | |||
| 100 | $presenter = $validator = 'no'; |
||
| 101 | $fillable = $rules = ''; |
||
| 102 | $fields = $request->get('fields'); |
||
| 103 | |||
| 104 | if (!empty($fields)) { |
||
| 105 | foreach ($fields as $index => $field) { |
||
| 106 | if ($field['name'] && $field['type']) { |
||
| 107 | $fillable .= $field['name'].':'.$field['type']; |
||
| 108 | if (isset($field['nullable'])) { |
||
| 109 | $fillable .= ':nullable()'; |
||
| 110 | } |
||
| 111 | if ($field['key']) { |
||
| 112 | $fillable .= ':'.$field['key']; |
||
| 113 | } |
||
| 114 | if ($field['default']) { |
||
| 115 | $fillable .= ':default'."('{$field['default']}')"; |
||
| 116 | } |
||
| 117 | if ($field['comment']) { |
||
| 118 | $fillable .= ':comment'."('{$field['comment']}')"; |
||
| 119 | } |
||
| 120 | $fillable .= ','; |
||
| 121 | if ($field['rule']) { |
||
| 122 | $rules .= $field['name'].'=>'.$field['rule'].','; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $fillable = rtrim($fillable, ','); |
||
| 129 | |||
| 130 | $rules = rtrim($rules, ','); |
||
| 131 | |||
| 132 | // 1. Create presenter. |
||
| 133 | if (in_array('presenter', $request->get('create'))) { |
||
| 134 | Artisan::call('yl:presenter', [ |
||
| 135 | 'name' => $model_name, |
||
| 136 | '--force' => $force, |
||
| 137 | ]); |
||
| 138 | $paths['presenters'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('presenters', true).'/'.$this->getName($model_name).'Presenter.php'; |
||
| 139 | |||
| 140 | Artisan::call('yl:transformer', [ |
||
| 141 | 'name' => $model_name, |
||
| 142 | '--fields' => $fields, |
||
| 143 | '--force' => $force, |
||
| 144 | ]); |
||
| 145 | $paths['transformers'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('transformers', true).'/'.$this->getName($model_name).'Transformer.php'; |
||
| 146 | |||
| 147 | $presenter = 'yes'; |
||
| 148 | } |
||
| 149 | |||
| 150 | // 2. Create validator. |
||
| 151 | if (in_array('validator', $request->get('create'))) { |
||
| 152 | Artisan::call('yl:validator', [ |
||
| 153 | 'name' => $model_name, |
||
| 154 | '--rules' => $rules, |
||
| 155 | '--force' => $force, |
||
| 156 | ]); |
||
| 157 | $paths['validators'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('validators', true).'/'.$this->getName($model_name).'Validator.php'; |
||
| 158 | $validator = 'yes'; |
||
| 159 | } |
||
| 160 | |||
| 161 | // 3. Create controller. |
||
| 162 | if (in_array('controller', $request->get('create'))) { |
||
| 163 | Artisan::call('yl:controller', [ |
||
| 164 | 'name' => $model_name, |
||
| 165 | '--fields' => $fields, |
||
| 166 | '--force' => $force, |
||
| 167 | ]); |
||
| 168 | $paths['controllers'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('controllers', true).'/'.Str::plural($this->getName($model_name)).'Controller.php'; |
||
| 169 | $paths['create_request'] = $this->getBasePath().'/Http/Requests/'.$this->getName($model_name).'CreateRequest.php'; |
||
| 170 | $paths['update_request'] = $this->getBasePath().'/Http/Requests/'.$this->getName($model_name).'UpdateRequest.php'; |
||
| 171 | } |
||
| 172 | |||
| 173 | // 4. Create views. |
||
| 174 | if (in_array('views', $request->get('create'))) { |
||
| 175 | Artisan::call('yl:views', [ |
||
| 176 | 'name' => $model_name, |
||
| 177 | '--fields' => $fields, |
||
| 178 | '--force' => $force, |
||
| 179 | ]); |
||
| 180 | $paths['views_index'] = resource_path($this->getConfigGeneratorClassPath('views', true)).'/'.$this->getSnakeName($model_name).'/index.blade.php'; |
||
| 181 | $paths['views_create'] = resource_path($this->getConfigGeneratorClassPath('views', true)).'/'.$this->getSnakeName($model_name).'/create.blade.php'; |
||
| 182 | $paths['views_edit'] = resource_path($this->getConfigGeneratorClassPath('views', true)).'/'.$this->getSnakeName($model_name).'/edit.blade.php'; |
||
| 183 | } |
||
| 184 | |||
| 185 | // 5. Create language package. |
||
| 186 | if (in_array('lang', $request->get('create'))) { |
||
| 187 | Artisan::call('yl:lang', [ |
||
| 188 | 'name' => $model_name, |
||
| 189 | '--fields' => $fields, |
||
| 190 | '--force' => $force, |
||
| 191 | ]); |
||
| 192 | $filesystem = new Filesystem(); |
||
| 193 | $directories = $filesystem->directories(resource_path($this->getConfigGeneratorClassPath('lang', true))); |
||
| 194 | foreach ($directories as $index => $directory) { |
||
| 195 | $paths['lang_'.$index] = $directory.'/'.$this->getSnakeName($model_name).'.php'; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | // 6. Create api controller. |
||
| 200 | if (in_array('api_controller', $request->get('create'))) { |
||
| 201 | Artisan::call('yl:api_controller', [ |
||
| 202 | 'name' => $model_name, |
||
| 203 | '--fields' => $fields, |
||
| 204 | '--force' => $force, |
||
| 205 | ]); |
||
| 206 | $paths['api_controllers'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('api_controllers', true).'/'.Str::plural($this->getName($model_name)).'Controller.php'; |
||
| 207 | $paths['api_create_request'] = $this->getBasePath().'/Http/Requests/Api/'.$this->getName($model_name).'CreateRequest.php'; |
||
| 208 | $paths['api_update_request'] = $this->getBasePath().'/Http/Requests/Api/'.$this->getName($model_name).'UpdateRequest.php'; |
||
| 209 | } |
||
| 210 | |||
| 211 | //7. Create repository |
||
| 212 | Artisan::call('yl:repository', [ |
||
| 213 | 'name' => $model_name, |
||
| 214 | '--fillable' => $fillable, |
||
| 215 | '--rules' => $rules, |
||
| 216 | '--fields' => $fields, |
||
| 217 | '--validator' => $validator, |
||
| 218 | '--presenter' => $presenter, |
||
| 219 | '--force' => $force, |
||
| 220 | ]); |
||
| 221 | $paths['repositories'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('repositories', true).'/'.$this->getName($model_name).'RepositoryEloquent.php'; |
||
| 222 | $paths['interfaces'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('interfaces', true).'/'.$this->getName($model_name).'Repository.php'; |
||
| 223 | $paths['models'] = $this->getBasePath().'/'.$this->getConfigGeneratorClassPath('models', true).'/'.($model_name).'.php'; |
||
| 224 | |||
| 225 | //8. Bind repository |
||
| 226 | Artisan::call('yl:bindings', [ |
||
| 227 | 'name' => $model_name, |
||
| 228 | '--force' => $force, |
||
| 229 | ]); |
||
| 230 | |||
| 231 | // 9. Run migrate. |
||
| 232 | if (in_array('migrate', $request->get('create'))) { |
||
| 233 | Artisan::call('migrate'); |
||
| 234 | $message = Artisan::output(); |
||
| 235 | } |
||
| 236 | } catch (\Exception $exception) { |
||
| 237 | // Delete generated files if exception thrown. |
||
| 238 | app('files')->delete($paths); |
||
| 239 | |||
| 240 | return $this->backWithException($exception); |
||
| 241 | } |
||
| 242 | |||
| 243 | return $this->backWithSuccess($model_name, $paths, $message); |
||
| 244 | } |
||
| 379 |