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