| Total Complexity | 54 |
| Total Lines | 331 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PostProcessRunner 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 PostProcessRunner, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class PostProcessRunner extends BaseRunner |
||
| 11 | { |
||
| 12 | private function runPostProcess(array $releases, int $maxProcesses, string $type, string $desc): void |
||
| 13 | { |
||
| 14 | if (empty($releases)) { |
||
| 15 | $this->headerNone(); |
||
| 16 | |||
| 17 | return; |
||
| 18 | } |
||
| 19 | |||
| 20 | // If streaming is enabled, run commands with real-time output |
||
| 21 | if ((bool) config('nntmux.stream_fork_output', false) === true) { |
||
| 22 | $commands = []; |
||
| 23 | foreach ($releases as $release) { |
||
| 24 | // id may already be a single GUID bucket char; if not, take first char defensively |
||
| 25 | $char = isset($release->id) ? substr((string) $release->id, 0, 1) : ''; |
||
| 26 | // Use postprocess:guid command which accepts the GUID character |
||
| 27 | $commands[] = PHP_BINARY.' artisan postprocess:guid '.$type.' '.$char; |
||
| 28 | } |
||
| 29 | $this->runStreamingCommands($commands, $maxProcesses, $desc); |
||
| 30 | |||
| 31 | return; |
||
| 32 | } |
||
| 33 | |||
| 34 | $count = count($releases); |
||
| 35 | $this->headerStart('postprocess: '.$desc, $count, $maxProcesses); |
||
| 36 | |||
| 37 | // Process in batches using Laravel's native Concurrency facade |
||
| 38 | $batches = array_chunk($releases, max(1, $maxProcesses)); |
||
| 39 | |||
| 40 | foreach ($batches as $batchIndex => $batch) { |
||
| 41 | $tasks = []; |
||
| 42 | foreach ($batch as $idx => $release) { |
||
| 43 | $char = isset($release->id) ? substr((string) $release->id, 0, 1) : ''; |
||
| 44 | // Use postprocess:guid command which accepts the GUID character |
||
| 45 | $command = PHP_BINARY.' artisan postprocess:guid '.$type.' '.$char; |
||
| 46 | $tasks[$idx] = fn () => $this->executeCommand($command); |
||
| 47 | } |
||
| 48 | |||
| 49 | try { |
||
| 50 | $results = Concurrency::run($tasks); |
||
| 51 | |||
| 52 | foreach ($results as $taskIdx => $output) { |
||
| 53 | echo $output; |
||
| 54 | $this->colorCli->primary('Finished task for '.$desc); |
||
| 55 | } |
||
| 56 | } catch (\Throwable $e) { |
||
| 57 | Log::error('Postprocess batch failed: '.$e->getMessage()); |
||
| 58 | $this->colorCli->error('Batch '.($batchIndex + 1).' failed: '.$e->getMessage()); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | public function processAdditional(): void |
||
| 64 | { |
||
| 65 | $ppAddMinSize = Settings::settingValue('minsizetopostprocess') !== '' ? (int) Settings::settingValue('minsizetopostprocess') : 1; |
||
| 66 | $ppAddMinSize = ($ppAddMinSize > 0 ? ('AND r.size > '.($ppAddMinSize * 1048576)) : ''); |
||
| 67 | $ppAddMaxSize = (Settings::settingValue('maxsizetopostprocess') !== '') ? (int) Settings::settingValue('maxsizetopostprocess') : 100; |
||
| 68 | $ppAddMaxSize = ($ppAddMaxSize > 0 ? ('AND r.size < '.($ppAddMaxSize * 1073741824)) : ''); |
||
| 69 | |||
| 70 | $sql = ' |
||
| 71 | SELECT DISTINCT LEFT(r.leftguid, 1) AS id |
||
| 72 | FROM releases r |
||
| 73 | LEFT JOIN categories c ON c.id = r.categories_id |
||
| 74 | WHERE r.passwordstatus = -1 |
||
| 75 | AND r.haspreview = -1 |
||
| 76 | AND c.disablepreview = 0 |
||
| 77 | '.$ppAddMaxSize.' '.$ppAddMinSize.' |
||
| 78 | LIMIT 16'; |
||
| 79 | $queue = DB::select($sql); |
||
| 80 | |||
| 81 | $maxProcesses = (int) Settings::settingValue('postthreads'); |
||
| 82 | $this->runPostProcess($queue, $maxProcesses, 'additional', 'additional postprocessing'); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function processNfo(): void |
||
| 86 | { |
||
| 87 | if ((int) Settings::settingValue('lookupnfo') !== 1) { |
||
| 88 | $this->headerNone(); |
||
| 89 | |||
| 90 | return; |
||
| 91 | } |
||
| 92 | |||
| 93 | $nfoQuery = \Blacklight\Nfo::NfoQueryString(); |
||
| 94 | |||
| 95 | $checkSql = 'SELECT r.id FROM releases r WHERE 1=1 '.$nfoQuery.' LIMIT 1'; |
||
| 96 | if (count(DB::select($checkSql)) === 0) { |
||
| 97 | $this->headerNone(); |
||
| 98 | |||
| 99 | return; |
||
| 100 | } |
||
| 101 | |||
| 102 | $sql = ' |
||
| 103 | SELECT DISTINCT LEFT(r.leftguid, 1) AS id |
||
| 104 | FROM releases r |
||
| 105 | WHERE 1=1 '.$nfoQuery.' |
||
| 106 | LIMIT 16'; |
||
| 107 | $queue = DB::select($sql); |
||
| 108 | |||
| 109 | $maxProcesses = (int) Settings::settingValue('nfothreads'); |
||
| 110 | $this->runPostProcess($queue, $maxProcesses, 'nfo', 'nfo postprocessing'); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function processMovies(bool $renamedOnly): void |
||
| 149 | } |
||
| 150 | |||
| 151 | public function processTv(bool $renamedOnly): void |
||
| 152 | { |
||
| 153 | if ((int) Settings::settingValue('lookuptv') <= 0) { |
||
| 154 | $this->headerNone(); |
||
| 155 | |||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | $condLookup = ((int) Settings::settingValue('lookuptv') === 2 ? 'AND isrenamed = 1' : ''); |
||
| 160 | $condRenamedOnly = ($renamedOnly ? 'AND isrenamed = 1' : ''); |
||
| 161 | |||
| 162 | $checkSql = ' |
||
| 163 | SELECT id |
||
| 164 | FROM releases |
||
| 165 | WHERE categories_id BETWEEN 5000 AND 5999 |
||
| 166 | AND categories_id != 5070 |
||
| 167 | AND videos_id = 0 |
||
| 168 | AND size > 1048576 |
||
| 169 | AND tv_episodes_id BETWEEN -3 AND 0 |
||
| 170 | '.$condLookup.' '.$condRenamedOnly.' |
||
| 171 | LIMIT 1'; |
||
| 172 | if (count(DB::select($checkSql)) === 0) { |
||
| 173 | $this->headerNone(); |
||
| 174 | |||
| 175 | return; |
||
| 176 | } |
||
| 177 | |||
| 178 | $renamedFlag = ($renamedOnly ? 2 : 1); |
||
| 179 | $sql = ' |
||
| 180 | SELECT DISTINCT LEFT(leftguid, 1) AS id, '.$renamedFlag.' AS renamed |
||
| 181 | FROM releases |
||
| 182 | WHERE categories_id BETWEEN 5000 AND 5999 |
||
| 183 | AND categories_id != 5070 |
||
| 184 | AND videos_id = 0 |
||
| 185 | AND tv_episodes_id BETWEEN -3 AND 0 |
||
| 186 | AND size > 1048576 |
||
| 187 | '.$condLookup.' '.$condRenamedOnly.' |
||
| 188 | LIMIT 16'; |
||
| 189 | $queue = DB::select($sql); |
||
| 190 | |||
| 191 | $maxProcesses = (int) Settings::settingValue('postthreadsnon'); |
||
| 192 | |||
| 193 | // Use pipelined TV processing for better efficiency |
||
| 194 | $this->runPostProcessTvPipeline($queue, $maxProcesses, 'tv postprocessing (pipelined)', $renamedOnly); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Run pipelined TV post-processing across multiple GUID buckets in parallel. |
||
| 199 | * Each parallel process runs the full provider pipeline sequentially. |
||
| 200 | */ |
||
| 201 | private function runPostProcessTvPipeline(array $releases, int $maxProcesses, string $desc, bool $renamedOnly): void |
||
|
|
|||
| 202 | { |
||
| 203 | if (empty($releases)) { |
||
| 204 | $this->headerNone(); |
||
| 205 | |||
| 206 | return; |
||
| 207 | } |
||
| 208 | |||
| 209 | // If streaming is enabled, run commands with real-time output |
||
| 210 | if ((bool) config('nntmux.stream_fork_output', false) === true) { |
||
| 211 | $commands = []; |
||
| 212 | foreach ($releases as $release) { |
||
| 213 | $char = isset($release->id) ? substr((string) $release->id, 0, 1) : ''; |
||
| 214 | $renamed = isset($release->renamed) ? $release->renamed : ''; |
||
| 215 | // Use the pipelined TV command |
||
| 216 | $commands[] = PHP_BINARY.' artisan postprocess:tv-pipeline '.$char.($renamed ? ' '.$renamed : '').' --mode=pipeline'; |
||
| 217 | } |
||
| 218 | $this->runStreamingCommands($commands, $maxProcesses, $desc); |
||
| 219 | |||
| 220 | return; |
||
| 221 | } |
||
| 222 | |||
| 223 | $count = count($releases); |
||
| 224 | $this->headerStart('postprocess: '.$desc, $count, $maxProcesses); |
||
| 225 | |||
| 226 | // Process in batches using Laravel's native Concurrency facade |
||
| 227 | $batches = array_chunk($releases, max(1, $maxProcesses)); |
||
| 228 | |||
| 229 | foreach ($batches as $batchIndex => $batch) { |
||
| 230 | $tasks = []; |
||
| 231 | foreach ($batch as $idx => $release) { |
||
| 232 | $char = isset($release->id) ? substr((string) $release->id, 0, 1) : ''; |
||
| 233 | $renamed = isset($release->renamed) ? $release->renamed : ''; |
||
| 234 | // Use the pipelined TV command for each GUID bucket |
||
| 235 | $command = PHP_BINARY.' artisan postprocess:tv-pipeline '.$char.($renamed ? ' '.$renamed : '').' --mode=pipeline'; |
||
| 236 | $tasks[$idx] = fn () => $this->executeCommand($command); |
||
| 237 | } |
||
| 238 | |||
| 239 | try { |
||
| 240 | $results = Concurrency::run($tasks); |
||
| 241 | |||
| 242 | foreach ($results as $taskIdx => $output) { |
||
| 243 | echo $output; |
||
| 244 | $this->colorCli->primary('Finished task for '.$desc); |
||
| 245 | } |
||
| 246 | } catch (\Throwable $e) { |
||
| 247 | Log::error('TV pipeline batch failed: '.$e->getMessage()); |
||
| 248 | $this->colorCli->error('Batch '.($batchIndex + 1).' failed: '.$e->getMessage()); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Lightweight check to determine if there is any TV work to process. |
||
| 255 | */ |
||
| 256 | public function hasTvWork(bool $renamedOnly): bool |
||
| 277 | } |
||
| 278 | |||
| 279 | public function processAnime(): void |
||
| 309 | } |
||
| 310 | |||
| 311 | public function processBooks(): void |
||
| 341 | } |
||
| 342 | } |
||
| 344 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.