| Total Complexity | 46 |
| Total Lines | 372 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like UpdateNNTmux 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 UpdateNNTmux, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class UpdateNNTmux extends Command |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The name and signature of the console command. |
||
| 17 | */ |
||
| 18 | protected $signature = 'nntmux:all |
||
| 19 | {--skip-git : Skip git operations} |
||
| 20 | {--skip-composer : Skip composer update} |
||
| 21 | {--skip-npm : Skip npm operations} |
||
| 22 | {--skip-db : Skip database migrations} |
||
| 23 | {--force : Force update even if up-to-date}'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The console command description. |
||
| 27 | */ |
||
| 28 | protected $description = 'Update NNTmux installation with improved performance and error handling'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var bool Whether the app was in maintenance mode before we started |
||
| 32 | */ |
||
| 33 | private bool $wasInMaintenance = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool Whether tmux was running before we started |
||
| 37 | */ |
||
| 38 | private bool $tmuxWasRunning = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ProgressBar Progress bar for tracking operations |
||
| 42 | */ |
||
| 43 | private ProgressBar $progressBar; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Execute the console command. |
||
| 47 | */ |
||
| 48 | public function handle(): int |
||
| 49 | { |
||
| 50 | $this->info('🚀 Starting NNTmux update process...'); |
||
| 51 | |||
| 52 | // Initialize progress tracking |
||
| 53 | $totalSteps = $this->calculateTotalSteps(); |
||
| 54 | $this->progressBar = $this->output->createProgressBar($totalSteps); |
||
| 55 | $this->progressBar->start(); |
||
| 56 | |||
| 57 | try { |
||
| 58 | // Prepare environment |
||
| 59 | $this->prepareEnvironment(); |
||
| 60 | |||
| 61 | // Execute update steps |
||
| 62 | $this->executeUpdateSteps(); |
||
| 63 | |||
| 64 | // Finalize |
||
| 65 | $this->finalizeUpdate(); |
||
| 66 | |||
| 67 | $this->progressBar->finish(); |
||
| 68 | $this->newLine(2); |
||
| 69 | $this->info('✅ NNTmux update completed successfully!'); |
||
| 70 | |||
| 71 | return Command::SUCCESS; |
||
| 72 | |||
| 73 | } catch (\Exception $e) { |
||
| 74 | $this->progressBar->finish(); |
||
| 75 | $this->newLine(2); |
||
| 76 | $this->error('❌ Update failed: '.$e->getMessage()); |
||
| 77 | $this->restoreEnvironment(); |
||
| 78 | |||
| 79 | return Command::FAILURE; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Calculate total steps for progress tracking |
||
| 85 | */ |
||
| 86 | private function calculateTotalSteps(): int |
||
| 87 | { |
||
| 88 | $steps = 3; // prepare, finalize, cleanup |
||
| 89 | |||
| 90 | if (! $this->option('skip-git')) { |
||
| 91 | $steps++; |
||
| 92 | } |
||
| 93 | if (! $this->option('skip-composer')) { |
||
| 94 | $steps++; |
||
| 95 | } |
||
| 96 | if (! $this->option('skip-npm')) { |
||
| 97 | $steps += 2; |
||
| 98 | } // install + build |
||
| 99 | if (! $this->option('skip-db')) { |
||
| 100 | $steps++; |
||
| 101 | } |
||
| 102 | |||
| 103 | $steps++; // env merge |
||
| 104 | |||
| 105 | return $steps; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Prepare the environment for updates |
||
| 110 | */ |
||
| 111 | private function prepareEnvironment(): void |
||
| 112 | { |
||
| 113 | $this->info('🔧 Preparing environment...'); |
||
| 114 | |||
| 115 | // Check if app is in maintenance mode |
||
| 116 | $this->wasInMaintenance = App::isDownForMaintenance(); |
||
| 117 | if (! $this->wasInMaintenance) { |
||
| 118 | $this->call('down', [ |
||
| 119 | '--render' => 'errors::maintenance', |
||
| 120 | '--retry' => 120, |
||
| 121 | '--secret' => config('app.key'), |
||
| 122 | ]); |
||
| 123 | } |
||
| 124 | |||
| 125 | // Check if tmux is running |
||
| 126 | $tmux = new Tmux; |
||
| 127 | $this->tmuxWasRunning = $tmux->isRunning(); |
||
| 128 | if ($this->tmuxWasRunning) { |
||
| 129 | $this->call('tmux-ui:stop', ['--kill' => true]); |
||
| 130 | } |
||
| 131 | |||
| 132 | $this->progressBar->advance(); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Execute the main update steps |
||
| 137 | */ |
||
| 138 | private function executeUpdateSteps(): void |
||
| 139 | { |
||
| 140 | // Git operations |
||
| 141 | if (! $this->option('skip-git')) { |
||
| 142 | $this->performGitUpdate(); |
||
| 143 | } |
||
| 144 | |||
| 145 | // Composer operations |
||
| 146 | if (! $this->option('skip-composer')) { |
||
| 147 | $this->performComposerUpdate(); |
||
| 148 | } |
||
| 149 | |||
| 150 | // Database migrations |
||
| 151 | if (! $this->option('skip-db')) { |
||
| 152 | $this->performDatabaseUpdate(); |
||
| 153 | } |
||
| 154 | |||
| 155 | // NPM operations |
||
| 156 | if (! $this->option('skip-npm')) { |
||
| 157 | $this->performNpmOperations(); |
||
| 158 | } |
||
| 159 | |||
| 160 | // Clear caches and perform maintenance |
||
| 161 | $this->performMaintenanceTasks(); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Perform git update with better error handling |
||
| 166 | */ |
||
| 167 | private function performGitUpdate(): void |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Perform composer update with optimization |
||
| 182 | */ |
||
| 183 | private function performComposerUpdate(): void |
||
| 184 | { |
||
| 185 | $this->info('📦 Updating composer dependencies...'); |
||
| 186 | |||
| 187 | $composerResult = $this->call('nntmux:composer'); |
||
| 188 | |||
| 189 | if ($composerResult !== 0) { |
||
| 190 | throw new \Exception('Composer update failed'); |
||
| 191 | } |
||
| 192 | |||
| 193 | $this->progressBar->advance(); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Perform database updates |
||
| 198 | */ |
||
| 199 | private function performDatabaseUpdate(): void |
||
| 200 | { |
||
| 201 | $this->info('🗄️ Updating database...'); |
||
| 202 | |||
| 203 | $dbResult = $this->call('nntmux:db'); |
||
| 204 | |||
| 205 | if ($dbResult !== 0) { |
||
| 206 | throw new \Exception('Database update failed'); |
||
| 207 | } |
||
| 208 | |||
| 209 | $this->progressBar->advance(); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Perform NPM operations with parallel processing where possible |
||
| 214 | */ |
||
| 215 | private function performNpmOperations(): void |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Perform maintenance tasks |
||
| 258 | */ |
||
| 259 | private function performMaintenanceTasks(): void |
||
| 260 | { |
||
| 261 | // Clear view cache |
||
| 262 | $this->info('🧹 Clearing view cache...'); |
||
| 263 | $this->call('view:clear'); |
||
| 264 | |||
| 265 | // Merge environment variables |
||
| 266 | $this->info('⚙️ Merging environment configuration...'); |
||
| 267 | $this->mergeEnvironmentConfig(); |
||
| 268 | $this->progressBar->advance(); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Merge environment configuration with improved error handling |
||
| 273 | */ |
||
| 274 | private function mergeEnvironmentConfig(): void |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Parse environment file into key-value pairs |
||
| 313 | */ |
||
| 314 | private function parseEnvFile(string $path): array |
||
| 315 | { |
||
| 316 | $content = File::get($path); |
||
| 317 | $vars = []; |
||
| 318 | |||
| 319 | foreach (preg_split("/\r\n|\n|\r/", $content) as $line) { |
||
| 320 | $line = trim($line); |
||
| 321 | |||
| 322 | if (empty($line) || str_starts_with($line, '#')) { |
||
| 323 | continue; |
||
| 324 | } |
||
| 325 | |||
| 326 | if (preg_match('/^([^=]+)=(.*)$/', $line, $matches)) { |
||
| 327 | $key = trim($matches[1]); |
||
| 328 | $value = $matches[2]; |
||
| 329 | $vars[$key] = $value; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | return $vars; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Add missing environment variables to .env file |
||
| 338 | */ |
||
| 339 | private function addMissingEnvVars(string $envPath, array $missingKeys): void |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Finalize the update process |
||
| 358 | */ |
||
| 359 | private function finalizeUpdate(): void |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Restore the original environment state |
||
| 374 | */ |
||
| 375 | private function restoreEnvironment(): void |
||
| 385 | } |
||
| 386 | } |
||
| 388 |