| Total Complexity | 42 |
| Total Lines | 335 |
| Duplicated Lines | 0 % |
| Changes | 21 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Rebuild often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Rebuild, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Rebuild extends Command |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * The name and signature of the console command. |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $signature = 'rebuild'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The console command description. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $description = 'Rebuild the app from scratch'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create a new command instance. |
||
| 26 | * |
||
| 27 | * @return void |
||
| 28 | */ |
||
| 29 | public function __construct(Composer $composer) |
||
| 30 | { |
||
| 31 | parent::__construct(); |
||
| 32 | |||
| 33 | $this->composer = $composer; |
||
|
|
|||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Execute the console command. |
||
| 38 | * |
||
| 39 | * @return mixed |
||
| 40 | */ |
||
| 41 | public function handle() |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Command operation sequence |
||
| 56 | * |
||
| 57 | * @return mixed |
||
| 58 | */ |
||
| 59 | protected function rebuildSequence() |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Set application to Maintenance Mode |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | protected function enterMaintenanceMode() |
||
| 92 | { |
||
| 93 | if (config('rebuild.should_enter_maintenance_mode')) { |
||
| 94 | $this->call('down'); |
||
| 95 | $this->line(''); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Rebuild database schema |
||
| 101 | * |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | protected function rebuildDatabaseSchema() |
||
| 105 | { |
||
| 106 | if (config('rebuild.should_rebuild_database_schema')) { |
||
| 107 | $this->call('migrate:fresh', ['--force' => true]); |
||
| 108 | $this->info('Rebuilding database schema is done.'); |
||
| 109 | $this->line(''); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Seeding initial data |
||
| 115 | * |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | protected function seedInitialData() |
||
| 119 | { |
||
| 120 | if (config('rebuild.should_seed_initial_data')) { |
||
| 121 | $this->composer->dumpAutoloads(); |
||
| 122 | |||
| 123 | $this->call('db:seed', ['--force' => true]); |
||
| 124 | $this->info('Seeding initial data is done.'); |
||
| 125 | $this->line(''); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Seeding dummy data |
||
| 131 | * |
||
| 132 | * @return void |
||
| 133 | */ |
||
| 134 | protected function seedDummyData() |
||
| 135 | { |
||
| 136 | if (config('rebuild.dummy.should_seed')) { |
||
| 137 | if ($this->confirm('Install dummy data?')) { |
||
| 138 | try { |
||
| 139 | $this->call('db:seed', [ |
||
| 140 | '--class' => config('rebuild.dummy.seeder_name'), |
||
| 141 | '--force' => true, |
||
| 142 | ]); |
||
| 143 | } catch (\Exception $e) { |
||
| 144 | $this->error($e->getMessage()); |
||
| 145 | $this->info('Seeding dummy data is aborted.'); |
||
| 146 | $this->line(''); |
||
| 147 | |||
| 148 | return true; |
||
| 149 | } |
||
| 150 | |||
| 151 | $this->info('Seeding dummy data is done.'); |
||
| 152 | $this->line(''); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Seeding example data |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | protected function seedExampleData() |
||
| 163 | { |
||
| 164 | if (config('rebuild.example.should_seed')) { |
||
| 165 | if ($this->confirm('Install example data?')) { |
||
| 166 | try { |
||
| 167 | $this->call('db:seed', [ |
||
| 168 | '--class' => config('rebuild.example.seeder_name'), |
||
| 169 | '--force' => true, |
||
| 170 | ]); |
||
| 171 | } catch (\Exception $e) { |
||
| 172 | $this->error($e->getMessage()); |
||
| 173 | $this->info('Seeding example data is aborted.'); |
||
| 174 | $this->line(''); |
||
| 175 | |||
| 176 | return true; |
||
| 177 | } |
||
| 178 | |||
| 179 | $this->info('Seeding example data is done.'); |
||
| 180 | $this->line(''); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Migrate sessions table |
||
| 187 | * |
||
| 188 | * @return void |
||
| 189 | */ |
||
| 190 | protected function migrateSessionsTable() |
||
| 191 | { |
||
| 192 | if (config('rebuild.should_migrate_sessions_table')) { |
||
| 193 | try { |
||
| 194 | $this->call('session:table'); |
||
| 195 | $this->call('migrate'); |
||
| 196 | } catch (\Exception $e) { |
||
| 197 | $this->error($e->getMessage()); |
||
| 198 | $this->info('Migrating sessions table is aborted.'); |
||
| 199 | $this->line(''); |
||
| 200 | |||
| 201 | return true; |
||
| 202 | } |
||
| 203 | |||
| 204 | $this->info('Migrating sessions table is done.'); |
||
| 205 | $this->line(''); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Migrate notifications table |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | protected function migrateNotificationsTable() |
||
| 215 | { |
||
| 216 | if (config('rebuild.should_migrate_notifications_table')) { |
||
| 217 | try { |
||
| 218 | $this->call('notification:table'); |
||
| 219 | $this->call('migrate'); |
||
| 220 | } catch (\Exception $e) { |
||
| 221 | $this->error($e->getMessage()); |
||
| 222 | $this->info('Migrating notifications table is aborted.'); |
||
| 223 | $this->line(''); |
||
| 224 | |||
| 225 | return true; |
||
| 226 | } |
||
| 227 | |||
| 228 | $this->info('Migrating notifications table is done.'); |
||
| 229 | $this->line(''); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Clear event |
||
| 235 | * |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | protected function clearEvent() |
||
| 239 | { |
||
| 240 | if (config('rebuild.should_clear_event')) { |
||
| 241 | $this->line('Run \'artisan event:clear\' command:'); |
||
| 242 | $this->call('event:clear'); |
||
| 243 | $this->line(''); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Clear these following cached files: |
||
| 249 | * Views, Application cache, Route cache, Configuration cache, and |
||
| 250 | * Compiled services and packages files |
||
| 251 | * |
||
| 252 | * @return void |
||
| 253 | */ |
||
| 254 | protected function clearFrameworkBootstrapFiles() |
||
| 255 | { |
||
| 256 | if (config('rebuild.should_clear_framework_bootstrap_files')) { |
||
| 257 | $this->line('Run \'artisan optimize:clear\' command:'); |
||
| 258 | $this->call('optimize:clear'); |
||
| 259 | $this->line(''); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Cache framework bootstrap files |
||
| 265 | * |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | protected function cacheFrameworkBootstrapFiles() |
||
| 269 | { |
||
| 270 | if (config('rebuild.should_cache_framework_bootstrap_files')) { |
||
| 271 | $this->line('Run \'artisan optimize\' command:'); |
||
| 272 | $this->call('optimize'); |
||
| 273 | $this->line(''); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Rediscover packages |
||
| 279 | * |
||
| 280 | * @return void |
||
| 281 | */ |
||
| 282 | protected function rediscoverPackages() |
||
| 283 | { |
||
| 284 | if (config('rebuild.should_rediscover_packages')) { |
||
| 285 | $this->line('Run \'artisan package:discover\' command:'); |
||
| 286 | $this->call('package:discover'); |
||
| 287 | $this->line(''); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Create symbolic link |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | protected function createSymbolicLink() |
||
| 297 | { |
||
| 298 | if (config('rebuild.should_create_symbolic_link')) { |
||
| 299 | $this->line('Run \'artisan storage:link\' command:'); |
||
| 300 | $this->call('storage:link'); |
||
| 301 | $this->line(''); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Run BeyondCode's Laravel Self-Diagnosis command |
||
| 307 | * more: https://github.com/beyondcode/laravel-self-diagnosis |
||
| 308 | * |
||
| 309 | * @return void |
||
| 310 | */ |
||
| 311 | protected function runSelfDiagnosis() |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Run application tests |
||
| 322 | * |
||
| 323 | * @return void |
||
| 324 | */ |
||
| 325 | protected function runApplicationTest() |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Wake up application from Maintenance Mode |
||
| 335 | * |
||
| 336 | * @return void |
||
| 337 | */ |
||
| 338 | protected function leaveMaintenanceMode() |
||
| 343 | } |
||
| 344 | } |
||
| 346 |