| Total Complexity | 52 |
| Total Lines | 383 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like NntmuxResetPostProcessing 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 NntmuxResetPostProcessing, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class NntmuxResetPostProcessing extends Command |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | private static $allowedCategories = [ |
||
| 15 | 'music', |
||
| 16 | 'console', |
||
| 17 | 'movie', |
||
| 18 | 'game', |
||
| 19 | 'tv', |
||
| 20 | 'adult', |
||
| 21 | 'book', |
||
| 22 | 'misc', |
||
| 23 | ]; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The name and signature of the console command. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $signature = 'nntmux:resetpp {--c|category=* : Reset all, multiple or single category (music, console, movie, game, tv, adult, book, misc). Supports comma-separated and repeated options}'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The console command description. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $description = 'Reset all, multiple or single release category postprocessing'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Create a new command instance. |
||
| 41 | * |
||
| 42 | * @return void |
||
| 43 | */ |
||
| 44 | public function __construct() |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Execute the console command. |
||
| 51 | */ |
||
| 52 | public function handle(): void |
||
| 53 | { |
||
| 54 | |||
| 55 | // Allow resetting categories only if environment is local and category is 'misc' |
||
| 56 | if (app()->environment() !== 'local' && ((isset($this->option('category')['0']) && $this->option('category')[0] !== 'misc') || ! isset($this->option('category')['0']))) { |
||
|
|
|||
| 57 | $this->error('This command can only be run in local environment'); |
||
| 58 | |||
| 59 | return; |
||
| 60 | } |
||
| 61 | |||
| 62 | $raw = (array) $this->option('category'); |
||
| 63 | if (empty($raw)) { |
||
| 64 | $qry = Release::query()->select(['id'])->get(); |
||
| 65 | $total = \count($qry); |
||
| 66 | if ($total > 0) { |
||
| 67 | $bar = $this->output->createProgressBar($total); |
||
| 68 | $bar->setOverwrite(true); // Terminal needs to support ANSI Encoding for this? |
||
| 69 | $bar->start(); |
||
| 70 | $this->info('Resetting all postprocessing'); |
||
| 71 | foreach ($qry as $releases) { |
||
| 72 | Release::query()->where('id', $releases->id)->update( |
||
| 73 | [ |
||
| 74 | 'consoleinfo_id' => null, |
||
| 75 | 'gamesinfo_id' => null, |
||
| 76 | 'imdbid' => null, |
||
| 77 | 'movieinfo_id' => null, |
||
| 78 | 'musicinfo_id' => null, |
||
| 79 | 'bookinfo_id' => null, |
||
| 80 | 'videos_id' => 0, |
||
| 81 | 'tv_episodes_id' => 0, |
||
| 82 | 'xxxinfo_id' => 0, |
||
| 83 | 'passwordstatus' => -1, |
||
| 84 | 'haspreview' => -1, |
||
| 85 | 'jpgstatus' => 0, |
||
| 86 | 'videostatus' => 0, |
||
| 87 | 'audiostatus' => 0, |
||
| 88 | 'nfostatus' => -1, |
||
| 89 | ] |
||
| 90 | ); |
||
| 91 | $bar->advance(); |
||
| 92 | } |
||
| 93 | $bar->finish(); |
||
| 94 | $this->newLine(); |
||
| 95 | } else { |
||
| 96 | $this->info('No releases to reset'); |
||
| 97 | } |
||
| 98 | } else { |
||
| 99 | $normalized = $this->normalizeCategories($raw); |
||
| 100 | |||
| 101 | // Validate |
||
| 102 | $invalid = $this->invalidCategories($normalized); |
||
| 103 | if (! empty($invalid)) { |
||
| 104 | $this->error('Unknown category option(s): '.implode(', ', $invalid)); |
||
| 105 | $this->line('Allowed: '.implode(', ', self::$allowedCategories).' (or omit --category to reset all).'); |
||
| 106 | |||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | // If user explicitly passed 'all', treat as full reset |
||
| 111 | if (in_array('all', $normalized, true) || empty($normalized)) { |
||
| 112 | $this->call('nntmux:resetpp'); // fall back to full reset |
||
| 113 | |||
| 114 | return; |
||
| 115 | } |
||
| 116 | |||
| 117 | foreach ($normalized as $adjusted) { |
||
| 118 | // skip 'all' since handled above |
||
| 119 | if ($adjusted === 'all') { |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | $this->info('Resetting postprocessing for '.$adjusted.' category'); |
||
| 123 | switch ($adjusted) { |
||
| 124 | case 'console': |
||
| 125 | $this->resetConsole(); |
||
| 126 | break; |
||
| 127 | case 'movie': |
||
| 128 | $this->resetMovies(); |
||
| 129 | break; |
||
| 130 | case 'game': |
||
| 131 | $this->resetGames(); |
||
| 132 | break; |
||
| 133 | case 'book': |
||
| 134 | $this->resetBooks(); |
||
| 135 | break; |
||
| 136 | case 'music': |
||
| 137 | $this->resetMusic(); |
||
| 138 | break; |
||
| 139 | case 'adult': |
||
| 140 | $this->resetAdult(); |
||
| 141 | break; |
||
| 142 | case 'tv': |
||
| 143 | $this->resetTv(); |
||
| 144 | break; |
||
| 145 | case 'misc': |
||
| 146 | $this->resetMisc(); |
||
| 147 | break; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Normalize raw category options into a unique, lowercased list. |
||
| 155 | * Handles comma-separated values, repeated options, casing, simple plurals, |
||
| 156 | * and values provided as key=value (e.g. category=tv or the single-dash typo -category=tv). |
||
| 157 | */ |
||
| 158 | private function normalizeCategories(array $raw): array |
||
| 159 | { |
||
| 160 | $normalized = collect($raw) |
||
| 161 | ->flatMap(function ($opt) { |
||
| 162 | $opt = is_array($opt) ? implode(',', $opt) : (string) $opt; |
||
| 163 | |||
| 164 | return preg_split('/[\s,]+/', $opt, -1, PREG_SPLIT_NO_EMPTY); |
||
| 165 | }) |
||
| 166 | ->map(function ($opt) { |
||
| 167 | $opt = trim((string) $opt); |
||
| 168 | // If the token contains '=', take the substring after the last '=' |
||
| 169 | if (str_contains($opt, '=')) { |
||
| 170 | $parts = explode('=', $opt); |
||
| 171 | $opt = end($parts); |
||
| 172 | } |
||
| 173 | $opt = strtolower(trim($opt)); |
||
| 174 | // normalize common plurals |
||
| 175 | $singular = rtrim($opt, 's'); |
||
| 176 | if (in_array($singular, self::$allowedCategories, true)) { |
||
| 177 | return $singular; |
||
| 178 | } |
||
| 179 | |||
| 180 | return $opt; |
||
| 181 | }) |
||
| 182 | ->filter() |
||
| 183 | ->unique() |
||
| 184 | ->values() |
||
| 185 | ->all(); |
||
| 186 | |||
| 187 | return $normalized; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Return invalid categories from a normalized list. |
||
| 192 | * Keeps 'all' as a special allowed token. |
||
| 193 | */ |
||
| 194 | private function invalidCategories(array $normalized): array |
||
| 195 | { |
||
| 196 | return collect($normalized) |
||
| 197 | ->reject(function ($opt) { |
||
| 198 | return in_array($opt, self::$allowedCategories, true) || $opt === 'all'; |
||
| 199 | }) |
||
| 200 | ->values() |
||
| 201 | ->all(); |
||
| 202 | } |
||
| 203 | |||
| 204 | private function resetConsole(): void |
||
| 205 | { |
||
| 206 | $qry = Release::query()->whereNotNull('consoleinfo_id')->whereBetween('categories_id', [Category::GAME_ROOT, Category::GAME_OTHER])->get(); |
||
| 207 | $total = $qry->count(); |
||
| 208 | if ($total > 0) { |
||
| 209 | $bar = $this->output->createProgressBar($total); |
||
| 210 | $bar->setOverwrite(true); // Terminal needs to support ANSI Encoding for this? |
||
| 211 | $bar->start(); |
||
| 212 | foreach ($qry as $releases) { |
||
| 213 | Release::query()->where('id', $releases->id)->update( |
||
| 214 | [ |
||
| 215 | 'consoleinfo_id' => null, |
||
| 216 | ]); |
||
| 217 | $bar->advance(); |
||
| 218 | } |
||
| 219 | $bar->finish(); |
||
| 220 | $this->newLine(); |
||
| 221 | $this->info(number_format($total).' consoleinfo_id\'s reset.'); |
||
| 222 | } else { |
||
| 223 | $this->info('No releases to reset'); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | private function resetMovies(): void |
||
| 228 | { |
||
| 229 | $qry = Release::query()->whereNotNull('movieinfo_id')->whereBetween('categories_id', [Category::MOVIE_ROOT, Category::MOVIE_OTHER])->get(); |
||
| 230 | $total = $qry->count(); |
||
| 231 | if ($total > 0) { |
||
| 232 | $bar = $this->output->createProgressBar($total); |
||
| 233 | $bar->setOverwrite(true); // Terminal needs to support ANSI Encoding for this? |
||
| 234 | $bar->start(); |
||
| 235 | foreach ($qry as $releases) { |
||
| 236 | Release::query()->where('id', $releases->id)->update( |
||
| 237 | [ |
||
| 238 | 'movieinfo_id' => null, |
||
| 239 | 'imdbid' => null, |
||
| 240 | ]); |
||
| 241 | $bar->advance(); |
||
| 242 | } |
||
| 243 | $bar->finish(); |
||
| 244 | $this->newLine(); |
||
| 245 | $this->info(number_format($total).' movieinfo_id\'s reset.'); |
||
| 246 | } else { |
||
| 247 | $this->info('No releases to reset'); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | private function resetGames(): void |
||
| 252 | { |
||
| 253 | $qry = Release::query()->whereNotNull('gamesinfo_id')->where('categories_id', '=', Category::PC_GAMES)->get(); |
||
| 254 | $total = $qry->count(); |
||
| 255 | if ($total > 0) { |
||
| 256 | $bar = $this->output->createProgressBar($total); |
||
| 257 | $bar->setOverwrite(true); // Terminal needs to support ANSI Encoding for this? |
||
| 258 | $bar->start(); |
||
| 259 | foreach ($qry as $releases) { |
||
| 260 | Release::query()->where('id', $releases->id)->update( |
||
| 261 | [ |
||
| 262 | 'gamesinfo_id' => null, |
||
| 263 | ]); |
||
| 264 | $bar->advance(); |
||
| 265 | } |
||
| 266 | $bar->finish(); |
||
| 267 | $this->newLine(); |
||
| 268 | $this->info(number_format($total).' gamesinfo_id\'s reset.'); |
||
| 269 | } else { |
||
| 270 | $this->info('No releases to reset'); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | private function resetBooks(): void |
||
| 275 | { |
||
| 276 | $qry = Release::query()->whereNotNull('bookinfo_id')->whereBetween('categories_id', [Category::BOOKS_ROOT, Category::BOOKS_UNKNOWN])->get(); |
||
| 277 | $total = $qry->count(); |
||
| 278 | if ($total > 0) { |
||
| 279 | $bar = $this->output->createProgressBar($total); |
||
| 280 | $bar->setOverwrite(true); // Terminal needs to support ANSI Encoding for this? |
||
| 281 | $bar->start(); |
||
| 282 | foreach ($qry as $releases) { |
||
| 283 | Release::query()->where('id', $releases->id)->update( |
||
| 284 | [ |
||
| 285 | 'bookinfo_id' => null, |
||
| 286 | ]); |
||
| 287 | $bar->advance(); |
||
| 288 | } |
||
| 289 | $bar->finish(); |
||
| 290 | $this->newLine(); |
||
| 291 | $this->info(number_format($total).' bookinfo_id\'s reset.'); |
||
| 292 | } else { |
||
| 293 | $this->info('No releases to reset'); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | private function resetMusic(): void |
||
| 298 | { |
||
| 299 | $qry = Release::query()->whereNotNull('musicinfo_id')->whereBetween('categories_id', [Category::MUSIC_ROOT, Category::MUSIC_OTHER])->get(); |
||
| 300 | $total = $qry->count(); |
||
| 301 | if ($total > 0) { |
||
| 302 | $bar = $this->output->createProgressBar($total); |
||
| 303 | $bar->setOverwrite(true); // Terminal needs to support ANSI Encoding for this? |
||
| 304 | $bar->start(); |
||
| 305 | foreach ($qry as $releases) { |
||
| 306 | Release::query()->where('id', $releases->id)->update( |
||
| 307 | [ |
||
| 308 | 'musicinfo_id' => null, |
||
| 309 | ]); |
||
| 310 | $bar->advance(); |
||
| 311 | } |
||
| 312 | $bar->finish(); |
||
| 313 | $this->newLine(); |
||
| 314 | $this->info(number_format($total).' musicinfo_id\'s reset.'); |
||
| 315 | } else { |
||
| 316 | $this->info('No releases to reset'); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | private function resetAdult(): void |
||
| 321 | { |
||
| 322 | $qry = Release::query()->whereNotNull('xxxinfo_id')->whereBetween('categories_id', [Category::XXX_ROOT, Category::XXX_OTHER])->get(); |
||
| 323 | $total = $qry->count(); |
||
| 324 | if ($total > 0) { |
||
| 325 | $bar = $this->output->createProgressBar($total); |
||
| 326 | $bar->setOverwrite(true); // Terminal needs to support ANSI Encoding for this? |
||
| 327 | $bar->start(); |
||
| 328 | foreach ($qry as $releases) { |
||
| 329 | Release::query()->where('id', $releases->id)->update( |
||
| 330 | [ |
||
| 331 | 'xxxinfo_id' => null, |
||
| 332 | ]); |
||
| 333 | $bar->advance(); |
||
| 334 | } |
||
| 335 | $bar->finish(); |
||
| 336 | $this->newLine(); |
||
| 337 | $this->info(number_format($total).' xxxinfo_id\'s reset.'); |
||
| 338 | } else { |
||
| 339 | $this->info('No releases to reset'); |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | private function resetTv(): void |
||
| 344 | { |
||
| 345 | $qry = Release::query()->where('videos_id', '!=', 0)->where('tv_episodes_id', '!=', 0)->whereBetween('categories_id', [Category::TV_ROOT, Category::TV_OTHER])->get(); |
||
| 346 | $total = $qry->count(); |
||
| 347 | if ($total > 0) { |
||
| 348 | $bar = $this->output->createProgressBar($total); |
||
| 349 | $bar->setOverwrite(true); // Terminal needs to support ANSI Encoding for this? |
||
| 350 | $bar->start(); |
||
| 351 | foreach ($qry as $releases) { |
||
| 352 | Release::query()->where('id', $releases->id)->update( |
||
| 353 | [ |
||
| 354 | 'videos_id' => 0, |
||
| 355 | 'tv_episodes_id' => 0, |
||
| 356 | ]); |
||
| 357 | $bar->advance(); |
||
| 358 | } |
||
| 359 | $bar->finish(); |
||
| 360 | $this->newLine(); |
||
| 361 | $this->info(number_format($total).' video_id\'s reset.'); |
||
| 362 | } else { |
||
| 363 | $this->info('No releases to reset'); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | private function resetMisc(): void |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 |