| Total Complexity | 49 |
| Total Lines | 383 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 14 | class UpdateNNTmux extends Command |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The name and signature of the console command. |
||
| 18 | */ |
||
| 19 | protected $signature = 'nntmux:all |
||
| 20 | {--skip-git : Skip git operations} |
||
| 21 | {--skip-composer : Skip composer update} |
||
| 22 | {--skip-npm : Skip npm operations} |
||
| 23 | {--skip-db : Skip database migrations} |
||
| 24 | {--force : Force update even if up-to-date}'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The console command description. |
||
| 28 | */ |
||
| 29 | protected $description = 'Update NNTmux installation with improved performance and error handling'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var bool Whether the app was in maintenance mode before we started |
||
| 33 | */ |
||
| 34 | private bool $wasInMaintenance = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var bool Whether tmux was running before we started |
||
| 38 | */ |
||
| 39 | private bool $tmuxWasRunning = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var ProgressBar Progress bar for tracking operations |
||
| 43 | */ |
||
| 44 | private ProgressBar $progressBar; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Execute the console command. |
||
| 48 | */ |
||
| 49 | public function handle(): int |
||
| 50 | { |
||
| 51 | $this->info('🚀 Starting NNTmux update process...'); |
||
| 52 | |||
| 53 | // Initialize progress tracking |
||
| 54 | $totalSteps = $this->calculateTotalSteps(); |
||
| 55 | $this->progressBar = $this->output->createProgressBar($totalSteps); |
||
| 56 | $this->progressBar->start(); |
||
| 57 | |||
| 58 | try { |
||
| 59 | // Prepare environment |
||
| 60 | $this->prepareEnvironment(); |
||
| 61 | |||
| 62 | // Execute update steps |
||
| 63 | $this->executeUpdateSteps(); |
||
| 64 | |||
| 65 | // Finalize |
||
| 66 | $this->finalizeUpdate(); |
||
| 67 | |||
| 68 | $this->progressBar->finish(); |
||
| 69 | $this->newLine(2); |
||
| 70 | $this->info('✅ NNTmux update completed successfully!'); |
||
| 71 | |||
| 72 | return Command::SUCCESS; |
||
| 73 | |||
| 74 | } catch (\Exception $e) { |
||
| 75 | $this->progressBar->finish(); |
||
| 76 | $this->newLine(2); |
||
| 77 | $this->error('❌ Update failed: ' . $e->getMessage()); |
||
| 78 | $this->restoreEnvironment(); |
||
| 79 | |||
| 80 | return Command::FAILURE; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Calculate total steps for progress tracking |
||
| 86 | */ |
||
| 87 | private function calculateTotalSteps(): int |
||
| 88 | { |
||
| 89 | $steps = 3; // prepare, finalize, cleanup |
||
| 90 | |||
| 91 | if (!$this->option('skip-git')) $steps++; |
||
| 92 | if (!$this->option('skip-composer')) $steps++; |
||
| 93 | if (!$this->option('skip-npm')) $steps += 2; // install + build |
||
| 94 | if (!$this->option('skip-db')) $steps++; |
||
| 95 | |||
| 96 | $steps++; // smarty cache clear |
||
| 97 | $steps++; // env merge |
||
| 98 | |||
| 99 | return $steps; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Prepare the environment for updates |
||
| 104 | */ |
||
| 105 | private function prepareEnvironment(): void |
||
| 106 | { |
||
| 107 | $this->info('🔧 Preparing environment...'); |
||
| 108 | |||
| 109 | // Check if app is in maintenance mode |
||
| 110 | $this->wasInMaintenance = App::isDownForMaintenance(); |
||
| 111 | if (!$this->wasInMaintenance) { |
||
| 112 | $this->call('down', [ |
||
| 113 | '--render' => 'errors::maintenance', |
||
| 114 | '--retry' => 120, |
||
| 115 | '--secret' => config('app.key') |
||
| 116 | ]); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Check if tmux is running |
||
| 120 | $tmux = new Tmux(); |
||
| 121 | $this->tmuxWasRunning = $tmux->isRunning(); |
||
| 122 | if ($this->tmuxWasRunning) { |
||
| 123 | $this->call('tmux-ui:stop', ['--kill' => true]); |
||
| 124 | } |
||
| 125 | |||
| 126 | $this->progressBar->advance(); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Execute the main update steps |
||
| 131 | */ |
||
| 132 | private function executeUpdateSteps(): void |
||
| 133 | { |
||
| 134 | // Git operations |
||
| 135 | if (!$this->option('skip-git')) { |
||
| 136 | $this->performGitUpdate(); |
||
| 137 | } |
||
| 138 | |||
| 139 | // Composer operations |
||
| 140 | if (!$this->option('skip-composer')) { |
||
| 141 | $this->performComposerUpdate(); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Database migrations |
||
| 145 | if (!$this->option('skip-db')) { |
||
| 146 | $this->performDatabaseUpdate(); |
||
| 147 | } |
||
| 148 | |||
| 149 | // NPM operations |
||
| 150 | if (!$this->option('skip-npm')) { |
||
| 151 | $this->performNpmOperations(); |
||
| 152 | } |
||
| 153 | |||
| 154 | // Clear caches and perform maintenance |
||
| 155 | $this->performMaintenanceTasks(); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Perform git update with better error handling |
||
| 160 | */ |
||
| 161 | private function performGitUpdate(): void |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Perform composer update with optimization |
||
| 176 | */ |
||
| 177 | private function performComposerUpdate(): void |
||
| 178 | { |
||
| 179 | $this->info('📦 Updating composer dependencies...'); |
||
| 180 | |||
| 181 | $composerResult = $this->call('nntmux:composer'); |
||
| 182 | |||
| 183 | if ($composerResult !== 0) { |
||
| 184 | throw new \Exception('Composer update failed'); |
||
| 185 | } |
||
| 186 | |||
| 187 | $this->progressBar->advance(); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Perform database updates |
||
| 192 | */ |
||
| 193 | private function performDatabaseUpdate(): void |
||
| 194 | { |
||
| 195 | $this->info('🗄️ Updating database...'); |
||
| 196 | |||
| 197 | $dbResult = $this->call('nntmux:db'); |
||
| 198 | |||
| 199 | if ($dbResult !== 0) { |
||
| 200 | throw new \Exception('Database update failed'); |
||
| 201 | } |
||
| 202 | |||
| 203 | $this->progressBar->advance(); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Perform NPM operations with parallel processing where possible |
||
| 208 | */ |
||
| 209 | private function performNpmOperations(): void |
||
| 210 | { |
||
| 211 | // Check if package.json has changed |
||
| 212 | $packageLockExists = File::exists(base_path('package-lock.json')); |
||
| 213 | $shouldInstall = !$packageLockExists || $this->option('force'); |
||
| 214 | |||
| 215 | if ($shouldInstall) { |
||
| 216 | $this->info('📦 Installing npm packages...'); |
||
| 217 | |||
| 218 | $process = Process::timeout(600) |
||
| 219 | ->path(base_path()) |
||
| 220 | ->run('npm ci --silent'); |
||
| 221 | |||
| 222 | if (!$process->successful()) { |
||
| 223 | // Fallback to npm install if ci fails |
||
| 224 | $process = Process::timeout(600) |
||
| 225 | ->path(base_path()) |
||
| 226 | ->run('npm install --silent'); |
||
| 227 | |||
| 228 | if (!$process->successful()) { |
||
| 229 | throw new \Exception('NPM install failed: ' . $process->errorOutput()); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | $this->progressBar->advance(); |
||
| 235 | |||
| 236 | // Build assets |
||
| 237 | $this->info('🔨 Building assets...'); |
||
| 238 | |||
| 239 | $buildProcess = Process::timeout(600) |
||
| 240 | ->path(base_path()) |
||
| 241 | ->run('npm run build'); |
||
| 242 | |||
| 243 | if (!$buildProcess->successful()) { |
||
| 244 | throw new \Exception('Asset build failed: ' . $buildProcess->errorOutput()); |
||
| 245 | } |
||
| 246 | |||
| 247 | $this->progressBar->advance(); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Perform maintenance tasks |
||
| 252 | */ |
||
| 253 | private function performMaintenanceTasks(): void |
||
| 254 | { |
||
| 255 | // Clear Smarty cache |
||
| 256 | $this->info('🧹 Clearing Smarty cache...'); |
||
| 257 | $this->clearSmartyCache(); |
||
| 258 | $this->progressBar->advance(); |
||
| 259 | |||
| 260 | // Merge environment variables |
||
| 261 | $this->info('⚙️ Merging environment configuration...'); |
||
| 262 | $this->mergeEnvironmentConfig(); |
||
| 263 | $this->progressBar->advance(); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Clear Smarty compiled templates |
||
| 268 | */ |
||
| 269 | private function clearSmartyCache(): void |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Merge environment configuration with improved error handling |
||
| 288 | */ |
||
| 289 | private function mergeEnvironmentConfig(): void |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Parse environment file into key-value pairs |
||
| 325 | */ |
||
| 326 | private function parseEnvFile(string $path): array |
||
| 327 | { |
||
| 328 | $content = File::get($path); |
||
| 329 | $vars = []; |
||
| 330 | |||
| 331 | foreach (preg_split("/\r\n|\n|\r/", $content) as $line) { |
||
| 332 | $line = trim($line); |
||
| 333 | |||
| 334 | if (empty($line) || str_starts_with($line, '#')) { |
||
| 335 | continue; |
||
| 336 | } |
||
| 337 | |||
| 338 | if (preg_match('/^([^=]+)=(.*)$/', $line, $matches)) { |
||
| 339 | $key = trim($matches[1]); |
||
| 340 | $value = $matches[2]; |
||
| 341 | $vars[$key] = $value; |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | return $vars; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Add missing environment variables to .env file |
||
| 350 | */ |
||
| 351 | private function addMissingEnvVars(string $envPath, array $missingKeys): void |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Finalize the update process |
||
| 370 | */ |
||
| 371 | private function finalizeUpdate(): void |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Restore the original environment state |
||
| 386 | */ |
||
| 387 | private function restoreEnvironment(): void |
||
| 397 | } |
||
| 398 | } |
||
| 400 |