Total Complexity | 49 |
Total Lines | 394 |
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 |
||
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')) { |
||
92 | $steps++; |
||
93 | } |
||
94 | if (! $this->option('skip-composer')) { |
||
95 | $steps++; |
||
96 | } |
||
97 | if (! $this->option('skip-npm')) { |
||
98 | $steps += 2; |
||
99 | } // install + build |
||
100 | if (! $this->option('skip-db')) { |
||
101 | $steps++; |
||
102 | } |
||
103 | |||
104 | $steps++; // smarty cache clear |
||
105 | $steps++; // env merge |
||
106 | |||
107 | return $steps; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Prepare the environment for updates |
||
112 | */ |
||
113 | private function prepareEnvironment(): void |
||
114 | { |
||
115 | $this->info('🔧 Preparing environment...'); |
||
116 | |||
117 | // Check if app is in maintenance mode |
||
118 | $this->wasInMaintenance = App::isDownForMaintenance(); |
||
119 | if (! $this->wasInMaintenance) { |
||
120 | $this->call('down', [ |
||
121 | '--render' => 'errors::maintenance', |
||
122 | '--retry' => 120, |
||
123 | '--secret' => config('app.key'), |
||
124 | ]); |
||
125 | } |
||
126 | |||
127 | // Check if tmux is running |
||
128 | $tmux = new Tmux; |
||
129 | $this->tmuxWasRunning = $tmux->isRunning(); |
||
130 | if ($this->tmuxWasRunning) { |
||
131 | $this->call('tmux-ui:stop', ['--kill' => true]); |
||
132 | } |
||
133 | |||
134 | $this->progressBar->advance(); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Execute the main update steps |
||
139 | */ |
||
140 | private function executeUpdateSteps(): void |
||
141 | { |
||
142 | // Git operations |
||
143 | if (! $this->option('skip-git')) { |
||
144 | $this->performGitUpdate(); |
||
145 | } |
||
146 | |||
147 | // Composer operations |
||
148 | if (! $this->option('skip-composer')) { |
||
149 | $this->performComposerUpdate(); |
||
150 | } |
||
151 | |||
152 | // Database migrations |
||
153 | if (! $this->option('skip-db')) { |
||
154 | $this->performDatabaseUpdate(); |
||
155 | } |
||
156 | |||
157 | // NPM operations |
||
158 | if (! $this->option('skip-npm')) { |
||
159 | $this->performNpmOperations(); |
||
160 | } |
||
161 | |||
162 | // Clear caches and perform maintenance |
||
163 | $this->performMaintenanceTasks(); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Perform git update with better error handling |
||
168 | */ |
||
169 | private function performGitUpdate(): void |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Perform composer update with optimization |
||
184 | */ |
||
185 | private function performComposerUpdate(): void |
||
186 | { |
||
187 | $this->info('📦 Updating composer dependencies...'); |
||
188 | |||
189 | $composerResult = $this->call('nntmux:composer'); |
||
190 | |||
191 | if ($composerResult !== 0) { |
||
192 | throw new \Exception('Composer update failed'); |
||
193 | } |
||
194 | |||
195 | $this->progressBar->advance(); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Perform database updates |
||
200 | */ |
||
201 | private function performDatabaseUpdate(): void |
||
202 | { |
||
203 | $this->info('🗄️ Updating database...'); |
||
204 | |||
205 | $dbResult = $this->call('nntmux:db'); |
||
206 | |||
207 | if ($dbResult !== 0) { |
||
208 | throw new \Exception('Database update failed'); |
||
209 | } |
||
210 | |||
211 | $this->progressBar->advance(); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Perform NPM operations with parallel processing where possible |
||
216 | */ |
||
217 | private function performNpmOperations(): void |
||
218 | { |
||
219 | // Check if package.json has changed |
||
220 | $packageLockExists = File::exists(base_path('package-lock.json')); |
||
221 | $shouldInstall = ! $packageLockExists || $this->option('force'); |
||
222 | |||
223 | if ($shouldInstall) { |
||
224 | $this->info('📦 Installing npm packages...'); |
||
225 | |||
226 | $process = Process::timeout(600) |
||
227 | ->path(base_path()) |
||
228 | ->run('npm ci --silent'); |
||
229 | |||
230 | if (! $process->successful()) { |
||
231 | // Fallback to npm install if ci fails |
||
232 | $process = Process::timeout(600) |
||
233 | ->path(base_path()) |
||
234 | ->run('npm install --silent'); |
||
235 | |||
236 | if (! $process->successful()) { |
||
237 | throw new \Exception('NPM install failed: '.$process->errorOutput()); |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | |||
242 | $this->progressBar->advance(); |
||
243 | |||
244 | // Build assets |
||
245 | $this->info('🔨 Building assets...'); |
||
246 | |||
247 | $buildProcess = Process::timeout(600) |
||
248 | ->path(base_path()) |
||
249 | ->run('npm run build'); |
||
250 | |||
251 | if (! $buildProcess->successful()) { |
||
252 | throw new \Exception('Asset build failed: '.$buildProcess->errorOutput()); |
||
253 | } |
||
254 | |||
255 | $this->progressBar->advance(); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Perform maintenance tasks |
||
260 | */ |
||
261 | private function performMaintenanceTasks(): void |
||
262 | { |
||
263 | // Clear Smarty cache |
||
264 | $this->info('🧹 Clearing Smarty cache...'); |
||
265 | $this->clearSmartyCache(); |
||
266 | $this->progressBar->advance(); |
||
267 | |||
268 | // Merge environment variables |
||
269 | $this->info('⚙️ Merging environment configuration...'); |
||
270 | $this->mergeEnvironmentConfig(); |
||
271 | $this->progressBar->advance(); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Clear Smarty compiled templates |
||
276 | */ |
||
277 | private function clearSmartyCache(): void |
||
291 | } |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Merge environment configuration with improved error handling |
||
296 | */ |
||
297 | private function mergeEnvironmentConfig(): void |
||
331 | } |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Parse environment file into key-value pairs |
||
336 | */ |
||
337 | private function parseEnvFile(string $path): array |
||
338 | { |
||
339 | $content = File::get($path); |
||
340 | $vars = []; |
||
341 | |||
342 | foreach (preg_split("/\r\n|\n|\r/", $content) as $line) { |
||
343 | $line = trim($line); |
||
344 | |||
345 | if (empty($line) || str_starts_with($line, '#')) { |
||
346 | continue; |
||
347 | } |
||
348 | |||
349 | if (preg_match('/^([^=]+)=(.*)$/', $line, $matches)) { |
||
350 | $key = trim($matches[1]); |
||
351 | $value = $matches[2]; |
||
352 | $vars[$key] = $value; |
||
353 | } |
||
354 | } |
||
355 | |||
356 | return $vars; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Add missing environment variables to .env file |
||
361 | */ |
||
362 | private function addMissingEnvVars(string $envPath, array $missingKeys): void |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Finalize the update process |
||
381 | */ |
||
382 | private function finalizeUpdate(): void |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Restore the original environment state |
||
397 | */ |
||
398 | private function restoreEnvironment(): void |
||
408 | } |
||
409 | } |
||
411 |