| Conditions | 7 |
| Paths | 78 |
| Total Lines | 71 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 46 | public function handle(): int |
||
| 47 | { |
||
| 48 | $this->colorCli = new ColorCLI; |
||
| 49 | |||
| 50 | try { |
||
| 51 | // Reset old collections if requested |
||
| 52 | if ($this->option('reset-collections')) { |
||
| 53 | $this->resetOldCollections(); |
||
| 54 | } |
||
| 55 | |||
| 56 | // Initialize services |
||
| 57 | $sessionName = $this->option('session') |
||
| 58 | ?? Settings::settingValue('tmux_session') |
||
| 59 | ?? config('tmux.session.default_name', 'nntmux'); |
||
| 60 | |||
| 61 | $this->sessionManager = new TmuxSessionManager($sessionName); |
||
| 62 | $this->monitor = new TmuxMonitorService; |
||
| 63 | $this->taskRunner = new TmuxTaskRunner($sessionName); |
||
| 64 | $this->tmuxOutput = new TmuxOutput; |
||
| 65 | |||
| 66 | // Verify session exists |
||
| 67 | if (! $this->sessionManager->sessionExists()) { |
||
| 68 | $this->error("❌ Tmux session '{$sessionName}' does not exist."); |
||
| 69 | $this->info("💡 Run 'php artisan tmux-ui:start' to create the session first."); |
||
| 70 | |||
| 71 | return Command::FAILURE; |
||
| 72 | } |
||
| 73 | |||
| 74 | $this->colorCli->header('Starting Tmux Monitor'); |
||
| 75 | $this->info("📊 Monitoring session: {$sessionName}"); |
||
| 76 | |||
| 77 | // Initialize monitor |
||
| 78 | $runVar = $this->monitor->initializeMonitor(); |
||
|
|
|||
| 79 | |||
| 80 | // Main monitoring loop |
||
| 81 | $iteration = 0; |
||
| 82 | while ($this->monitor->shouldContinue()) { |
||
| 83 | $iteration++; |
||
| 84 | |||
| 85 | // Collect statistics |
||
| 86 | $runVar = $this->monitor->collectStatistics(); |
||
| 87 | |||
| 88 | // Update display |
||
| 89 | $this->tmuxOutput->updateMonitorPane($runVar); |
||
| 90 | |||
| 91 | // Run pane tasks if tmux is running |
||
| 92 | if ((int) ($runVar['settings']['is_running'] ?? 0) === 1) { |
||
| 93 | $this->runPaneTasks($runVar); |
||
| 94 | } else { |
||
| 95 | if ($iteration % 60 === 0) { // Log every 10 minutes |
||
| 96 | $this->info('⏸️ Tmux is not running. Waiting...'); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | // Increment iteration and sleep |
||
| 101 | $this->monitor->incrementIteration(); |
||
| 102 | sleep(10); |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->info('🛑 Monitor stopped by exit flag'); |
||
| 106 | |||
| 107 | return Command::SUCCESS; |
||
| 108 | |||
| 109 | } catch (\Exception $e) { |
||
| 110 | $this->error('❌ Monitor failed: '.$e->getMessage()); |
||
| 111 | logger()->error('Tmux monitor error', [ |
||
| 112 | 'message' => $e->getMessage(), |
||
| 113 | 'trace' => $e->getTraceAsString(), |
||
| 114 | ]); |
||
| 115 | |||
| 116 | return Command::FAILURE; |
||
| 117 | } |
||
| 236 |