NntmuxResetDb::handle()   C
last analyzed

Complexity

Conditions 11
Paths 32

Size

Total Lines 156
Code Lines 108

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 108
c 1
b 0
f 0
dl 0
loc 156
rs 5.8533
cc 11
nc 32
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\UsenetGroup;
6
use Blacklight\ManticoreSearch;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Facades\File;
10
11
class NntmuxResetDb extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'nntmux:resetdb';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'This command will reset your database to blank state (will retain settings)';
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     * @return void
31
     */
32
    public function __construct()
33
    {
34
        parent::__construct();
35
    }
36
37
    /**
38
     * @throws \Exception
39
     */
40
    public function handle(): void
41
    {
42
        // Allow database reset only if app environment is local
43
        if (app()->environment() !== 'local') {
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

43
        if (app()->/** @scrutinizer ignore-call */ environment() !== 'local') {
Loading history...
44
            $this->error('This command can only be run in local environment');
45
46
            return;
47
        }
48
        if ($this->confirm('This command removes all releases, nzb files, samples, previews , nfos, truncates all article tables and resets all groups. Are you sure you want reset the DB?')) {
49
            $timestart = now();
50
51
            DB::statement('SET FOREIGN_KEY_CHECKS = 0;');
52
53
            UsenetGroup::query()->update([
54
                'first_record' => 0,
55
                'first_record_postdate' => null,
56
                'last_record' => 0,
57
                'last_record_postdate' => null,
58
                'last_updated' => null,
59
            ]);
60
            $this->info('Reseting all groups completed.');
61
62
            $arr = [
63
                'binaries',
64
                'collections',
65
                'parts',
66
                'missed_parts',
67
                'videos',
68
                'tv_episodes',
69
                'tv_info',
70
                'release_nfos',
71
                'release_comments',
72
                'users_releases',
73
                'user_movies',
74
                'user_series',
75
                'movieinfo',
76
                'musicinfo',
77
                'release_files',
78
                'audio_data',
79
                'release_subtitles',
80
                'video_data',
81
                'releases',
82
                'anidb_titles',
83
                'anidb_info',
84
                'anidb_episodes',
85
                'releases_groups',
86
            ];
87
            foreach ($arr as &$value) {
88
                DB::statement("TRUNCATE TABLE $value");
89
                $this->info('Truncating '.$value.' completed.');
90
            }
91
            unset($value);
92
93
            if (config('nntmux.elasticsearch_enabled') === true) {
94
                if (\Elasticsearch::indices()->exists(['index' => 'releases'])) {
95
                    \Elasticsearch::indices()->delete(['index' => 'releases']);
96
                }
97
                $releases_index = [
98
                    'index' => 'releases',
99
                    'body' => [
100
                        'settings' => [
101
                            'number_of_shards' => 2,
102
                            'number_of_replicas' => 0,
103
                        ],
104
                        'mappings' => [
105
                            'properties' => [
106
                                'id' => [
107
                                    'type' => 'long',
108
                                    'index' => false,
109
                                ],
110
                                'name' => ['type' => 'text'],
111
                                'searchname' => [
112
                                    'type' => 'text',
113
                                    'fields' => [
114
                                        'sort' => [
115
                                            'type' => 'keyword',
116
                                        ],
117
                                    ],
118
                                ],
119
                                'plainsearchname' => [
120
                                    'type' => 'text',
121
                                    'fields' => [
122
                                        'sort' => [
123
                                            'type' => 'keyword',
124
                                        ],
125
                                    ],
126
                                ],
127
                                'fromname' => ['type' => 'text'],
128
                                'filename' => ['type' => 'text'],
129
                                'add_date' => ['type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss'],
130
                                'post_date' => ['type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss'],
131
                            ],
132
                        ],
133
                    ],
134
                ];
135
136
                \Elasticsearch::indices()->create($releases_index);
137
138
                if (\Elasticsearch::indices()->exists(['index' => 'predb'])) {
139
                    \Elasticsearch::indices()->delete(['index' => 'predb']);
140
                }
141
                $predb_index = [
142
                    'index' => 'predb',
143
                    'body' => [
144
                        'settings' => [
145
                            'number_of_shards' => 2,
146
                            'number_of_replicas' => 0,
147
                        ],
148
                        'mappings' => [
149
                            'properties' => [
150
                                'id' => [
151
                                    'type' => 'long',
152
                                    'index' => false,
153
                                ],
154
                                'title' => [
155
                                    'type' => 'text',
156
                                    'fields' => [
157
                                        'sort' => [
158
                                            'type' => 'keyword',
159
                                        ],
160
                                    ],
161
                                ],
162
                                'filename' => ['type' => 'text'],
163
                                'source' => ['type' => 'text'],
164
                            ],
165
                        ],
166
                    ],
167
168
                ];
169
170
                \Elasticsearch::indices()->create($predb_index);
171
172
                $this->info('All done! ElasticSearch indexes are deleted and recreated.');
173
            } else {
174
                (new ManticoreSearch)->truncateRTIndex(['releases_rt', 'predb_rt']);
175
            }
176
177
            $this->info('Deleting nzbfiles subfolders.');
178
            $files = File::allFiles(config('nntmux_settings.path_to_nzbs'));
179
            File::delete($files);
180
181
            $this->info('Deleting all images, previews and samples that still remain.');
182
183
            $files = File::allFiles(storage_path('covers/'));
184
            foreach ($files as $file) {
185
                if (basename($file) !== '.gitignore' && basename($file) !== 'no-cover.jpg' && basename($file) !== 'no-backdrop.jpg') {
186
                    File::delete($file);
187
                }
188
            }
189
190
            $this->call('cache:clear');
191
192
            $this->info('Deleted all releases, images, previews and samples. This script finished '.now()->diffForHumans($timestart).' start');
193
            DB::statement('SET FOREIGN_KEY_CHECKS = 1;');
194
        } else {
195
            $this->info('Script execution stopped');
196
        }
197
    }
198
}
199