Passed
Push — master ( 4f5b3b...1c5d75 )
by Darko
09:48
created

NntmuxResetPostProcessing::resetMovies()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 21
rs 9.7333
cc 3
nc 3
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Category;
6
use App\Models\Release;
7
use Illuminate\Console\Command;
8
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()
45
    {
46
        parent::__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']))) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        if (app()->/** @scrutinizer ignore-call */ environment() !== 'local' && ((isset($this->option('category')['0']) && $this->option('category')[0] !== 'misc') || ! isset($this->option('category')['0']))) {
Loading history...
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)
0 ignored issues
show
Bug introduced by
$raw of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
        $normalized = collect(/** @scrutinizer ignore-type */ $raw)
Loading history...
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)
0 ignored issues
show
Bug introduced by
$normalized of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

196
        return collect(/** @scrutinizer ignore-type */ $normalized)
Loading history...
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
368
    {
369
        $qry = Release::query()->whereBetween('categories_id', [Category::OTHER_ROOT, Category::OTHER_HASHED])->get();
370
        $total = $qry->count();
371
        if ($total > 0) {
372
            $bar = $this->output->createProgressBar($total);
373
            $bar->setOverwrite(true); // Terminal needs to support ANSI Encoding for this?
374
            $bar->start();
375
            foreach ($qry as $releases) {
376
                Release::query()->where('id', $releases->id)->update(
377
                    [
378
                        'passwordstatus' => -1,
379
                        'haspreview' => -1,
380
                        'jpgstatus' => 0,
381
                        'videostatus' => 0,
382
                        'audiostatus' => 0,
383
                        'nfostatus' => -1,
384
                    ]);
385
                $bar->advance();
386
            }
387
            $bar->finish();
388
            $this->newLine();
389
            $this->info(number_format($total).' misc releases reset.');
390
        } else {
391
            $this->info('No releases to reset');
392
        }
393
    }
394
}
395