Completed
Push — dev ( 29c9c0...7b715a )
by Darko
05:55
created

NntmuxResetPostProcessing::handle()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 80
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 64
c 2
b 0
f 0
dl 0
loc 80
rs 6.3587
cc 12
nc 12
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Release;
6
use App\Models\Category;
7
use Blacklight\ColorCLI;
8
use Blacklight\ConsoleTools;
9
use Illuminate\Console\Command;
10
11
class NntmuxResetPostProcessing extends Command
12
{
13
    /**
14
     * @var array
15
     */
16
    private static $allowedCategories = [
17
        'music',
18
        'console',
19
        'movie',
20
        'game',
21
        'tv',
22
        'adult',
23
        'misc',
24
        'anime',
25
        'nfo',
26
    ];
27
    /**
28
     * The name and signature of the console command.
29
     *
30
     * @var string
31
     */
32
    protected $signature = 'nntmux:resetpp {--c|category=* : Reset all, multiple or single category}';
33
34
    /**
35
     * The console command description.
36
     *
37
     * @var string
38
     */
39
    protected $description = 'Reset all, multiple or single release category postprocessing';
40
    /**
41
     * @var ColorCLI
42
     */
43
    private $colorCli;
44
45
    /**
46
     * @var ConsoleTools
47
     */
48
    private $consoleTools;
49
50
    /**
51
     * Create a new command instance.
52
     *
53
     * @return void
54
     */
55
    public function __construct()
56
    {
57
        parent::__construct();
58
        $this->colorCli = new ColorCLI();
59
        $this->consoleTools = new ConsoleTools();
60
    }
61
62
    /**
63
     * Execute the console command.
64
     *
65
     * @return mixed
66
     */
67
    public function handle()
68
    {
69
        if (empty($this->option('category'))) {
70
            $qry = Release::query()->select(['id'])->get();
71
            $affected = 0;
72
            $total = \count($qry);
73
            if ($total > 0) {
74
                $this->colorCli->header('Resetting all postprocessing');
75
                foreach ($qry as $releases) {
76
                    Release::query()->where('id', $releases->id)->update(
77
                        [
78
                            'consoleinfo_id' => null,
79
                            'gamesinfo_id' => null,
80
                            'imdbid' => null,
81
                            'musicinfo_id' => null,
82
                            'bookinfo_id' => null,
83
                            'videos_id' => 0,
84
                            'tv_episodes_id' => 0,
85
                            'xxxinfo_id' => 0,
86
                            'passwordstatus' => -1,
87
                            'haspreview' => -1,
88
                            'jpgstatus' => 0,
89
                            'videostatus' => 0,
90
                            'audiostatus' => 0,
91
                            'nfostatus' => -1,
92
                        ]
93
                    );
94
                    $this->consoleTools->overWritePrimary('Resetting Releases:  '.$this->consoleTools->percentString(++$affected, $total));
95
                }
96
            } else {
97
                $this->colorCli->header('No releases to reset');
98
            }
99
        } else {
100
            foreach ($this->option('category') as $option) {
101
                $adjusted = str_replace('=', '', $option);
102
                if (\in_array($adjusted, self::$allowedCategories, false)) {
103
                    $this->info('Resetting postprocessing for '.$adjusted.' category');
104
                    switch ($adjusted) {
105
                        case 'console':
106
                            $this->colorCli->header('Resetting all Console postprocessing');
107
                            $qry = Release::query()->whereNotNull('consoleinfo_id')->whereBetween('categories_id', [Category::GAME_ROOT, Category::GAME_OTHER])->get();
108
                            $total = $qry->count();
109
                            $conCount = 0;
110
                            foreach ($qry as $releases) {
111
                                Release::query()->where('id', $releases->id)->update(
112
                                    [
113
                                        'consoleinfo_id' => null,
114
                                    ]);
115
                                $this->consoleTools->overWritePrimary('Resetting Console Releases:  '.$this->consoleTools->percentString(++$conCount, $total));
116
                            }
117
                            $this->colorCli->header(number_format($conCount).' consoleinfo_id\'s reset.');
118
                            break;
119
                        case 'movie':
120
                            $this->colorCli->header('Resetting all Movie postprocessing');
121
                            $qry = Release::query()->whereNotNull('movieinfo_id')->whereBetween('categories_id', [Category::MOVIE_ROOT, Category::MOVIE_OTHER])->get();
122
                            $total = $qry->count();
123
                            $conCount = 0;
124
                            foreach ($qry as $releases) {
125
                                Release::query()->where('id', $releases->id)->update(
126
                                    [
127
                                        'movieinfo_id' => null,
128
                                    ]);
129
                                $this->consoleTools->overWritePrimary('Resetting Movie Releases:  '.$this->consoleTools->percentString(++$conCount, $total));
130
                            }
131
                            $this->colorCli->header(number_format($conCount).' movieinfo_id\'s reset.');
132
                            break;
133
                        case 'game':
134
                            $this->colorCli->header('Resetting all PC Games postprocessing');
135
                            $qry = Release::query()->whereIn('gamesinfo_id', [-2, 0])->where('categories_id', '=', Category::PC_GAMES)->get();
136
                            $total = $qry->count();
137
                            $conCount = 0;
138
                            foreach ($qry as $releases) {
139
                                Release::query()->where('id', $releases->id)->update(
140
                                    [
141
                                        'gamesinfo_id' => null,
142
                                    ]);
143
                                $this->consoleTools->overWritePrimary('Resetting PC GAME Releases:  '.$this->consoleTools->percentString(++$conCount, $total));
144
                            }
145
                            $this->colorCli->header(number_format($conCount).' gamesinfo_id\'s reset.');
146
                            break;
147
                    }
148
                }
149
            }
150
        }
151
    }
152
}
153